Delete Annotations (facades)

Delete All Annotations from an Existing PDF File

PdfAnnotationEditor allows you delete all the annotations from the existing PDF file. First off, create a PdfAnnotationEditor object and bind input PDF file using BindPdf method. After that, call DeleteAnnotations method to delete all the annotations from the file, and then use Save method to save the updated PDF file. The following code snippet shows you how to delete all the annotations from the PDF file.

public static void DeleteAllAnnotations()
{
    // Open document
    PdfAnnotationEditor annotationEditor = new PdfAnnotationEditor();
    annotationEditor.BindPdf(_dataDir + "sample_cats_dogs.pdf");
    // Delete all annoations
    annotationEditor.DeleteAnnotations();
    // Save updated PDF
    annotationEditor.Save(_dataDir + "DeleteAllAnnotation.pdf");
}   

Delete All Annotations by Specified Type

You can use PdfAnnotationEditor class to delete all the annotations, by a specified annotation type, from the existing PDF file. In order to do that you need to create a PdfAnnotationEditor object and bind input PDF file using BindPdf method. After that, call DeleteAnnotations method, with the string parameter, to delete all the annotations from the file; the string parameter represents the annotation type to be deleted. Finally, use Save method to save the updated PDF file. The following code snippet shows you how to delete all annotations by specified annotation type.

public static void DeleteAnnotation()
{
    // Open document
    var document = new Document(_dataDir + "sample_cats_dogs.pdf");
    int index;
    for (index = 1; index <= document.Pages[1].Annotations.Count; index++)
    {
        System.Console.WriteLine($"{index}. {document.Pages[1].Annotations[index].Name} {document.Pages[1].Annotations[index].AnnotationType}");
    }
    System.Console.Write("Please enter number:");
    index = int.Parse(System.Console.ReadLine());

    PdfAnnotationEditor annotationEditor = new PdfAnnotationEditor();
    annotationEditor.BindPdf(document);
    annotationEditor.DeleteAnnotation(document.Pages[1].Annotations[index].Name);

    // Save updated PDF
    annotationEditor.Save(_dataDir + "DeleteAnnotation.pdf");
}