Developer Tools
|
Office Productivity Applications
|
Enterprise Solutions
|
|||||||||||||||||||||||||||
In this article, you will learn how to create and edit actions for PDF link annotations. Action elements specify what happens when a user clicks on a link annotation in a PDF file.
There are several types of PDF actions - go-to, remote go-to, URI, JavaScript, launch, and named.
For this article, I will limit myself to go-to and remote go-to actions. A go-to action links to a destination in the same document while a remote go-to action links to a destination in another PDF document.
Our customer had a PDF document containing several link annotations. In particular, some link annotations had remote go-to actions - all of which were set to destinations in another PDF document.
A problem arose when the second PDF document was moved to a different location and all the remote go-to actions of link annotations in the original PDF document needed to be updated with the new path of the second document.
To reproduce the problem, let us create a PDF document (C:\Old_Path\document1.pdf) with two link annotations. A go-to action will be associated with the first link annotation and a remote go-to action will be associated with the second link annotation. The remote go-to action will be linked to a destination in another PDF document (C:\Old_Path\document2.pdf).
public static void createLinkAnnotActions()
throws IOException, PdfException
{
PdfWriter w = PdfWriter.fileWriter("document1.pdf");
PdfDocument d = new PdfDocument(w);
// Creates font objects
PdfFont font_Helvetica = PdfFont.create(
"Helvetica",
15,
PdfEncodings.WINANSI);
font_Helvetica.setColor(Color.blue);
PdfFont font_HelveticaSmall = PdfFont.create(
"Helvetica",
10,
PdfEncodings.WINANSI);
font_HelveticaSmall.setColor(Color.black);
// Creates new page (1)
PdfPage p = new PdfPage(PdfPageSize.A4);
// Writes text on page (1) using the fonts
p.writeText("Create and Modify PDF Annotation Actions",
font_Helvetica,
PdfTextFormatter.LEFT,
50, 50);
p.writeText("Link to page 2 of this document.",
font_HelveticaSmall,
50, 100);
p.writeText("Link to page 3 of remote document.",
font_HelveticaSmall,
50, 120);
// Creates link annotation (1) on text written above
PdfLinkAnnot linkAnnot1 =
new PdfLinkAnnot(new PdfRect(81, 100, 33, 14),
Color.white);
linkAnnot1.setHighlightMode(
PdfLinkAnnot.HIGHLIGHT_MODE_INVERT);
// Creates a go-to action that links to page (2)
PdfGotoAction goto_action = new PdfGotoAction(2, 0, 0, 100);
// Add the go-to action to link annotation (1)
linkAnnot1.addAction(goto_action);
// Creates link annotation (2) on text written above
PdfLinkAnnot linkAnnot2 =
new PdfLinkAnnot(new PdfRect(81, 120, 33, 14),
Color.white);
linkAnnot2.setHighlightMode(
PdfLinkAnnot.HIGHLIGHT_MODE_INVERT);
// Creates a remote go-to action that links to page 3
// in a remote document located in the C: drive
PdfRemoteGotoAction remoteGoto_action =
new PdfRemoteGotoAction(
"C:\\Old_Path\\document2.pdf", 3, true);
// Adds the remote go-to action to link annotation (2)
linkAnnot2.addAction(remoteGoto_action);
// Adds the link annotations to page (1)
p.addAnnotation(linkAnnot1);
p.addAnnotation(linkAnnot2);
// Adds page (1) to document
d.add(p);
// Creates page (2)
p = new PdfPage(PdfPageSize.A4);
// Writes some text on page (2)
p.writeText("Page 2", 50, 50);
// Adds page (2) to the document
d.add(p);
d.setOpenAfterSave(true);
// Writes document to the output file
d.write();
w.dispose();
}
Now, we assume that document2.pdf was moved from C:\Old_Path to D:\New_Path and update remote go-to actions for the link annotations in document1.pdf accordingly.
For this, we first load document1.pdf, retrieve its first page, iterate through all link annotations in that page, get all actions for each link annotation that is found, and update any remote go-to action with the new location of document2.pdf.
public static void manageLinkAnnotActions()
throws IOException, PdfException {
PdfReader r = PdfReader.fileReader("document1.pdf");
r.setOutFilePath("document1_modified.pdf");
PdfDocument d = new PdfDocument(r);
String actualFilePath = "C:\\Old_Path";
String replacementFilePath = "D:\\New_Path";
// Creates a regular expression
String regex = "C:\\\\Old_Path";
// Gets page (1)
PdfPage p = d.getPage(1);
// Gets all annotations of link type and stores it in a list
List annotList = p.getAllAnnotations(PdfAnnot.ANNOT_TYPE_LINK);
// Iterate sthrough the list of link annotations
for (int i = 0, limit = annotList.size(); i < limit; i++) {
PdfLinkAnnot linkAnnot = (PdfLinkAnnot) annotList.get(i);
// Populates a list with remote go-to actions of the
// current link annotation
List al = linkAnnot.getAllActions(PdfAction.REMOTE_GOTO);
// If you call getAllActions() without any arguments
// (this is an overoaded method), you will be able to
// retrieve all actions irrespective of type.
// Iterates through the list of remote go-to actions of
// the current link annotation
for (int j = 0, size = al.size(); j < size; j++)
{
PdfRemoteGotoAction remoteGoToAction
= (PdfRemoteGotoAction) al.get(j);
// Checks remote file pathname of the remote go-to action
// for old pathname
if (remoteGoToAction.getPdfFilePath().startsWith(actualFilePath))
{
// Replaces old pathname with new pathname
remoteGoToAction.setPdfFilePath(
remoteGoToAction.getPdfFilePath().replaceAll(regex,
replacementFilePath));
}
}
}
d.setOpenAfterSave(true);
d.write();
r.dispose(); }
We use the getAllAnnotations() method of the page object to retrieve all link annotations objects in the page. We then call getAllActions() of each annotation and get a list of remote go-to action objects. The getPdfFilePath() is used to retrieve old pathname of document2.pdf. Finally, we use setPdfFilePath() to update the remote go-to action with the new pathname of document2.pdf.
| Privacy | Legal | Feedback | Newsletter | © 2002-2010 Gnostice Information Technologies Private Limited. All rights reserved. |
This site is best viewed on a screen with minimum resolution of 1152 x 864 pixels. Windows users are advised to use Microsoft ClearType Tuning for optimal experience. Linux and other users can enable font smoothing, as supported by their OS. Also, please use the latest version of a standards-compliant browser such as Opera, FireFox, Chrome or Safari.