【问题标题】:PDFBox Button execute javascript to close documentPDFBox 按钮执行 javascript 以关闭文档
【发布时间】: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 示例所示)

标签: java pdfbox


【解决方案1】:

OP 自己的答案的 cmets 中讨论的改进答案,它还包括来自 follow-up question 的答案。

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();

【讨论】:

  • 很好的解决方案!希望这对其他人也有帮助!感谢您的帮助!
猜你喜欢
  • 2021-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多