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.

const string inputFile = @"..\..\..\inputDocuments\PackingLightBrochure.pdf";
using (FileStream inFile = new FileStream(inputFile, FileMode.Open, FileAccess.Read))
{
    // open the source document
    var document = new Document(inFile);

    // stamp each page
    for (int i = 0; i < document.Pages.Count; i++)
    {
        Page page = document.Pages[i];

        // create a shape image
        ImageShape image = new ImageShape(@"..\..\logo.gif");

        TranslateTransform translate = new TranslateTransform();
        image.Transform = translate;

        // position at center of page
        image.Width *= 2;
        translate.X = page.Width / 2 - image.Width / 2;
        translate.Y = page.Height / 2 - image.Height / 2;

        // at the shape image to the overlay of the page
        page.Overlay.Add(image);
    }

    // write the modified document to disk
    const string fileName = @"..\..\stampimage.pdf";
    using (FileStream outFile = new FileStream(fileName, FileMode.Create, FileAccess.Write))
    {
        document.Write(outFile);
    }
}
Const inputFile As String = "..\..\..\inputDocuments\PackingLightBrochure.pdf"
Using inFile As New FileStream(inputFile, FileMode.Open, FileAccess.Read)
    ' open the source document
    Dim document = New Document(inFile)

    ' stamp each page
    For i As Integer = 0 To document.Pages.Count - 1
        Dim page As Page = document.Pages(i)

        ' create a shape image
        Dim image As New ImageShape("..\..\logo.gif")

        Dim translate As New TranslateTransform()
        image.Transform = translate

        ' position at center of page
        image.Width *= 2
        translate.X = page.Width / 2 - image.Width / 2
        translate.Y = page.Height / 2 - image.Height / 2

        ' at the shape image to the overlay of the page
        page.Overlay.Add(image)
    Next

    ' write the modified document to disk
    Const fileName As String = "..\..\stampimage.pdf"
    Using outFile As New FileStream(fileName, FileMode.Create, FileAccess.Write)
        document.Write(outFile)
    End Using
End Using

The original PDF:

PDF-without-stamp.png

The PDF with the stamp:

PDF-add-stamp.png