Remove PDF Annotations in C# .NET

To remove an annotation in a PDF document, 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 number 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. Remove the annotation with the RemoveAnnotation method. It requires the annotation index as its parameter.

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

To remove all free text annotations located on the first page of a PDF document, 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 = annotCount-1; i >= 0 ; i--)
{
    // Check if the current annotation is of the `FreeText` type.
    if (gdpicturePDF.GetAnnotationSubType(i) == "FreeText")
    {
        // Remove the current annotation.
        gdpicturePDF.RemoveAnnotation(i);
    }
}
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 = annotCount - 1 To 0
        ' Check if the current annotation is of the `FreeText` type.
        If gdpicturePDF.GetAnnotationSubType(i) Is "FreeText" Then
            ' Remove the current annotation.
            gdpicturePDF.RemoveAnnotation(i)
        End If
    Next
    gdpicturePDF.SaveToFile("C:\temp\output.pdf")
End Using
Used Methods

Related Topics