How to Add Annotation to PDF in WPF

An annotation is a note or comment added to a pdf file, it can be an explanation or a reference to the specific part of the original data. Add annotations can make the file becomes easier to understand for readers.

Spire.PDF enables us to add, edit and delete annotation from a PDF document. This section will demonstrate how to add text annotation and edit annotation in pdf from WPF Applications using Spire.PDF for WPF.

This is the effective screenshot after adding and editing annotation:

Add Annotation to PDF in WPF

After editing, the text content of the annotation changed from “Demo” to “This is an annotation”.

At first, use the following namespace:

using System.Drawing;
using System.Windows;
using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;

Then refer to the detail steps below:

Step 1: Create a new PDF document and add a new page to it.

PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();

Step 2: Draw text in the page and set text font, font size, color and location.

PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 15);
string text = "Spire.PDF";
PdfSolidBrush brush = new PdfSolidBrush(Color.DeepSkyBlue);
PointF point = new PointF(50, 100);
page.Canvas.DrawString(text, font, brush, point);

Step 3: Add annotation.

First, create a text annotation using the PdfTextMarkupAnnotation class:

PdfTextMarkupAnnotation annotation = new PdfTextMarkupAnnotation("Author", "Demo", text, new PointF(0, 0), font);

There are four parameters of the annotation:

“Author”: the author of annotation.
“Demo”: text content of annotation.
text: the text which will be marked.
new PointF(0, 0): not supported at present.
font: the font of the text which will be marked.

Second, Set Border, TextMarkupColor and location of the annotation:

annotation.Border = new PdfAnnotationBorder(0.50f);
annotation.TextMarkupColor = Color.LemonChiffon;
annotation.Location = new PointF(point.X + doc.PageSettings.Margins.Left, point.Y + doc.PageSettings.Margins.Left);

Third, add the annotation to the specified page:

(page as PdfNewPage).Annotations.Add(annotation);

If you need to edit the text of the annotation, then you can use following code:

(page as PdfNewPage).Annotations[0].Text = "This is an annotation";

Step 4: Save and launch the file.

doc.SaveToFile("Annotation.pdf");
System.Diagnostics.Process.Start("Annotation.pdf");

Full codes:

private void button1_Click(object sender, RoutedEventArgs e)
{
    PdfDocument doc = new PdfDocument();
    PdfPageBase page = doc.Pages.Add();
    PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 15);
    string text = "Spire.PDF";
    PdfSolidBrush brush = new PdfSolidBrush(Color.DeepSkyBlue);
    PointF point = new PointF(50, 100);
    page.Canvas.DrawString(text, font, brush, point);
    
    //Add annotation
    PdfTextMarkupAnnotation annotation = new PdfTextMarkupAnnotation("Author", "Demo", text, new PointF(0, 0), font);
    annotation.Border = new PdfAnnotationBorder(0.50f);
    annotation.TextMarkupColor = Color.LemonChiffon;

    annotation.Location = new PointF(point.X + doc.PageSettings.Margins.Left, point.Y + doc.PageSettings.Margins.Left);
(page as PdfNewPage).Annotations.Add(annotation);

    //Edit the text of the annotation
    (page as PdfNewPage).Annotations[0].Text = "This is an annotation";

    doc.SaveToFile("Annotation.pdf");
System.Diagnostics.Process.Start("Annotation.pdf");
}