Manage Annotations in PDF via C#

Managing annotations in PDF document. Use Aspose.PDF for .NET to modify PDF files programmatically

How to Manage Annotations Using .NET Library

In order to add Text Annotation in PDF file, we’ll use Aspose.PDF for .NET API which is a feature-rich, powerful and easy to use document manipulation API for net platform. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.

Package Manager Console

PM > Install-Package Aspose.PDF

Create Annotations in PDF document via C#


You need Aspose.PDF for.NET to try the code in your environment.

  1. Load PDF in an instance of Document class.
  2. Create an annotation that you want to add to the PDF.
  3. Add the annotation to the Page object’s Annotations collection.
  4. Save the PDF file.

PDF Text Annotation - C#

Example: C#


    Document pdfDocument = new Document(dataDir + "AddAnnotation.pdf");
    TextAnnotation textAnnotation = new TextAnnotation(pdfDocument.Pages[1], new Aspose.Pdf.Rectangle(200, 400, 400, 600));
    textAnnotation.Title = "Sample Annotation Title";
    textAnnotation.Subject = "Sample Subject";
    textAnnotation.State = AnnotationState.Accepted;
    textAnnotation.Contents = "Sample contents for the annotation";
    textAnnotation.Open = true;
    textAnnotation.Icon = TextIcon.Key;
    Border border = new Border(textAnnotation);
    border.Width = 5;
    border.Dash = new Dash(1, 1);
    textAnnotation.Border = border;
    textAnnotation.Rect = new Aspose.Pdf.Rectangle(200, 400, 400, 600);
    pdfDocument.Pages[1].Annotations.Add(textAnnotation);
    dataDir = dataDir + "AddAnnotation_out.pdf";
    pdfDocument.Save(dataDir);