PDFOne .NET
Powerful all-in-one PDF library for .NET
Compatibility
VS 2005/2008/2010/2012/2013

How To Create And Edit PDF Annotations In .NET

Learn to enhance your documents with comments, notes, attachments, etc.
By V. Subhash

Annotations and forms are two popular ways of providing interactivity in PDF documents. In this article, we will focus on annotations. First, we will see how to create annotations and then move on to modifying and deleting them.

Types of Annotations

There are several types of PDF annotations. Some use icons or shapes and display text in a popup window. Some others play multimedia objects. Some even display text directly on the page.

Text Annotations

A text annotation draws attention to itself using an icon on the page. When the end-user clicks the icon, the PDF viewer application opens up a popup window displaying more information.

Here is how you add an annotation to a PDF page.

// Create a new document (automatically adds page 1)
PDFDocument doc1 = new PDFDocument("your-license-key");
doc1.Load("FREE-PDF-READER.pdf");

// Create a text annotation
PDFTextAnnot ta1 = new PDFTextAnnot(
                            0.7f,            // x-coordinate
                            2.6f,            // y-coordinate
                            Color.Orange);   // Color
ta1.Title = "My Review";
ta1.Subject = "1. Date";
ta1.Content = "Please update the date";
ta1.AnnotIcon = PDFTextAnnotIcon.Comment;

// Add annotation to page 1
doc1.AddAnnot(ta1);

// Save document to file
doc1.Save("text_annotation_demo.pdf");
doc1.Close();

To create a text annotation, call the constructor of the PDFTextAnnot() class. You will have to specify the annotation location (known as the annotation rectangle) and some other properties. There is also a choice of icons for the annotation.

An animation showing different icons that can be used for a text annotation.

Stamp Annotations

A stamp annotation allows you to almost literally "rubber-stamp" a page. Several types of stamps are available - from the forbidding "CONFIDENTIAL" to the humble "DRAFT".

// Create a new document (automatically adds page 1)
PDFDocument doc1 = new PDFDocument("your-license-key");
doc1.Load("FREE-PDF-READER.pdf");

// Create a stamp annotation
PDFStampAnnot sa1 = new PDFStampAnnot(new RectangleF(3f, 0.2f, 2, 1), Color.Gold);
sa1.Stamp = PDFAnnotStamps.Approved;
sa1.Content = "Here's is my seal of approval. Take it;-)";
sa1.Title = "V. Subhash";
sa1.Subject = "My review";

// Add annotation to page 1
doc1.AddAnnot(sa1);

// Save document to file
doc1.Save("stamp_annotation_demo.pdf");
doc1.Close();

Link Annotations

The annotation type that is most popular is the humble link annotation. A link annotation is used to add interactivity to a page region. When the user clicks the page region, the viewer applications changes the view to another page in the same document or even in another document.

You can also make an link annotation link to URI. When the end-user clicks the region, the viewer application will make the OS shell to resolve the http: or mailto: address linked by the annotation. In other words, a browser window/tab or a "new message" mail client window will be opened.

// Create a document and add the annotations
PDFDocument doc1 = new PDFDocument("your-license-key");
doc1.WriteText("Link annotation ====>", 1f, 1f);

doc1.Brush.Color = Color.Blue;
doc1.WriteText("www.gnostice.com", 2.3f, 1f);
doc1.Brush.Color = Color.Black;

// Create a link annotation
PDFLinkAnnot la1 = new PDFLinkAnnot(new RectangleF(2.2f, 0.9f, 1.2f, 0.3f));
la1.AddActionURI("http://www.gnostice.com");

// Add annotation to page 1
doc1.AddAnnot(la1);

// Save document to file
doc1.Save("link_annotation_demo.pdf");
doc1.Close();

A link annotation can also be used to make the viewer application execute a JavaScript script.

// Load an existing document
PDFDocument doc1 = new PDFDocument("your-license-key");
doc1.Load("FREE-PDF-READER.pdf");

// Render text that appears in the annotation rectangle
doc1.WriteText("(JS) Link annotation ====>", 1f, 0.5f);
doc1.Brush.Color = Color.Blue;
doc1.WriteText("Show when this doc was created", 2.4f, 0.5f);
doc1.Brush.Color = Color.Black;

// Place a link annotation in the region of the text
PDFLinkAnnot la1 = new PDFLinkAnnot(new RectangleF(2.2f, 0.3f, 2f, 0.5f));
la1.AddActionJavaScript("app.alert(this.info['creationDate'])");
doc1.AddAnnot(la1);

// Save document to file
doc1.Save("link_annotation_demo.pdf");
doc1.Close();

Squiggly Markup Annotation

// Load an existing PDF document
PDFDocument doc1 = new PDFDocument("your-license-key");
doc1.Load("FREE-PDF-READER.pdf");

// Create a squiggly annotation
PDFMarkupAnnot sa1 = new PDFMarkupAnnot();
sa1.MarkupStyle = PDFMarkupStyle.Squiggly;
sa1.Content = "Is the 'G' in 'Gnostice' silent?";
sa1.Title = "My review";
sa1.Subject = "Document review";
sa1.SetQuadPoints(new RectangleF(1.38f,3.35f,0.52f,0.3f));
sa1.FillColor = Color.Red;

// Add annotation to page 1
doc1.AddAnnot(sa1);

// Save document to file
doc1.Save("link_annotation_demo.pdf");
doc1.Close();

Highlight Markup Annotation

// Load an existing PDF document
PDFDocument doc1 = new PDFDocument("your-license-key");
doc1.Load("FREE-PDF-READER.pdf");

// Create a squiggly annotation
PDFMarkupAnnot sa1 = new PDFMarkupAnnot();
sa1.MarkupStyle = PDFMarkupStyle.Highlight;
sa1.Content = "This is important - not the usual blah blah blah.";
sa1.Title = "Instructions";
sa1.Subject = "For end-users";
sa1.FillColor = Color.Lime;
sa1.SetQuadPoints(new RectangleF(1f,3f, 4.1f, 0.25f));

// Add annotation to page 1
doc1.AddAnnot(sa1);

// Save document to file
doc1.Save("link_annotation_demo.pdf");
doc1.Close();

---o0O0o---

Our .NET Developer Tools
Gnostice Document Studio .NET

Multi-format document-processing component suite for .NET developers.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
Gnostice Document Studio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
Gnostice Document Studio Java

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2024 Gnostice Information Technologies Private Limited. All rights reserved.