该操作是一个转到操作,它的目标是page 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");