如何在PDF BOX中创建按钮?

Nir*_*tel 7 java pdf-generation pdfbox

我想在PDFBOX中创建一个按钮,即验证或重置按钮,该按钮将调用PDF中嵌入式javascript的某些功能.

如何在PDFBOX中创建这样的按钮?

我尝试使用PDPushButton片段跟随代码,但它现在正常工作.在这里,当我点击按钮区域时,会显示勾选标记符号并在每次点击时切换.边框也没有显示出来.相反,我想显示标签和边框周围的普通按钮.

我使用的是pdfbox版本1.8.10.

PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);            

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

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

COSDictionary cosDict = new COSDictionary();
            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("My Btn"));
            cosDict.setItem(COSName.V, new COSString("Validate"));
            cosDict.setItem(COSName.DA, new COSString("/Helv 7 Tf 0 g"));

            PDPushButton button = new PDPushButton(acroForm, cosDict);
            button.setValue("Validate Button");

            PDActionJavaScript tfJs = new PDActionJavaScript("validate("+index+");");
            PDAnnotationAdditionalActions tfAction = new PDAnnotationAdditionalActions();
            tfAction.setU(tfJs);
            button.getWidget().setActions(tfAction);

            PDGamma colourBlack = new PDGamma();
            PDAppearanceCharacteristicsDictionary fieldAppearance = 
                    new PDAppearanceCharacteristicsDictionary(cosDict);
            fieldAppearance.setBorderColour(colourBlack);
            button.getWidget().setAppearanceCharacteristics(fieldAppearance);

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

            acroForm.getFields().add(button);
Run Code Online (Sandbox Code Playgroud)

Nir*_*tel 5

下面的代码生成带边框的正确按钮,并且还可以正确调用javascript函数.唯一的问题是在按钮上设置验证标签.如果有人有解决方案,请提供您的反馈.

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

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

        PDPushButton button = new PDPushButton(acroForm, cosDict);
        button.setValue("Validate Button");

        PDActionJavaScript tfJs = new PDActionJavaScript("validate("+1+");");
        PDAnnotationAdditionalActions tfAction = new PDAnnotationAdditionalActions();
        tfAction.setU(tfJs);
        button.getWidget().setActions(tfAction);

        PDGamma colourBlack = new PDGamma();
        PDAppearanceCharacteristicsDictionary fieldAppearance = 
                new PDAppearanceCharacteristicsDictionary(new COSDictionary());
        fieldAppearance.setBorderColour(colourBlack);
        button.getWidget().setAppearanceCharacteristics(fieldAppearance);

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

        acroForm.getFields().add(button);

         doc.save("/path");
         doc.close();
Run Code Online (Sandbox Code Playgroud)