Add Stamp to PDF

Manipulate PDF, Shapes
6/5/2014

Downloads

This code sample shows how to stamp a PDF in C#. You can stamp images and watermarks to your PDF for example.

C# code sample

1 const string inputFile = @"..\..\..\inputDocuments\PackingLightBrochure.pdf"; 2 using (FileStream inFile = new FileStream(inputFile, FileMode.Open, FileAccess.Read)) 3 { 4 // open the source document 5 var document = new Document(inFile); 6 7 // stamp each page 8 for (int i = 0; i < document.Pages.Count; i++) 9 { 10 Page page = document.Pages[i]; 11 12 // create a shape image 13 ImageShape image = new ImageShape(@"..\..\logo.gif"); 14 15 TranslateTransform translate = new TranslateTransform(); 16 image.Transform = translate; 17 18 // position at center of page 19 image.Width *= 2; 20 translate.X = page.Width / 2 - image.Width / 2; 21 translate.Y = page.Height / 2 - image.Height / 2; 22 23 // at the shape image to the overlay of the page 24 page.Overlay.Add(image); 25 } 26 27 // write the modified document to disk 28 const string fileName = @"..\..\stampimage.pdf"; 29 using (FileStream outFile = new FileStream(fileName, FileMode.Create, FileAccess.Write)) 30 { 31 document.Write(outFile); 32 } 33 }

VB.NET code sample

1 Const inputFile As String = "..\..\..\inputDocuments\PackingLightBrochure.pdf" 2 Using inFile As New FileStream(inputFile, FileMode.Open, FileAccess.Read) 3 ' open the source document 4 Dim document = New Document(inFile) 5 6 ' stamp each page 7 For i As Integer = 0 To document.Pages.Count - 1 8 Dim page As Page = document.Pages(i) 9 10 ' create a shape image 11 Dim image As New ImageShape("..\..\logo.gif") 12 13 Dim translate As New TranslateTransform() 14 image.Transform = translate 15 16 ' position at center of page 17 image.Width *= 2 18 translate.X = page.Width / 2 - image.Width / 2 19 translate.Y = page.Height / 2 - image.Height / 2 20 21 ' at the shape image to the overlay of the page 22 page.Overlay.Add(image) 23 Next 24 25 ' write the modified document to disk 26 Const fileName As String = "..\..\stampimage.pdf" 27 Using outFile As New FileStream(fileName, FileMode.Create, FileAccess.Write) 28 document.Write(outFile) 29 End Using 30 End Using

The original PDF:

PDF-without-stamp.png

The PDF with the stamp:

PDF-add-stamp.png