Add a Diagonal Watermark to PDF in C#

Shapes, Manipulate PDF
8/29/2011

Downloads

This code sample shows how to add a diagonal watermark to an existing PDF in C#.

pdf-add-watermark.PNG

C# code sample

1 static void Main(string[] args) 2 { 3 using (FileStream sourceFile = 4 new FileStream("original.pdf", FileMode.Open, FileAccess.Read)) 5 { 6 // open the document 7 Document pdf = new Document(sourceFile); 8 foreach (Page page in pdf.Pages) 9 { 10 // stamp each page 11 addWatermark(page); 12 } 13 14 // save the modified pdf 15 using (FileStream targetFile = 16 new FileStream("watermarked.pdf", FileMode.Create, FileAccess.Write)) 17 { 18 pdf.Write(targetFile); 19 } 20 } 21 }

VB.NET code sample

1 Using sourceFile As New FileStream("..\..\input.pdf", FileMode.Open, FileAccess.Read) 2 ' open the document 3 Dim pdf As New Document(sourceFile) 4 For Each page As Page In pdf.Pages 5 ' stamp each page 6 addWatermark(page) 7 Next 8 9 ' save the modified pdf 10 Using targetFile As New FileStream("..\..\output.pdf", FileMode.Create, FileAccess.Write) 11 pdf.Write(targetFile) 12 End Using 13 End Using

The following method adds the watermark to PDF.

C# code sample

1 static void addWatermark(Page page) 2 { 3 // calculate the page diagonal 4 double diagonal = Math.Sqrt(page.Height * page.Height + page.Width * page.Width); 5 6 // format the text 7 TextShape watermark = new TextShape(); 8 watermark.Text = "Diagonal watermark"; 9 watermark.Font = Font.CourierBold; 10 watermark.Pen = new Pen(RgbColor.Red, 2); 11 watermark.Brush = null; 12 watermark.FontSize = 1; // recalculated below 13 14 // calculate font size so to fit the diagonal of the page - 0.8 is a fuzzy factor 15 watermark.FontSize *= 0.8 * (diagonal / watermark.MeasuredWidth); 16 17 // transform the text so that it runs diagonal across the page 18 TransformCollection transforms = new TransformCollection(); 19 transforms.Add(new TranslateTransform(0, -0.5 * watermark.MeasuredHeight)); 20 transforms.Add(new TranslateTransform((diagonal - watermark.MeasuredWidth) / 2, 0)); 21 transforms.Add(new RotateTransform(-(180 / Math.PI) * Math.Atan2(page.Height, page.Width))); 22 watermark.Transform = transforms; 23 24 page.Overlay.Add(watermark); 25 }

VB.NET code sample

1 Private Sub addWatermark(page As Page) 2 ' calculate the page diagonal 3 Dim diagonal As Double = Math.Sqrt(page.Height * page.Height + page.Width * page.Width) 4 5 ' format the text 6 Dim watermark As New TextShape() 7 watermark.Text = "Diagonal watermark" 8 watermark.Font = Font.CourierBold 9 watermark.Pen = New Pen(RgbColor.Red, 2) 10 watermark.Brush = Nothing 11 watermark.FontSize = 1 12 ' recalculated below 13 ' calculate font size so to fit the diagonal of the page - 0.8 is a fuzzy factor 14 watermark.FontSize *= 0.8 * (diagonal / watermark.MeasuredWidth) 15 16 ' transform the text so that it runs diagonal across the page 17 Dim transforms As New TransformCollection() 18 transforms.Add(New TranslateTransform(0, -0.5 * watermark.MeasuredHeight)) 19 transforms.Add(New TranslateTransform((diagonal - watermark.MeasuredWidth) / 2, 0)) 20 transforms.Add(New RotateTransform(-(180 / Math.PI) * Math.Atan2(page.Height, page.Width))) 21 watermark.Transform = transforms 22 23 page.Overlay.Add(watermark) 24 End Sub