How to Add Annotations and Forms to PDF in .NET

How to Add Annotations and Forms to PDF in .NET

How to Add Annotations and Forms in .NET

This guide shows how to add annotations and interactive form fields to PDF documents using Aspose.PDF FOSS for .NET.


Prerequisites

RequirementDetail
Runtime.NET 8.0 or later
Packagedotnet add package Aspose.Pdf.Foss
LicenseMIT — no license key required

Step 1: Add a text annotation

Text annotations appear as sticky notes on the page. Supply a bounding rectangle, the note content, and an author title. Set open: true to display the note expanded when the document opens.

using var doc = Document.Open(File.ReadAllBytes("input.pdf"));
var page = doc.Pages[1];

page.Annotations.AddTextAnnotation(
    new Rectangle(72, 720, 200, 740),
    contents: "Review this section",
    title: "Editor",
    open: true);

doc.Save("annotated.pdf");

Step 2: Add a link annotation

Link annotations make a rectangular area clickable. Combine a Rectangle with a PdfAction.CreateUri call to navigate the user to an external URL when clicked.

var action = PdfAction.CreateUri("https://aspose.com");
page.Annotations.AddLinkAnnotation(
    new Rectangle(50, 700, 200, 720), action);

Step 3: Add highlight and shape annotations

Highlight annotations mark text regions with a translucent overlay. Shape annotations such as squares and circles draw geometric outlines on the page. Both accept color arrays and optional line-width parameters.

page.Annotations.AddHighlightAnnotation(
    new Rectangle(72, 680, 300, 700),
    quadPoints: null,
    color: new double[] { 1, 1, 0 });

page.Annotations.AddSquareAnnotation(
    new Rectangle(72, 640, 200, 680),
    borderColor: new double[] { 0, 0, 1 },
    fillColor: null,
    lineWidth: 1.5);

Step 4: Read form field values

AcroForm fields are accessed through Document.Form. Iterate Form.Fields to read each field’s name and current value without modifying the document.

using var doc = Document.Open(pdfBytes);
foreach (var field in doc.Form.Fields)
{
    Console.WriteLine($"{field.FullName}: {field.Value}");
}

Step 5: Flatten annotations

foreach (var annot in doc.Pages[1].Annotations)
{
    annot.Flatten();
}
doc.Save("flattened.pdf");

Key Classes

ClassPurpose
AnnotationCollectionAdd and manage page annotations
LinkAnnotationHyperlink annotation
TextAnnotationSticky-note annotation
FormAcroForm field access
FieldBase class for form fields
PdfAction.CreateUriCreate URI link action
AnnotationSelectorFilter annotations by type

Troubleshooting

ProblemSolution
Annotation not visibleCheck that rectangle coordinates are within the page bounds
Link URI is nullThe link may use a GoTo action — check ActionType
Form field value emptyThe field may not have a /V entry — verify field name

See Also