PDFBox按钮执行javascript以关闭文档 [英] PDFBox Button execute javascript to close document

查看:83
本文介绍了PDFBox按钮执行javascript以关闭文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用例是在pdf页面上有一个类似的按钮(确实是将它们添加到现有页面上,但是现在我只想看看它可以在任何东西上工作).

My use case is to have a button like so on a pdf page (really to add them to existing pages but for now I just want to see it work on anything).

----------
-  Back  -
----------

它的作用是关闭当前pdf页面.想法是打开多个选项卡,每个选项卡都是pdf,然后单击后退"按钮将关闭当前pdf,然后将其聚焦到上一个pdf.到目前为止,这是我一直在尝试的方法.

And what it does is just closes the current pdf page. The idea is to have multiple tabs opened and each tab is a pdf and then when you hit the "Back" button it closes the current pdf which will then focus back to the previous pdf. This is what I have been trying to use so far.

        // 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的以下代码,但是它仍然不可见.我只是能够将鼠标悬停在左下方,并注意到我可以单击链接.

And I cant seem to see anything on the pdf page (its just all white), I did get the following code at at least be able to go to a new page instead of the javascript but it was still invisible. I just was able to hover over the bottom left and notice i could click on a link.

            PDActionURI action = new PDActionURI();
            action.setURI("www.google.com");

推荐答案

改进的答案,如OP自己的答案的注释中所讨论,并且还包括来自

Improved answer as discussed in the comments of the OPs own answer, and it also includes the answer from the 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();

这篇关于PDFBox按钮执行javascript以关闭文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆