Extract Annotations from PDF Documents in Java
Leave feedback
On this page
PDF files often contain annotations – comments, notes, highlights, and other markup added by reviewers. With GroupDocs.Parser for Java, you can easily read these annotations programmatically using the getAnnotations method.
This guide shows how to extract annotations from PDF documents in Java step by step.
What Are PDF Annotations?
Annotations are notes, comments, and markup that reviewers add on top of a PDF document’s content – for example, sticky notes, text comments, or highlighted remarks. The getAnnotations method reads this markup and returns it as a collection of AnnotationItem objects, each exposing a getValue() method with the annotation’s text. Annotations without text content are skipped.
Extracts annotations from a specific page (zero-based index)
⚡ Note: Annotation support depends on the document format. Use getFeatures().isAnnotations() to check whether the loaded document supports annotation extraction.
How to Extract PDF Annotations in Java
Follow these steps to get annotations from a PDF document:
Create a Parser object and load the PDF file.
Call the getAnnotations method to retrieve the annotation collection.
Iterate through the collection and read each annotation’s value.
Example: Extract PDF Annotations in Java
// Create an instance of Parser class
try(Parserparser=newParser("MyDocument.pdf")){// Extract annotations
Iterable<AnnotationItem>annotations=parser.getAnnotations();// Check if annotation extraction is supported
if(annotations==null){System.out.println("Annotation extraction isn't supported.");return;}// Display annotations
for(AnnotationItemitem:annotations){System.out.println(item.getValue());}}
Warning
getAnnotations method returns null if annotation extraction isn’t supported for the document. If the PDF document has no annotations, getAnnotations method returns an empty collection.
Example: Extract Annotations from Each Page in Java
// Create an instance of Parser class
try(Parserparser=newParser("MyDocument.pdf")){// Check if the document supports annotation extraction
if(!parser.getFeatures().isAnnotations()){System.out.println("Document isn't supports annotation extraction.");return;}// Get the document info
IDocumentInfodocumentInfo=parser.getDocumentInfo();// Iterate over pages
for(intp=0;p<documentInfo.getPageCount();p++){System.out.println(String.format("Page %d/%d",p+1,documentInfo.getPageCount()));// Extract annotations from the page
for(AnnotationItemitem:parser.getAnnotations(p)){System.out.println(item.getValue());}}}
Additional Annotation Metadata
The AnnotationItem class exposes extra metadata that gives more context about each annotation:
Method
Type
Description
getAnnotationType()
String
The annotation type (for PDF documents, the type of the PDF annotation).
getTimestamp()
java.util.Date
Date and time when the annotation was created or last modified; null if the document doesn’t provide it.
getAuthorName()
String
Name of the author who created or last modified the annotation; null if the document doesn’t provide it.
getAuthorInitials()
String
Initials of the author; null if the document doesn’t provide it (PDF annotations don’t store initials).
Example: Read Annotation Metadata in Java
// Create an instance of Parser class
try(Parserparser=newParser("MyDocument.pdf")){Iterable<AnnotationItem>annotations=parser.getAnnotations();if(annotations==null){System.out.println("Annotation extraction isn't supported.");return;}for(AnnotationItemannotation:annotations){System.out.println("Type: "+annotation.getAnnotationType());System.out.println("Author: "+annotation.getAuthorName());System.out.println("Created/Modified: "+annotation.getTimestamp());System.out.println("Content: "+annotation.getValue());}}
This metadata makes it easier to filter annotations by author or type, or sort them by time. To map annotations to a specific page, use the getAnnotations(int pageIndex) overload as shown above.
Extract PDF Document Text Together with Annotations
Sometimes it’s useful to get the document’s text and its annotations in one pass, instead of calling getAnnotations separately. Call setIncludeAnnotations(true) on TextOptions and pass it to the getText(TextOptions) method – the annotation text will be included in the extracted output alongside the regular document text.
Example: Extract Text with Annotations in Java
// Create an instance of Parser class
try(Parserparser=newParser("MyDocument.pdf")){// Create the options to extract text in accurate mode and include annotations
TextOptionsoptions=newTextOptions(false);options.setIncludeAnnotations(true);// Extract a text into the reader
try(TextReaderreader=parser.getText(options)){// Print a text with annotations
System.out.println(reader==null?"Text extraction isn't supported.":reader.readToEnd());}}
Why Extract PDF Annotations?
Extracting PDF annotations is useful for:
Review workflows – collect reviewer comments and feedback from a document.
Collaboration – surface highlighted or noted sections without opening the PDF.
Auditing – track markup left on a document over time.
More resources
GitHub examples
You may easily run the code above and see the feature in action in our GitHub examples:
Along with full featured .NET library we provide simple, but powerful free Apps.
You are welcome to parse documents and extract data from PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online Free Online Document Parser App.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
On this page
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.