PDFBox 避免是否要在关闭前保存更改 [英] PDFBox Avoid Do you want to save changes before closing

查看:26
本文介绍了PDFBox 避免是否要在关闭前保存更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试向现有的 pdf 页面添加一个按钮,单击该按钮会关闭当前选项卡.我使用 PDFbox 2.0.15 通过以下代码实现了这一点...

I am currently trying to add a button to an existing pdf page that upon clicking the button it closes the current tab. I have achieved that with the following code using PDFbox 2.0.15...

try {

            InputStream pdfInput = new FileInputStream(new File("C:\\Users\\justi\\Desktop\\test\\real.pdf"));
            PDDocument doc = PDDocument.load(pdfInput);
            PDPage page = doc.getPage(0);
//            PDDocument doc = new PDDocument();
//            PDPage page = new PDPage();
//            doc.addPage(page);

            COSDictionary acroFormDict = new COSDictionary();
            acroFormDict.setBoolean(COSName.getPDFName("NeedAppearances"), true);
            acroFormDict.setItem(COSName.getPDFName("Fields"), new COSArray());

//            PDDocumentCatalog pdCatalog = doc.getDocumentCatalog();
//            PDAcroForm acroForm = pdCatalog.getAcroForm();

            PDAcroForm acroForm = new PDAcroForm(doc, acroFormDict);
            doc.getDocumentCatalog().setAcroForm(acroForm);

            PDActionJavaScript javascript = new PDActionJavaScript("function validate(index){ app.alert(index); }");
            doc.getDocumentCatalog().setOpenAction( javascript );

            COSDictionary cosDict = new COSDictionary();
            PDPushButton button = new PDPushButton(acroForm);
            cosDict = button.getCOSObject();
            COSArray rect = new COSArray();
            rect.add(new COSFloat(100));
            rect.add(new COSFloat(10));
            rect.add(new COSFloat(200));
            rect.add(new COSFloat(60));


            cosDict.setItem(COSName.RECT, rect);
            cosDict.setItem(COSName.FT, COSName.getPDFName("Btn")); // Field Type
            cosDict.setItem(COSName.TYPE, COSName.ANNOT);
            cosDict.setItem(COSName.SUBTYPE, COSName.getPDFName("Widget"));
            cosDict.setItem(COSName.T, new COSString("Btn"+1));
            cosDict.setItem(COSName.V, new COSString("Validate"));
            cosDict.setItem(COSName.DA, new COSString("/Helv 7 Tf 0 g"));
            cosDict.setInt(COSName.FF, 65536);

//            button.setValue("Validate Button");

            PDActionJavaScript tfJs = new PDActionJavaScript("this.closeDoc();");
            PDAnnotationAdditionalActions tfAction = new PDAnnotationAdditionalActions();
            tfAction.setU(tfJs);
            button.getWidget().setActions(tfAction);

            PDColor colourBlack = new PDColor(new float[] { 0, 0, 0 }, PDDeviceRGB.INSTANCE);
            PDAppearanceCharacteristicsDictionary fieldAppearance =
                    new PDAppearanceCharacteristicsDictionary(new COSDictionary());
            fieldAppearance.setBorderColour(colourBlack);
            button.getWidget().setAppearanceCharacteristics(fieldAppearance);

            page.getAnnotations().add(button.getWidget());

            acroForm.getFields().add(button);

            doc.save("C:\\Users\\justi\\Desktop\\test\\test2.pdf");
            doc.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

我目前遇到的主要问题是,每次我打开文档并单击按钮时,它都会打开一个提示,提示您要在关闭前保存对 xyz.pdf 的更改吗?环顾四周,我发现设置"NeedAppearances" 为 false 会删除此提示,但不会显示要单击的框.有没有办法让它显示该框,但仍会在没有该提示的情况下退出 pdf?

The main problem I am currently having is that everytime I open the document and click on the button it opens a prompt that says "Do you want to save changes to xyz.pdf before closing? Looking around I found out that setting the "NeedAppearances" to false removes this prompt but then does not show the box to click on. Is there a way to make it show the box but still exit out of the pdf without that prompt?

--更新--看来,如果您在打开文档后保存文档,则可以单击按钮将其关闭,没问题.也许另一种解决方案是在我生成它后正确保存它?不知道这会如何运作.

--Update-- It appears that if you save the document after you open it you can then click the button to close it no problem. Maybe another solution could be to save it correctly after I generate it? Not sure how that would work tho.

推荐答案

删除/NeedAppearances 条目是正确的,但您还需要创建一个外观,这里是:黑色边框.我还添加了一个文本.

Removing the /NeedAppearances entry is correct, but you also need to create an appearance, here: a black border. I've also added a text.

    PDAnnotationWidget widget = button.getWidgets().get(0);
    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);

这篇关于PDFBox 避免是否要在关闭前保存更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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