How to add an image stamp to a PDF file in C#

The stamp in the PDF file gives the additional information for the PDF file, such as to protect the PDF file to be used by others and to confirm the security of the contents of the PDF file. We have already introduced how to create a dynamic stamp in PDF with the help of Spire.PDF. This article will show you how to add an image stamp to a PDF file in C#.

Note: Before Start, please download the latest version of Spire.PDF and add Spire.Pdf.dll in the bin folder as the reference of Visual Studio.

Here comes to the steps of how to create an image stamp to a PDF in C#:

Step 1: Create a new PDF document and load from file.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile("Test.pdf");

Step 2: Get the first page of the PDF file.

PdfPageBase page = doc.Pages[0];

Step 3: Create a rubber stamp annotation.

PdfRubberStampAnnotation loStamp = new PdfRubberStampAnnotation(new RectangleF(new PointF(0, 0), new SizeF(60, 60)));

Step 4: Create an instance of PdfAppearance.

PdfAppearance loApprearance = new PdfAppearance(loStamp);

Step 5: Load the image to be used as image stamp.

PdfImage image = PdfImage.FromFile("Stamp.jpg");

Step 6: Create a new template and draw a pdf image into pdf template.

PdfTemplate template = new PdfTemplate(160, 160);
template.Graphics.DrawImage(image, 0, 0);
loApprearance.Normal = template;
loStamp.Appearance = loApprearance;

Step 7: Add the rubber stamp annotation into the PDF.

page.AnnotationsWidget.Add(loStamp);

Step 8: Save the document to file.

string output = "ImageStamp.pdf";
doc.SaveToFile(output);

Effective screenshot after adding the image stamp to PDF file:

How to add an image stamp to a PDF file in C#

Full codes:

static void Main(string[] args)
{
    PdfDocument doc = new PdfDocument();
    doc.LoadFromFile("Test.pdf");
  
    PdfPageBase page = doc.Pages[0];
            
    PdfRubberStampAnnotation loStamp = new PdfRubberStampAnnotation(new RectangleF(new PointF(0, 0), new SizeF(60, 60)));
    PdfAppearance loApprearance = new PdfAppearance(loStamp);
    PdfImage image = PdfImage.FromFile("Stamp.jpg");
 
   PdfTemplate template = new PdfTemplate(160, 160);
    template.Graphics.DrawImage(image, 0, 0);
    loApprearance.Normal = template;
    loStamp.Appearance = loApprearance;
               
    page.AnnotationsWidget.Add(loStamp);

    string output = "ImageStamp.pdf";
    doc.SaveToFile(output);

        }
    }
}