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

Defining Custom Appearances for PDF Annotations In .NET

Learn to replace the default appearances of PDF annotations using C# and PDFOne .NET.
By V. Subhash

The PDF format supports several types of annotations. It also specifies the default appearance with which these annotations need to displayed. Most PDF viewer applications follow Adobe Reader's implementation as the guide.

Yet, there are situations where the default appearance of an annotation may not be very flexible or suitable enough for the purpose it is used. Recognizing this, Adobe has provided support for custom appearances for annotations.

A PDF custom appearances is a standalone fragment of visible page PDF content. Once defined in a PDF document, custom appearances can be used to override the default appearance of any number of annotations and form fields.

Regular Appearance for a PDF Annotation

Here is an example of a document where an annotation has been added.

Before we go into adding a custom appearance, let us see how we added this annotation to a PDF. Here is the C# code.

// Create a text annotation 
PDFTextAnnot ta = new PDFTextAnnot();
ta.Rectangle = new RectangleF(5f, 0.5f, 2.5f, 2.5f);
ta.Title = "Comment (1)";
ta.Subject = "Review by V. Subhash";
ta.Content = "Add \"First come, first served\"";
ta.Color = Color.Yellow;
ta.AnnotIcon = PDFTextAnnotIcon.Comment;

// Load a PDF document, add the annotation, and save
// modified document to a new file
PDFDocument doc = new PDFDocument("license-key");
doc.Load("input.pdf");
doc.MeasurementUnit = PDFMeasurementUnit.Inches;
doc.CurrentPageNo = 2;
doc.AddAnnot(ta);
doc.Save("output1.pdf");  // Save with default appearance
doc.Close();

Custom Appearance for a PDF Annotation

To create a custom appearance, you need to create an instance of PDFAppearanceStream class where you will specify how big this fragment is. Then, you use several of its content-creation methods such as DrawImage() or WriteText(). Finally, you set the PDFAppearanceStream to the NormalAppearance property of the PDF annotation object. The C# code example follows.

// Create a text annotation 
PDFTextAnnot ta = new PDFTextAnnot();      
ta.Rectangle = new RectangleF(5f, 0.5f, 2.5f, 2.5f);
ta.Title = "Comment (1)";
ta.Subject = "Review by V. Subhash";
ta.Content = "Add \"First come, first served\"";
ta.Color = Color.Yellow;
ta.AnnotIcon = PDFTextAnnotIcon.Comment;  // Plan B?

// Create a custom appearance with some image and text
PDFAppearanceStream pas = 
     new PDFAppearanceStream(new RectangleF(0,0,2.5f,2.5f),
                             PDFMeasurementUnit.Inches);
pas.DrawImage("note.png");
pas.WriteText("Click here for my review",
              new PDFFont(StdType1Font.Helvetica_Bold, 18),
              new RectangleF(0.5f, 0.5f, 1.5f, 1.5f),
              new SolidBrush(Color.Red),
              new PDFTextFormatter());
      
// Set the custom appearance for the annotation
ta.NormalAppearance = pas;  

// Load a PDF document, add the annotation, and save
// modified document to a new file
PDFDocument doc = new PDFDocument("license-key");
doc.Load("input.pdf");
doc.MeasurementUnit = PDFMeasurementUnit.Inches;
doc.CurrentPageNo = 2;
doc.AddAnnot(ta);
doc.Save("output2.pdf");  // Save with custom appearance
doc.Close();

You can take this further by creating additional but different custom appearances for the RolloverAppearance and DownAppearance properties of the annotation. Now, you are only limited by your imagination in making annotations more attractive and useful. May the force be with you.

---o0O0o---

Our .NET Developer Tools
XtremeDocumentStudio .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
XtremeDocumentStudio 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
XtremeDocumentStudio (for 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-2017 Gnostice Information Technologies Private Limited. All rights reserved.