我的用例是在 pdf 页面上有一个像这样的按钮(实际上是将它们添加到现有页面,但现在我只想看到它对任何东西都有效)。
----------
- Back -
----------
它所做的只是关闭当前的 pdf 页面。这个想法是打开多个选项卡,每个选项卡都是一个 pdf,然后当您点击“后退”按钮时,它会关闭当前的 pdf,然后将焦点返回到上一个 pdf。这是我迄今为止一直在尝试使用的。
// Create a new empty document
try {
PDDocument document = new PDDocument();
// Create a new blank page and add it to the document
PDPage blankPage = new PDPage();
document.addPage( blankPage );
PDBorderStyleDictionary borderULine = new PDBorderStyleDictionary();
borderULine.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE);
PDColor green = new PDColor(new float[] { 0, 1, 0 }, PDDeviceRGB.INSTANCE);
// PDAnnotationTextMarkup txtMark = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT);
// textWidth = (font.getStringWidth("Click Here") / 1000) * 18;
PDAnnotationLink txtLink = new PDAnnotationLink();
txtLink.setBorderStyle(borderULine);
// add an action
// PDActionURI action = new PDActionURI();
// action.setURI("www.google.com");
PDActionJavaScript action = new PDActionJavaScript("this.closeDoc()");
txtLink.setAction(action);
txtLink.setContents("HI");
txtLink.setColor(green);
PDRectangle position = new PDRectangle();
position.setLowerLeftX(10);
position.setLowerLeftY(20);
position.setUpperRightX(100);
position.setUpperRightY(40);
txtLink.setRectangle(position);
txtLink.setInvisible(false);
blankPage.getAnnotations().add(txtLink);
// Save the newly created document
document.save("C:\\Users\\jsmith\\Desktop\\demo\\BlankPage.pdf");
document.close();
} catch (IOException e) {
e.printStackTrace();
}
我似乎看不到 pdf 页面上的任何内容(它只是全白色),我确实得到了以下代码,至少能够转到新页面而不是 javascript,但它仍然不可见。我只是能够将鼠标悬停在左下角,并注意到我可以单击一个链接。
PDActionURI action = new PDActionURI();
action.setURI("www.google.com");
OP 自己的答案的评论中讨论了改进的答案,它还包括来自后续问题 https://stackoverflow.com/questions/56267694/.
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
COSDictionary acroFormDict = new COSDictionary();
PDAcroForm acroForm = new PDAcroForm(doc, acroFormDict);
doc.getDocumentCatalog().setAcroForm(acroForm);
acroForm.setFields(new ArrayList<>());
PDPushButton button = new PDPushButton(acroForm);
button.setPartialName("Btn1");
PDActionJavaScript actionJavaScript = new PDActionJavaScript("this.closeDoc();");
PDAnnotationAdditionalActions additionalActions = new PDAnnotationAdditionalActions();
additionalActions.setU(actionJavaScript);
// widget
PDAnnotationWidget widget = button.getWidgets().get(0);
widget.setActions(additionalActions);
widget.setRectangle(new PDRectangle(100, 700, 100, 50));
PDColor colourBlack = new PDColor(new float[] { 0, 0, 0 }, PDDeviceRGB.INSTANCE);
PDAppearanceCharacteristicsDictionary fieldAppearance
= new PDAppearanceCharacteristicsDictionary(new COSDictionary());
fieldAppearance.setBorderColour(colourBlack);
widget.setAppearanceCharacteristics(fieldAppearance);
// Create appearance
PDAppearanceDictionary appearanceDictionary = new PDAppearanceDictionary();
PDAppearanceStream appearanceStream = new PDAppearanceStream(doc);
appearanceStream.setResources(new PDResources());
try (PDPageContentStream cs = new PDPageContentStream(doc, appearanceStream))
{
PDRectangle bbox = new PDRectangle(widget.getRectangle().getWidth(), widget.getRectangle().getHeight());
appearanceStream.setBBox(bbox);
cs.setNonStrokingColor(0, 0, 0); // black
cs.addRect(bbox.getLowerLeftX() + 0.5f, bbox.getLowerLeftY() + 0.5f, bbox.getWidth() - 1, bbox.getHeight() - 1);
cs.stroke();
// put some useful text
cs.setFont(PDType1Font.HELVETICA, 20);
cs.beginText();
cs.newLineAtOffset(20, 20);
cs.showText("Close");
cs.endText();
}
appearanceDictionary.setNormalAppearance(appearanceStream);
widget.setAppearance(appearanceDictionary);
page.getAnnotations().add(widget);
acroForm.getFields().add(button);
doc.save("..../Button.pdf");
doc.close();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)