PDFBox - 如何单击 PDF 文件中的 link 以移动到另一页并在其中的特定文本下划线?
PDFBox - How to click a link in a PDF file to move to another page and underline specific text inside it?
在 Adobe Acrobat 中,我可以在 PDF 文件中定义一个 link 并设置一个 Java 脚本操作,当它被点击以转到另一个页面并在该页面中的特定单词下划线时,如它出现在这张图片中:
我想使用 Java 的 PDFBox 库做同样的事情。我已经成功地定义了一个 link,但是如何设置 JavaScript 代码让那个 link 移动到另一个页面并在该页面中的特定单词下划线?
这是我当前的代码:
PDAnnotationLink myLink = new PDAnnotationLink();
/*
* Some code here to define the link, then i should define the link action.
*/
PDActionJavaScript javascriptAction = new PDActionJavaScript( "app.alert( \"I should now go to page 10 and undeline a word out there.\" );" );
myLink.setAction( javascriptAction );
annotations.add( myLink );
该操作是一个 GoTo 操作,它以第 2 页对象作为目标。它有一个 "Next" 条目,那个条目有 Javascript 操作。
此代码复制了 Adobe 一直在做的事情:
List<PDAnnotation> annotations = pdfDocument.getPage(0).getAnnotations();
PDAnnotationLink myLink = new PDAnnotationLink();
myLink.setRectangle(new PDRectangle(122.618f, 706.037f, 287.127f-122.618f, 718.255f-706.037f));
myLink.setColor(new PDColor(new float[]{1, 1 / 3f, 0}, PDDeviceRGB.INSTANCE));
PDBorderStyleDictionary bs = new PDBorderStyleDictionary();
bs.setWidth(3);
bs.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE);
myLink.setBorderStyle(bs);
PDPageFitRectangleDestination dest = new PDPageFitRectangleDestination();
dest.setLeft(48);
dest.setBottom(401);
dest.setRight(589);
dest.setTop(744);
dest.setPage(pdfDocument.getPage(1));
PDActionGoTo gotoAction = new PDActionGoTo();
gotoAction.setDestination(dest);
List<PDAction> actionList = new ArrayList<>();
String js = "var annot = this.addAnnot({\n"
+ "page: 1,\n"
+ "type: \"Underline\",\n"
+ "quads: this.getPageNthWordQuads(1, 4),\n"
+ "author: \"Brad Colin\",\n"
+ "contents: \"Fifth word on page 2\"\n"
+ "});";
PDActionJavaScript jsAction = new PDActionJavaScript(js);
actionList.add(jsAction);
gotoAction.setNext(actionList);
myLink.setAction(gotoAction);
annotations.add(myLink);
pdfDocument.save("1-new.pdf");
在 Adobe Acrobat 中,我可以在 PDF 文件中定义一个 link 并设置一个 Java 脚本操作,当它被点击以转到另一个页面并在该页面中的特定单词下划线时,如它出现在这张图片中:
我想使用 Java 的 PDFBox 库做同样的事情。我已经成功地定义了一个 link,但是如何设置 JavaScript 代码让那个 link 移动到另一个页面并在该页面中的特定单词下划线?
这是我当前的代码:
PDAnnotationLink myLink = new PDAnnotationLink();
/*
* Some code here to define the link, then i should define the link action.
*/
PDActionJavaScript javascriptAction = new PDActionJavaScript( "app.alert( \"I should now go to page 10 and undeline a word out there.\" );" );
myLink.setAction( javascriptAction );
annotations.add( myLink );
该操作是一个 GoTo 操作,它以第 2 页对象作为目标。它有一个 "Next" 条目,那个条目有 Javascript 操作。
此代码复制了 Adobe 一直在做的事情:
List<PDAnnotation> annotations = pdfDocument.getPage(0).getAnnotations();
PDAnnotationLink myLink = new PDAnnotationLink();
myLink.setRectangle(new PDRectangle(122.618f, 706.037f, 287.127f-122.618f, 718.255f-706.037f));
myLink.setColor(new PDColor(new float[]{1, 1 / 3f, 0}, PDDeviceRGB.INSTANCE));
PDBorderStyleDictionary bs = new PDBorderStyleDictionary();
bs.setWidth(3);
bs.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE);
myLink.setBorderStyle(bs);
PDPageFitRectangleDestination dest = new PDPageFitRectangleDestination();
dest.setLeft(48);
dest.setBottom(401);
dest.setRight(589);
dest.setTop(744);
dest.setPage(pdfDocument.getPage(1));
PDActionGoTo gotoAction = new PDActionGoTo();
gotoAction.setDestination(dest);
List<PDAction> actionList = new ArrayList<>();
String js = "var annot = this.addAnnot({\n"
+ "page: 1,\n"
+ "type: \"Underline\",\n"
+ "quads: this.getPageNthWordQuads(1, 4),\n"
+ "author: \"Brad Colin\",\n"
+ "contents: \"Fifth word on page 2\"\n"
+ "});";
PDActionJavaScript jsAction = new PDActionJavaScript(js);
actionList.add(jsAction);
gotoAction.setNext(actionList);
myLink.setAction(gotoAction);
annotations.add(myLink);
pdfDocument.save("1-new.pdf");