Extract Annotations from PDF

Contents
[ ]

ExtractAnnotations method allows you to extract annotations from a PDF file. In order to extract annotations, you need to create PdfAnnotationEditor object and bind PDF file using BindPdf method. After that, you need to create an enumeration of annotation types which you want to extract from PDF file.

You can then use ExtractAnnotations method to extract the annotations to an ArrayList. After that, you can loop through this list and get individual annotations. And finally, save the updated PDF file using Save method of the PdfAnnotationEditor object. The following code snippet shows you how to extract annotations from PDF file.

// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ExtractAnnotation()
{
    // The path to the documents directory
    string dataDir = RunExamples.GetDataDir_AsposePdf();
    // Open document
    using (var document = new Aspose.Pdf.Document(dataDir + "input.pdf"))
    {
        var annotationEditor = new Aspose.Pdf.Facades.PdfAnnotationEditor();
        annotationEditor.BindPdf(document);
        // Extract annotations
        var annotationTypes = new[] { Aspose.Pdf.Annotations.AnnotationType.FreeText, Aspose.Pdf.Annotations.AnnotationType.Text };
        IList<Annotation>? annotations = annotationEditor.ExtractAnnotations(1, 2, annotationTypes);
        foreach (Annotation? annotation in annotations)
        {
            Console.WriteLine(annotation.Contents);
        }
    }
}