Stamp a PDF document in C# .NET
To stamp a PDF document in the form of a PDF annotation, follow the steps below:
- Create a
GdPicturePDF
object. - Load the PDF file with the
LoadFromFile
method. - Set the origin of the coordinate system with the
SetOrigin
method. This method requires thePDFOrigin
enumeration. - Set the measurement unit with the
SetMeasurementUnit
method to specify the annotation’s dimensions and position. This method uses thePdfMeasurementUnit
enumeration. - Select the PDF page where you want to place the stamp annotation using the
SelectPage
method. - 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 thePdfRubberStampAnnotationIcon
enumeration that specifies the appearance of the stamp.- The annotation icon’s color expressed with the
Red
,Green
, andBlue
parameters represented in RGB values (from0
to255
).
- 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, "Nutrient", "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, "Nutrient", "Approved", PdfRubberStampAnnotationIcon.Approved, 0.75F, 173, 216, 230) gdpicturePDF.SaveToFile("C:\temp\output.pdf")End Using
Used methods
Related topics