How to Start Tutorials Troubleshooting Main Operations Convert PDF Read PDF Edit PDF PDF Report Generator 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, edit annotations, comments information from PDF document using C#

  • Read annotations, comments and stamps' information
  • Remove annotations
  • Annotation popup note window's properties
  • Read, update annotation flags
  • Export/import comments data to/from standard FDF, XFDF files
  • Make comments and stamps uneditable by flattening

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);




How to export/import annotation data from/to PDF file using C#?


You can easily use C# code to export annotation data from an existing PDF file to a FDF or XFDF file, or import an existing FDF or XFDF file on a PDF file. FDF and XFDF file formats are both supported by Adobe Acrobat software.

Besize export the whole PDF document annotation data, you can also export annotation data from a single PDF page using C#.



Export an existing PDF file annoation data to a FDF file in C#

String inputFilePath = @"C:\1.pdf";
String outputFilePath = @"C:\1.xfdf";

PDFDocument.ExportXFDFDocument(inputFilePath, outputFilePath);


Export PDF in stream object to XFDF in stream object in C#

String inputFilePath = @"C:\1.pdf";
String outputFilePath = @"C:\1.xfdf";

using (FileStream inputStream = File.Open(inputFilePath, FileMode.Open, FileAccess.ReadWrite))
{
    using (FileStream outputStream = File.Open(outputFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
    {
        PDFDocument.ExportXFDFDocument(inputStream, outputStream);
    }
}


Export a specified PDF page's annotation data to XFDF in stream object in C#

String inputFilePath = @"C:\1.pdf";
String outputFilePath = @"C:\1.xfdf";

//  export all annotations in the 1st page
int[] selectedPageIndexes = new int[] { 0 };
using (FileStream inputStream = File.Open(inputFilePath, FileMode.Open, FileAccess.ReadWrite))
{
    using (FileStream outputStream = File.Open(outputFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
    {
        PDFDocument.ExportXFDFDocument(inputStream, selectedPageIndexes, outputStream);
    }
}




How to import annotation data file to PDF file using C#?


You can easily import FDF/XFDF data file (It could be generated by other PDF software) onto an existing PDF file in your C#.NET application.

String inputFilePath = @"C:\2.pdf";
String srcXFDFFilePath = @"C:\1.xfdf";
String outputFilePath = @"C:\Annot_Sample47.pdf";

using (FileStream inputStream = File.Open(inputFilePath, FileMode.Open, FileAccess.ReadWrite))
{
    using (FileStream xfdfStream = File.Open(srcXFDFFilePath, FileMode.Open, FileAccess.ReadWrite))
    {
        using (FileStream outputStream = File.Open(outputFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
        {
            PDFDocument.ImportXFDFDocument(inputStream, xfdfStream, outputStream);
        }
    }
}




How to flatten annotations on PDF using C#?


By flatten annotations on a PDF file, you will make the PDF annotations, comments, and stamps uneditable.

Here are the C# sample code to flattern an existing PDF file using C#.

String inputFilePath = @"C:\Annot_1.pdf";
String outputFilePath = @"C:\Output.pdf";

PDFAnnotHandler.FlattenAnnotations(inputFilePath, outputFilePath);


You can apply flatten functions on selected PDF pages, and specified annotation types only using C# code.

String inputFilePath = @"C:\Annot_1.pdf";
String outputFilePath = @"C:\Output.pdf";

PDFAnnotFlattenOption ops = new PDFAnnotFlattenOption();
//  set a list of page indexes to apply annotation flatten.
//  null for all pages and empty array for none page.
ops.PageIndexes = new int[] { 0 };
//  add type of annotation would be flatten.
ops.Add(PDFAnnotFlattenOption.Type.Stamp);
ops.Add(PDFAnnotFlattenOption.Type.Line);

PDFAnnotHandler.FlattenAnnotations(inputFilePath, outputFilePath, ops);