Edit PDF Annotations in C#

To edit an annotation, use one of the methods starting with the SetAnnotation prefix. All these methods require the annotation index as their parameter. Any additional parameters depend on the method you want to use. These methods can change the following areas of an annotation, among others:

  • Appearance

  • Content

  • Function properties

To edit an annotation, follow these steps:

  1. Create a GdPicturePDF object.

  2. Load a PDF file with the LoadFromFile method.

  3. Select the PDF page where the searched annotation is located.

  4. Loop through all the annotations on the selected page. Use the GetAnnotationCount method to get the total amount of annotations on the selected page.

  5. Determine the searched annotation by any of its parameters, such as its title, type, or subject. Use the methods starting with the GetAnnotation prefix. The full list is available in the get properties guide.

  6. Modify the annotation with a method starting with the SetAnnotation prefix.

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

Information

If you want to edit an annotation you just created, save the return value of the method used to create the annotation, which is the annotation index.

To change the content of a free text annotation, use the following code:

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Select the PDF page.
gdpicturePDF.SelectPage(1);
// Get the annotation count.
int annotCount = gdpicturePDF.GetAnnotationCount();
// Loop through all annotations.
for (int i = 0; i < annotCount; i++)
{
    // Check if the current annotation is of the `FreeText` type.
    if (gdpicturePDF.GetAnnotationSubType(i) == "FreeText")
    {
        // Change the annotation content.
        gdpicturePDF.SetAnnotationContents(i, "New content");
    }
}
gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF()
    gdpicturePDF.LoadFromFile("C:\temp\source.pdf")
    ' Select the PDF page.
    gdpicturePDF.SelectPage(1)
    ' Get the annotation count.
    Dim annotCount As Integer = gdpicturePDF.GetAnnotationCount()
    ' Loop through all annotations.
    For i = 0 To annotCount - 1
        ' Check if the current annotation is of the `FreeText` type.
        If gdpicturePDF.GetAnnotationSubType(i) Is "FreeText" Then
            ' Change the annotation content.
            gdpicturePDF.SetAnnotationContents(i, "New content")
        End If
    Next
    gdpicturePDF.SaveToFile("C:\temp\output.pdf")
End Using
Used Methods

Related Topics