How to Start Convert PDF Read PDF Edit PDF PDF Report Builder Work with PDF Modules PDF Document PDF Pages Text Image Graph & Path Annotation, Markup & Drawing Redaction Security Digital Signature Forms Watermark Bookmark Link File Attachment File Metadata Printing Work with Other SDKs Barcode read Barcode create OCR Twain

Using C# PDF Annotation Library
How to create, edit, update, remove PDF file annotations and drawings using C#.NET


Sample Codes for C#.NET Users to create, edit pdf file existing annotations, comments on PDF in C#.NET Class. ASP.NET Annotate PDF function is based on C# PDF Annotate SDK.





In this C# tutorial, you learn how to read, remove annotation common information from PDF document using C#

  • Read annotation's information
  • Remove annotations
  • Annotation popup note window's properties
  • Read, update annotation flags

How to read PDF annotation information programmatically using C#

  1. Download XDoc.PDF Annotation C# library
  2. Install C# library to read PDF annotations' information programmatically
  3. Step by Step Tutorial












C# read all annotations information from a PDFDocument


The text and C# source code below will explain how to read, extract annotations' information from PDF using C# code.

  • Create a new PDFDocument object with an existing PDF file with annotations loaded
  • Use PDFAnnotHandler.GetAllAnnotations() to get list of IPDFAnnot objects, which contain all annotation data of the PDF file
  • For each IPDFAnnot object, you will get annotation's page index, artist, subject, and content data



String inputFilePath = Program.RootPath + "\\" + "1_Annots.pdf";

PDFDocument doc = new PDFDocument(inputFilePath);
List<IPDFAnnot> annots = PDFAnnotHandler.GetAllAnnotations(doc);

Console.WriteLine("Number of Annotations: " + annots.Count);
if (annots.Count > 0)
{
    foreach (IPDFAnnot annot in annots)
    {
        Console.WriteLine("Annotation");
        Console.WriteLine("  Page:     " + annot.PageIndex);
        Console.WriteLine("  Aritst:   " + annot.Artist);
        Console.WriteLine("  Subject:  " + annot.Subject);
        Console.WriteLine("  Content:  " + annot.Content);
    }
}




C# retrieve all annotations information from a PDF page


The C# sample source code will show how to read annotations from a PDF page.



String inputFilePath = Program.RootPath + "\\" + "1_Annots.pdf";

PDFDocument doc = new PDFDocument(inputFilePath);
PDFPage page = (PDFPage)doc.GetPage(0);
List<IPDFAnnot> annots = PDFAnnotHandler.GetAllAnnotations(page);




C# delete all annotations from pdf document


The C# sample source code will show how to remove all annotations from PDF.



String inputFilePath = Program.RootPath + "\\" + "1_Annots.pdf";

PDFDocument doc = new PDFDocument(inputFilePath);
//  get all annotations in the 1st page
PDFPage page = (PDFPage)doc.GetPage(0);
List<IPDFAnnot> annots = PDFAnnotHandler.GetAllAnnotations(page);
//  remove all annotations in the 1st page
PDFAnnotHandler.DeleteAnnotation(doc, annots);




C# retrieve annotation popup note window's properties


String inputFilePath = Program.RootPath + "\\" + "Annot_1.pdf";

PDFDocument doc = new PDFDocument(inputFilePath);
List<IPDFAnnot> annots = PDFAnnotHandler.GetAllAnnotations(doc);
foreach (IPDFAnnot annot in annots)
{
    if (annot is IPDFPopupAnnot)
    {
        Console.WriteLine("Annotation has popup window");

        IPDFPopupAnnot obj = (IPDFPopupAnnot)annot;
        Console.WriteLine("Is open in the begin:  " + obj.Popup.IsOpen);
        Console.WriteLine("Popup window boundary: " + obj.Popup.Boundary.ToString());
    }
    else
    {
        Console.WriteLine("Annotation has no popup window");
    }
}




How to read annotation flags from PDF using C#


The C# sample source code will show how to read annotations' flags information from PDF in C#.NET

  • Create a new PDFDocument object with an existing PDF file with annotations loaded
  • Use PDFAnnotHandler.GetAllAnnotations() to get list of IPDFAnnot objects, which contain all annotation data of the PDF file
  • For each IPDFAnnot object, you will get list of annotation's flag data, including: Invisible, IsHidden, IsPrint, NoZoom, NoRotate, NoView, IsReadOnly, IsLocked, IsToggleNoView, IsLockedContents



String inputFilePath = Program.RootPath + "\\" + "Annot_1.pdf";

PDFDocument doc = new PDFDocument(inputFilePath);
List<IPDFAnnot> annots = PDFAnnotHandler.GetAllAnnotations(doc);
foreach (IPDFAnnot annot in annots)
{
    Console.WriteLine("Annotation Flags");
    Console.WriteLine("  Invisible:       " + annot.IsInvisible);
    Console.WriteLine("  Hidden:          " + annot.IsHidden);
    Console.WriteLine("  Print:           " + annot.IsPrint);
    Console.WriteLine("  No Zoom:         " + annot.NoZoom);
    Console.WriteLine("  No Rotate:       " + annot.NoRotate);
    Console.WriteLine("  No View:         " + annot.NoView);
    Console.WriteLine("  Read Only:       " + annot.IsReadOnly);
    Console.WriteLine("  Locked:          " + annot.IsLocked);
    Console.WriteLine("  Toggle No View:  " + annot.IsToggleNoView);
    Console.WriteLine("  Locked Contents: " + annot.IsLockedContents);
}




C# set annotation flags


String inputFilePath = Program.RootPath + "\\" + "2.pdf";
String outputFilePath = Program.RootPath + "\\" + "Annot_1.pdf";

//  create the annotation
PDFAnnotDeleteLine annot = new PDFAnnotDeleteLine();
annot.StartPoint = new PointF(100F, 200F);
annot.EndPoint = new PointF(300F, 400F);

//  set annotation flags
annot.IsInvisible = false;
annot.IsPrint = true;
annot.NoRotate = true;
annot.NoZoom = true;

//  add annotation
PDFAnnotHandler.AddAnnotation(inputFilePath, 1, annot, outputFilePath);