Mark Stephens I have been working with Java and PDF since 1999 and am a big NetBeans fan. I enjoy speaking at conferences including Business of Software, JavaOne, CodeOne and DevFest. I have an MA in Medieval History and a passion for reading.

Extracting text references from a PDF file

2 min read

JPedal is used for alsorts of extraction tasks. A recent request was from a client who wanted to extract the references and text from a PDF file. Here is what you need to do to achieve this and some sample code if you would like to use our PDF library.

Links are stored as Annotation objects in a PDF file. Every page has a possible list of annotation objects on that page (if there are no annotations are present there will be no value). Annotations objects allow PDf files to contain animations and interactions. There are stored separately from the page text and drawn on by the PDF renderer.

There are several types of Annotation object. The one we are interested in is the Annot values with /Subtype of link. Here is what the raw data might look like.

23 0 obj<<

/F 4


/BS<</W 0>>


/StructParent 1

/Rect[60.72 684 86.88 696]



The values we are interested in are:-

/Subtype value which tells us it is a link (it could be a video, a sound, a form, a popup note or lots of other cool features)

/Rect value (which is the PDF co-ordinates of a rectangle which is the link. If you click on this area, the link will activate

/A value (this is the action value which tells us what to do. In this case we have a URL which we open in a browser

These are easy to extract from the Annot object.

As mentioned earlier, the text is stored separately so we need to decode the page and extract the text from the area of the page. Because of the way PDF works, you cannot be sure what is at any page location unless you parse the whole page.

I have found that there can be a slight mismatch between the Annot rectangle and the exact area the text occupies, so you may want to allow a small margin for error (ie use slightly larger figures).

So, that is how you would extract the references and their text, and here is the documented code example our PDF library.

Would you like some help building something you think would make an interesting blog article?

 * setup JPedal for text extraction
 */PdfDecoder decodePdf = new PdfDecoder(true);

String filename="/Users/markee/Downloads/sampelFile.pdf";

try {
} catch (PdfException e) {

 * main code loop here - scan all pages for Annots and text
 */for(int page=1;page<decodepdf.getpagecount()+1;page++){ get="" annots="" on="" thepage="" pdfarrayiterator="" annotlistforpage="decodePdf.getFormRenderer().getAnnotsOnPage(page);" if="" we="" have="" annots,="" decode="" page="" as="" well="" and="" data="" if(annotlistforpage!="null" &&="" annotlistforpage.gettokencount()="">0){ //can have empty lists

try {
} catch (Exception e1) {

//work through list getting values

//get ID of annot which has already been decoded and get actual object
String annotKey=annotListForPage.getNextValueAsString(true);

Object[] rawObj=decodePdf.getFormRenderer().getCompData().


FormObject annotObj=(FormObject)rawObj[0];

int subtype=annotObj.getParameterConstant(PdfDictionary.Subtype);

//the type of annot we are interested in

System.out.println("\nlink object");
float[] coords=annotObj.getFloatArray(PdfDictionary.Rect);
System.out.println("Rect= "+coords[0]+" "+coords[1]
                                         +" "+coords[2]+" "+coords[3]);

//text in A subobject
PdfObject aData=annotObj.getDictionary(PdfDictionary.A);
if(aData!=null &&
String text=aData.getTextStreamValue(PdfDictionary.URI);
System.out.println("link text="+text);

 * get data at location
 */try {

PdfGroupingAlgorithms currentGrouping = decodePdf.

//we need a small margin of error to ensure we get the text
int x1 = (int)coords[0]-1;
int y1 = (int)coords[1]-1;
int x2 = (int)coords[2]+1;
int y2 = (int)coords[3]+4;

/**The call to extract the text*/String text =currentGrouping.
System.out.println("text at location="+text);

} catch (Exception e) {

/**close the pdf file*/decodePdf.closePdfFile();

This post is part of our “Understanding the PDF File Format” series. In each article, we discuss a PDF feature, bug, gotcha or tip. If you wish to learn more about PDF, we have 13 years worth of PDF knowledge and tips, so click here to visit our series index!

IDRsolutions develop a Java PDF Viewer and SDK, an Adobe forms to HTML5 forms converter, a PDF to HTML5 converter and a Java ImageIO replacement. On the blog our team post anything interesting they learn about.

Mark Stephens I have been working with Java and PDF since 1999 and am a big NetBeans fan. I enjoy speaking at conferences including Business of Software, JavaOne, CodeOne and DevFest. I have an MA in Medieval History and a passion for reading.

Resaving Acrobat Forms in the Browser

With the removal of Acrobat Reader support in Chrome and other browsers, we have seen a big increase in the number of clients asking...
Mark Stephens
1 min read

3 Replies to “Extracting text references from a PDF file”

Leave a Reply

Your email address will not be published. Required fields are marked *

IDRsolutions Ltd 2019. All rights reserved.