【发布时间】:2019-05-17 19:35:51
【问题描述】:
我的用例是在 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");
【问题讨论】:
-
您的代码有效。可以看到左下角有一条绿线。在其上方单击将关闭 PDF。您的错误可能是认为内容会出现在链接注释中——不。你需要在那里输出一些普通的文本。 (如您最有可能使用的 AddAnnotations.java 示例所示)