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
GdPicturePDFobject. - Load the PDF file with the
LoadFromFilemethod. - Set the origin of the coordinate system with the
SetOriginmethod. This method requires thePDFOriginenumeration. - Set the measurement unit with the
SetMeasurementUnitmethod to specify the annotation’s dimensions and position. This method uses thePdfMeasurementUnitenumeration. - Select the PDF page where you want to place the stamp annotation using the
SelectPagemethod. - Add the stamp annotation using the
AddStampAnnotationmethod. 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 thePdfRubberStampAnnotationIconenumeration that specifies the appearance of the stamp.- The annotation icon’s color expressed with the
Red,Green, andBlueparameters represented in RGB values (from0to255).
- Save the PDF document to a file with the
SaveToFilemethod.
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 UsingUsed methods
Related topics