Create an annotation in a PDF using C#
To create an annotation in a PDF document, 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 annotation using the
SelectPagemethod. - Optional: Specify the font type. For more information, refer to the Font Type section of the guide on adding text to PDFs.
- Add the annotation with any of the following methods:
- Save the PDF document to a file with the
SaveToFilemethod.
All methods that create a new annotation return an integer value, which is the annotation’s index. Recommended: Save the annotation index to a variable so that you can later reference that annotation.
Register the SDK license before running annotation operations. For setup details, refer to the getting started with .NET SDK guide.
using GdPicture14;
LicenseManager license = new LicenseManager();license.RegisterKEY("");using GdPicturePDF gdpicturePDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"input.pdf");// Set the origin of the coordinate system.gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft);// Set the measurement unit to centimeters.gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter);// Select the first PDF page.gdpicturePDF.SelectPage(1);// Set the text content and determine its size.float fontSize = 16;string fontResName = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelvetica);string text = "Text annotation example.";float textWidth = gdpicturePDF.GetTextWidth(fontResName, fontSize, text);float textHeight = gdpicturePDF.GetTextHeight(fontResName, fontSize, false);// Add the free text annotation.int annotIndex = gdpicturePDF.AddFreeTextAnnotation(1, 2, textWidth + 0.5f, textHeight + 0.5f, true, "Annotation Example", "Example", text, fontResName, fontSize, 0, 0, 200, 100, 100, 100, 1f);gdpicturePDF.SaveToFile(@"output.pdf");Register the SDK license before running annotation operations. For setup details, refer to the getting started with .NET SDK guide.
Imports GdPicture14
Dim license As LicenseManager = New LicenseManager()license.RegisterKEY("")Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("input.pdf") ' Set the origin of the coordinate system. gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft) ' Set the measurement unit to centimeters. gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter) ' Select the first PDF page. gdpicturePDF.SelectPage(1) ' Set the text content and determine its size. Dim fontSize As Single = 16 Dim fontResName As String = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelvetica) Dim text = "Text annotation example." Dim textWidth As Single = gdpicturePDF.GetTextWidth(fontResName, fontSize, text) Dim textHeight As Single = gdpicturePDF.GetTextHeight(fontResName, fontSize, False) ' Add the free text annotation. Dim annotIndex As Integer = gdpicturePDF.AddFreeTextAnnotation(1, 2, textWidth + 0.5F, textHeight + 0.5F, True, "Annotation Example", "Example", text, fontResName, fontSize, 0, 0, 200, 100, 100, 100, 1F) gdpicturePDF.SaveToFile("output.pdf")End UsingUsed methods
Related topics