Exploring PDF Annotations with Sample Code

Exploring PDF Annotations with Sample Code

Published December 4, 2022

PDF annotations allow the user to add markup information or associate objects to specific locations on a page.  The largest category – Markup annotations – include Text, Circle, Redact and others that add features to the page.  A subcategory of Text Markup annotations cover Highlight, Underline, Squiggly and StrikeOut annotations used to modify or emphasize text content.  A “Sticky note” is a combination usage of Text and Popup annotations. Annotations can be especially helpful when remotely collaborating with others on PDFs.   

Non-markup annotations typically perform actions or add objects to the PDF.  Link annotations can redirect the user to another location within the document or externally to URLs. Stamps can be used to add images while other annotation types can be used to add sound, video, 3D streams or attachments.  

The Adobe PDF Library Java and .NET interfaces provide several sample programs that show how to write code to automatically add annotations to PDF files. Let’s explore some of these samples and how they can help users with their PDF workflows.  

AnnotationCopyPaste 

This sample demonstrates how to copy and paste the annotations from one PDF file to another PDF file. The program provides two optional input PDF documents. The first is the source of the annotations, and the second is the PDF document that receives these copied annotations. The program also provides a default name and path for the output PDF document. The second input document, Layers.pdf, is saved as the output document Paste-out.pdf. 

You can provide your own file names for these values in the code, or you can enter your own file names as command line parameters.  

The need to copy annotations from one document to another can be seen in shared review workflows where more than one person might need to comment on a document. This functionality can be used to solve the frustrations that can come along with manually copying and pasting the notes from one file to another, which is cumbersome and uses up a lot of time and resources. The ability to automate this functionality can save time and money and simplify your workflow. 

InkAnnotations

This sample creates and adds a new Ink annotation to a PDF document. An Ink annotation is a freeform line, like what you would create with a pen, or with a stylus on a mobile device.  

With ink annotations, you specify the vertices, and the PDF viewer will automatically draw smoothened curves linking them. This means that you do not have to record all the movements of a graphic pen on your PDF viewer.  

LinkAnnotation 

This program creates a PDF file with an embedded hyperlink, which takes the reader to the second page of the document. Sometimes you may want to add links into your PDF to direct viewers to a different portion of the file for easy referencing or to external locations such as a URL or another PDF document. Programmatically modifying Link annotations can reduce manual efforts when the destination reference has changed (e.g. if the destination http address has changed). 

 

Did you know you can also extract text from annotations? Check out our blog on extraction code samples to learn more.

 

PolygonAnnotations 

This program generates a PDF output file with a polygon shape (a triangle) as an annotation to the file. 

The program defines the vertices for the outlines of the annotation, and the line and fill colors. Polygon annotations can be used where complex polygons are needed, rather than rectangles, to define the shape and location of the object in a more precise way. 

PolyLineAnnotations 

This program generates a PDF output file with a line shape (an angle) as an annotation to the file. The program defines the vertices for the outlines of the annotation, and the line and fill colors.  A PolyLine is very similar to a Polygon annotation except that the first and last vertex need not be connected. 

Learn More about Annotations

If you want to learn more about PDF annotations, our documentation has more information on using annotations with APDFL.