Add Annotation to PDF File in C#

Annotation is an important part of PDF file. Spire.PDF, as a developer friendly .NET PDF component can meet your need of dealing annotations. Using Spire.PDF you can add a new annotation, edit an existing annotation and delete annotations and so on.

In this article, we will introduce you how to add new annotations and edit existing annotations. Spire.PDF provides you a class called PdfTextMarkupAnnotation to represent text annotation. Create text annotation using PdfTextMarkupAnnotation.

[C#]
PdfTextMarkupAnnotation annotation1 = new PdfTextMarkupAnnotation("Administrator", "Demo about Annotation.", text , new PointF(0, 0), font);

Parameters of annotation1:

  • "Administrator": specify the author of text annotation
  • "Demo about Annotation": text of annotation
  • text: font and text used to determine the size of mark shadowing on text
  • new PointF(0, 0): not supported at present
  • font: the font of the text which will be marked

You need to set the location of the mark upon text. Move the mouse on the mark in the result file, annotation will show.

[C#]
annotation1.Location = new PointF(point.X + doc.PageSettings.Margins.Left, point.Y + doc.PageSettings.Margins.Left);

Add annotation1 to the specified page of PDF file.

[C#]
(page as PdfNewPage).Annotations.Add(annotation1);

Edit the text of annotation1.

[C#]
(page as PdfNewPage).Annotations[0].Text = "Annotation Edited:(This is a Demo about Annotation)";

Effect screenshot:

add annotation to pdf

Full code:

[C#]
static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            PdfPageBase page = doc.Pages.Add();
            PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 13);
            string text = "HelloWorld";
            PointF point = new PointF(200, 100);
            page.Canvas.DrawString(text, font, PdfBrushes.CadetBlue, point);

            PdfTextMarkupAnnotation annotation1 = new PdfTextMarkupAnnotation("Administrator", "Demo about Annotation.", text, new PointF(0, 0), font);
            annotation1.Border = new PdfAnnotationBorder(0.75f);
            annotation1.TextMarkupColor = Color.Green;

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

            (page as PdfNewPage).Annotations[0].Text = "Annotation Edited:(Demo about Annotation)";

            doc.SaveToFile("result.pdf");

            System.Diagnostics.Process.Start("result.pdf");
        }