Links are saved as part of the annotations in a PDF document even though they are not really considered annotations.
Links in a PDF document do not specifically have a destination. Instead, they are more general, they hold a list of actions that can really be anything.
We define actions that we support in the com.qoppa.android.pdfViewer.actions package. You can get the list of actions for an annotation by calling:
annot.getActions();
This returns a List object that will hold objects that extend
com.qoppa.android.pdfViewer.actions.Action
Most inks will normally have a single action in the list and it will be one of these type:
- GotoPageAction – Go to a page within the same document
- GotoPageRemoteAction – Go to a page in another document
- URLAction – Open a web browser to a URL
Each of these classes has methods to get to the relevant data. i.e. the URLAction class has a method called getURL() that returns the target URL.
The java code sample below shows how to loop through the annotations in a PDF document and print out the URL for all links found.
import com.qoppa.pdf.annotations.Annotation;
import com.qoppa.pdf.annotations.Link;
import com.qoppa.pdfProcess.PDFDocument;
import com.qoppa.pdfProcess.PDFPage;
import com.qoppa.pdfViewer.actions.Action;
import com.qoppa.pdfViewer.actions.URLAction;
…
// Load the document
PDFDocument pdfDoc = new PDFDocument (“input.pdf”, null);
// Loop through pages
for(int count = 0; count < pdfDoc.getPageCount(); count++)
{
PDFPage page = pdfDoc.getPage(count);
if(page != null)
{
// Get list of annotations
Vector annots = page.getAnnotations();
// Write the annotations
if(annots != null)
{
for (int aCount = 0; aCount < annots.size(); ++aCount )
{
Annotation annot = (Annotation) annots.get (aCount);
if(annot != null)
{
if((annot instanceof Link) == false)
{
java.util.List actions = ((Link) annot).getActions();
if(actions != null)
{
for (int j = 0; count < actions.size(); ++j)
{
Action action = (Action)actions.get (j);
if (action instanceof URLAction)
{
String url = ((URLAction)action).getURL();
System.out.println(url);
}
}
}
}
}
}
}
}
}