Stamp a PDF Document in C# .NET

To stamp a PDF document in the form of a PDF annotation, follow these steps:

  1. Create a GdPicturePDF object.

  2. Load the PDF file with the LoadFromFile method.

  3. Set the origin of the coordinate system with the SetOrigin method. This method requires the PDFOrigin enumeration.

  4. Set the measurement unit with the SetMeasurementUnit method to specify the annotation’s dimensions and position. This method uses the PdfMeasurementUnit enumeration.

  5. Select the PDF page where you want to place the stamp annotation using the SelectPage method.

  6. Add the stamp annotation using the AddStampAnnotation method. This method uses the following parameters:

    • Left — The X coordinate of the top-left corner.

    • Top — The Y coordinate of the top-left corner.

    • Width — The width of the stamp annotation.

    • Height — The height of the stamp annotation.

    • Title — The title of the newly added stamp annotation object. By convention, it represents the author.

    • Contents — The text displayed in the annotation’s note.

    • StampStyle — A member of the PdfRubberStampAnnotationIcon enumeration that specifies the appearance of the stamp.

    • The annotation icon’s color expressed with the Red, Green, and Blue parameters represented in RGB values (from 0 to 255).

  7. Save the PDF document to a file with the SaveToFile method.

To add an “approve” stamp annotation to the last page of a PDF, use the following code:

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Set the origin of the coordinate system to the bottom-left corner.
gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginBottomLeft);
// Set the measurement unit to centimeters.
gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter);
// Select the page where to attach the file.
gdpicturePDF.SelectPage(gdpicturePDF.GetPageCount());
// Add an annotation with the image file.
gdpicturePDF.AddStampAnnotation(Left: 5, Top: 5, Width: 5, Height: 2,
    "PSPDFKit", "Approved", PdfRubberStampAnnotationIcon.Approved,
    0.75f, 173, 216, 230);
gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF()
    gdpicturePDF.LoadFromFile("C:\temp\source.pdf")
    ' Set the origin of the coordinate system to the bottom-left corner.
    gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginBottomLeft)
    ' Set the measurement unit to centimeters.
    gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter)
    ' Select the page to attach the file to.
    gdpicturePDF.SelectPage(gdpicturePDF.GetPageCount())
    ' Add an annotation with the image file.
    gdpicturePDF.AddStampAnnotation(Left:=5, Top:=5, Width:=5, Height:=2,
        "PSPDFKit", "Approved", PdfRubberStampAnnotationIcon.Approved,
        0.75F, 173, 216, 230)
    gdpicturePDF.SaveToFile("C:\temp\output.pdf")
End Using
Used Methods

Related Topics