Remove PDF annotations in C# .NET
To remove an annotation in a PDF document, follow these steps:
- Create a
GdPicturePDFobject. - Load a PDF file with the
LoadFromFilemethod. - Select the PDF page where the searched annotation is located.
- Loop through all the annotations on the selected page. Use the
GetAnnotationCountmethod to get the total number of annotations on the selected page. - Determine the searched annotation by any of its parameters, such as its title, type, or subject. Use the methods starting with the
GetAnnotationprefix. The full list is available in the get properties guide. - Remove the annotation with the
RemoveAnnotationmethod. It requires the annotation index as its parameter. - Save the PDF document to a file with the
SaveToFilemethod.
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 UsingRelated topics