小编典典

PDFBox按钮执行JavaScript以关闭文档

java

我的用例是在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");

阅读 317

收藏
2020-11-23

共1个答案

小编典典

改进的答案,如OP自己的评论中所述,并且还包括后续问题的答案。

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();
2020-11-23