当前位置: 首页 > 面试题库 >

使用PDFBox 2.x在PDF上放一个按钮

狄赞
2023-03-14
问题内容

我希望有人可以帮助我解决用PdfBox 2.x创建的PDF上的“按钮和文本字段问题”。

我试图在页面上放置一个按钮,该按钮使用Javascript函数在文本字段中设置日期。很好

然后,我尝试将Textfield和Button放在一个多页的文档中,以便Textfield和Button出现在每一页上,但是那样,页面上的Button只将Date写入Date到单击按钮所在的页面。

从那以后,我收到一个问题,第一页上的按钮对第一页上的文本字段做出了反应,但是第一页是按钮做出反应的唯一页面。

然后,我保存了4个文档,每个文档一页,每个文档工作正常。但是,当最后我将4个文档合并为一个4页的文档时,我收到了与以前相同的问题。

有人可以告诉我,这是什么问题?

谢谢托马斯

这是我的Java代码:

import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSFloat;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.cos.COSString;
import org.apache.pdfbox.io.MemoryUsageSetting;
import org.apache.pdfbox.multipdf.PDFMergerUtility;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.color.PDColor;
import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
import org.apache.pdfbox.pdmodel.interactive.action.PDActionJavaScript;
import org.apache.pdfbox.pdmodel.interactive.action.PDAnnotationAdditionalActions;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceCharacteristicsDictionary;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDPushButton;
import org.apache.pdfbox.pdmodel.interactive.form.PDTextField;

public class ScriptButton {

public static void main(String[] args) throws IOException {

    List<PDDocument> aDocList = new ArrayList<PDDocument>();

    String destall = ".\\DS216J\\home\\01Privat\\Script_Button_all.pdf";
    DecimalFormat DFMM = new DecimalFormat("00");

    for (int i = 0; i < 4; i++) {

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

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

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

        PDAnnotationAdditionalActions buttonAction1 = null;
        PDActionJavaScript javascript = null;
        PDActionJavaScript tfJs = null;

        String iStr = DFMM.format(i);
        String dest = ".\\DS216J\\home\\01Privat\\Script_Button_" + iStr
                + ".pdf";

        PDFont font = PDType1Font.HELVETICA;
        PDResources resources = new PDResources();
        resources.put(COSName.getPDFName("Helvetica"), font);
        acroForm.setDefaultResources(resources);

        PDAppearanceStream pdAppearanceStream = new PDAppearanceStream(doc);
        pdAppearanceStream.setResources(resources);

        PDTextField textField = new PDTextField(acroForm);
        textField.setPartialName("SampleField-" + iStr);

        String defaultAppearance = "/Helv 24 Tf 0 0 0 rg";
        textField.setDefaultAppearance(defaultAppearance);

        textField.setMultiline(true);
        textField.setValue("Click to get Date");

        acroForm.getFields().add(textField);

        PDAnnotationWidget fieldwidget = textField.getWidgets().get(0);
        PDRectangle rect = new PDRectangle(50, 600, 300, 70);
        fieldwidget.setRectangle(rect);
        fieldwidget.setPage(page);

        PDAppearanceCharacteristicsDictionary fieldAppearance = new PDAppearanceCharacteristicsDictionary(
                new COSDictionary());
        fieldAppearance.setBorderColour(new PDColor(
                new float[] { 0, 0, 0 }, PDDeviceRGB.INSTANCE));
        fieldAppearance.setBackground(new PDColor(new float[] { 1, 1, 1 },
                PDDeviceRGB.INSTANCE));
        fieldwidget.setAppearanceCharacteristics(fieldAppearance);

        fieldwidget.setPrinted(true);

        page.getAnnotations().add(fieldwidget);

        COSDictionary cosDict1 = new COSDictionary();
        COSArray buttonRect1 = new COSArray();
        buttonRect1.add(new COSFloat(50));
        buttonRect1.add(new COSFloat(575));
        buttonRect1.add(new COSFloat(150));
        buttonRect1.add(new COSFloat(550));

        cosDict1.setItem(COSName.RECT, buttonRect1);
        cosDict1.setItem(COSName.FT, COSName.getPDFName("Btn")); // Field
                                                                    // Type
        cosDict1.setItem(COSName.TYPE, COSName.ANNOT);
        cosDict1.setItem(COSName.SUBTYPE, COSName.getPDFName("Widget"));
        cosDict1.setItem(COSName.T, new COSString("Datum anzeigen"));
        cosDict1.setItem(COSName.DA,
                new COSString("/F0 6 Tf 0 g 1 1 1 rg "));

        PDPushButton button1 = new PDPushButton(acroForm);
        javascript = new PDActionJavaScript("function date" + iStr
                + "() {var fld" + iStr + " = this.getField('SampleField-"
                + iStr + "'); fld" + iStr
                + ".value = util.printd('dd mmmm yyyy',new Date());}");

        doc.getDocumentCatalog().setOpenAction(javascript);

        tfJs = new PDActionJavaScript("date" + iStr + "();");
        buttonAction1 = new PDAnnotationAdditionalActions();

        buttonAction1.setU(tfJs);
        button1.getWidgets().get(0).setActions(buttonAction1);

        button1.getCOSObject().addAll(cosDict1);
        acroForm.getFields().add(button1);

        PDAnnotationWidget buttonWidget1 = button1.getWidgets().get(0);

        PDAppearanceCharacteristicsDictionary buttonFieldAppearance = new PDAppearanceCharacteristicsDictionary(
                new COSDictionary());
        COSArray borderColorArray = new COSArray();
        borderColorArray.add(new COSFloat((float) (141f / 255f)));
        borderColorArray.add(new COSFloat((float) (179f / 255f)));
        borderColorArray.add(new COSFloat((float) (226f / 255f)));
        PDColor blue = new PDColor(borderColorArray, PDDeviceRGB.INSTANCE);
        buttonFieldAppearance.setBorderColour(blue);
        buttonFieldAppearance.setBackground(blue);
        buttonFieldAppearance.setNormalCaption("Felder löschen");

        buttonWidget1.setAppearanceCharacteristics(buttonFieldAppearance);
        page.getAnnotations().add(buttonWidget1);

        File file = new File(dest);
        file.getParentFile().mkdirs();

        doc.save(dest);
        doc.close();

        aDocList.add(doc);
    }

    PDDocument aDocWithallPages = new PDDocument();
    PDFMergerUtility PDFmerger = new PDFMergerUtility();

    PDFmerger.setDestinationFileName(destall);

    int i = 0;
    for (Iterator<PDDocument> iterator = aDocList.iterator(); iterator
            .hasNext();) {
        iterator.next();

        String iStr = DFMM.format(i);
        File newFile = new File(".\\DS216J\\home\\01Privat\\Script_Button_"
                + iStr + ".pdf");
        PDFmerger.addSource(newFile);

        i = i + 1;
    }

    PDFmerger.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());

    aDocWithallPages.close();

}

}


问题答案:

第二种解决方案(合并)将无法工作,因为PDFBox无法更改JS代码。我尝试重新创建的第一个解决方案(您未显示),恕我直言,一个问题是OpenAction中只有一个date函数。您需要JavaScript名称树中的每个函数(您甚至可以不用在字段中全部输入就可以工作,但我没有进行测试):

public static void main(String[] args) throws IOException
{
    String dest = "SO52807807.pdf";

    Map<String, PDActionJavaScript> map = new HashMap<>();
    DecimalFormat DFMM = new DecimalFormat("00");

    try (PDDocument doc = new PDDocument())
    {
        PDDocumentNameDictionary documentNameDictionary = new PDDocumentNameDictionary(doc.getDocumentCatalog());
        PDJavascriptNameTreeNode javascriptNameTreeNode = new PDJavascriptNameTreeNode();
        documentNameDictionary.setJavascript(javascriptNameTreeNode);

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

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

        for (int i = 0; i < 4; i++)
        {
            PDPage page = new PDPage();
            doc.addPage(page);

            PDAnnotationAdditionalActions buttonAction1 = null;
            PDActionJavaScript javascript = null;
            PDActionJavaScript tfJs = null;

            String iStr = DFMM.format(i);

            PDFont font = PDType1Font.HELVETICA;
            PDResources resources = new PDResources();
            resources.put(COSName.getPDFName("Helv"), font);
            acroForm.setDefaultResources(resources);

            PDAppearanceStream pdAppearanceStream = new PDAppearanceStream(doc);
            pdAppearanceStream.setResources(resources);

            PDTextField textField = new PDTextField(acroForm);
            textField.setPartialName("SampleField-" + iStr);

            String defaultAppearance = "/Helv 24 Tf 0 0 0 rg";
            textField.setDefaultAppearance(defaultAppearance);

            textField.setMultiline(true);

            acroForm.getFields().add(textField);

            PDAnnotationWidget fieldwidget = textField.getWidgets().get(0);
            PDRectangle rect = new PDRectangle(50, 600, 300, 70);
            fieldwidget.setRectangle(rect);
            fieldwidget.setPage(page);

            textField.setValue("Click to get Date");

            PDAppearanceCharacteristicsDictionary fieldAppearance = new PDAppearanceCharacteristicsDictionary(
                    new COSDictionary());
            fieldAppearance.setBorderColour(new PDColor(
                    new float[]
                    {
                        0, 0, 0
                    }, PDDeviceRGB.INSTANCE));
            fieldAppearance.setBackground(new PDColor(new float[]
            {
                1, 1, 1
            },
                    PDDeviceRGB.INSTANCE));
            fieldwidget.setAppearanceCharacteristics(fieldAppearance);

            fieldwidget.setPrinted(true);

            page.getAnnotations().add(fieldwidget);

            COSDictionary cosDict1 = new COSDictionary();
            COSArray buttonRect1 = new COSArray();
            buttonRect1.add(new COSFloat(50));
            buttonRect1.add(new COSFloat(575));
            buttonRect1.add(new COSFloat(150));
            buttonRect1.add(new COSFloat(550));

            cosDict1.setItem(COSName.RECT, buttonRect1);
            cosDict1.setItem(COSName.FT, COSName.getPDFName("Btn")); // Field
            // Type
            cosDict1.setItem(COSName.TYPE, COSName.ANNOT);
            cosDict1.setItem(COSName.SUBTYPE, COSName.getPDFName("Widget"));
            cosDict1.setItem(COSName.T, new COSString("Datum anzeigen"));
            cosDict1.setItem(COSName.DA,
                    new COSString("/F0 6 Tf 0 g 1 1 1 rg "));

            PDPushButton button1 = new PDPushButton(acroForm);
            javascript = new PDActionJavaScript("function date" + iStr
                    + "() {var fld" + iStr + " = this.getField('SampleField-"
                    + iStr + "'); fld" + iStr
                    + ".value = util.printd('dd mmmm yyyy',new Date());}");

            //doc.getDocumentCatalog().setOpenAction(javascript);
            map.put("date" + iStr, javascript);

            tfJs = new PDActionJavaScript("date" + iStr + "();");
            buttonAction1 = new PDAnnotationAdditionalActions();

            buttonAction1.setU(tfJs);
            button1.getWidgets().get(0).setActions(buttonAction1);

            button1.getCOSObject().addAll(cosDict1);
            acroForm.getFields().add(button1);

            PDAnnotationWidget buttonWidget1 = button1.getWidgets().get(0);

            PDAppearanceCharacteristicsDictionary buttonFieldAppearance = new PDAppearanceCharacteristicsDictionary(
                    new COSDictionary());
            COSArray borderColorArray = new COSArray();
            borderColorArray.add(new COSFloat((float) (141f / 255f)));
            borderColorArray.add(new COSFloat((float) (179f / 255f)));
            borderColorArray.add(new COSFloat((float) (226f / 255f)));
            PDColor blue = new PDColor(borderColorArray, PDDeviceRGB.INSTANCE);
            buttonFieldAppearance.setBorderColour(blue);
            buttonFieldAppearance.setBackground(blue);
            buttonFieldAppearance.setNormalCaption("Felder löschen");

            buttonWidget1.setAppearanceCharacteristics(buttonFieldAppearance);
            page.getAnnotations().add(buttonWidget1);

        }
        javascriptNameTreeNode.setNames(map);
        doc.getDocumentCatalog().setNames(documentNameDictionary);
        File file = new File(dest);
        file.getParentFile().mkdirs();
        doc.save(dest);
    }
}

更新20.10.2018:我对代码进行了两项更改:1)中的字体名称defaultAppearance必须与默认资源(“
Helv”)中的字体名称相同。2)必须在分配小部件之后而不是之前设置字段的值。(如果您考虑一下,这是有道理的-
小部件是关于视觉的。)如果不这样做,可能会在与非Adobe查看器一起显示时带来麻烦。

更新:25.5.2019:恕我直言,代码有点笨拙,因为不必设置大多数字典元素。在[此答案中](http://codingdict.com/questions/146749可以找到创建按钮的更好版本。



 类似资料:
  • 我已经设法使用Apache的Javafx和PDFbox将文本放入pdf文件中。现在,我正在尝试将拍摄场景屏幕截图的图像放入pdf文档中。 当我使用InputStream和OutputStream类时,会创建文档,但当我打开文档时,Adobe和其他程序会给我一个错误,说Acrobat无法正确显示页面。当我使用上面代码中注释掉的BuffereImage类时,文档很简单,没有创建,我不知道为什么。 笔记

  • 问题内容: 如何将图像放在特定位置的现有PDF文件上。pdf表示一张图纸页。图像将被缩放。我正在检查ReportLab,但找不到答案。谢谢。 问题答案: http://pybrary.net/pyPdf/: 我觉得就像,请参阅手册以获取更好的主意

  • 问题内容: 众所周知,我是一名完全的Java新手。我已经尝试过对此进行研究(通过阅读StackOverflow上的其他文章,谷歌搜索,并问一个不太喜欢Java新手的朋友),但我不知道。答案可能是明确而容易的,但我对此视而不见。我正在尝试从其他框架中放置A。 我的应用程序应该按以下方式工作: 按下框架X时有一个按钮:产生框架Y 框架Y有一个按钮,当按下时:产生框架Z 按下框架Z时有一个按钮:在放置框

  • 我刚刚接触Java,正在为我的大学班级做一个项目。我正在开发一款百万富翁游戏,但是我被卡住了。 我有一个JFrame类,其中我有2个面板。第一个由按钮组成,第二个是我想通过按下按钮来更改的面板。按钮具有自己的类及其构造函数,面板也是如此,因为它们具有不同的布局。我需要在按钮类中创建一个方法,以从框架中删除第二个面板并添加第三个面板(在另一个更JPanel类中描述)。因此,从技术上讲,我需要从按钮类

  • 我已经在我的Mac上安装了Postgres93。我可以打开应用程序,并通过应用程序打开一个与psql的命令行界面“打开psql”。 但是,当我键入时,不返回任何东西。安装路径为。如何使显示正确的结果? Mac OS X-小牛 PostgreSQL包,我不太确定。我到这里下载了它-http://postgresapp.com/

  • 我试图找到用pdfbox版本2签署pdf的例子。x、 在bouncycastle中,我看到的只是pdfbox版本1.8.9 https://github.com/mkl-public/testarea-pdfbox1/blob/master/src/main/java/mkl/testarea/pdfbox1/sign/CreateSignature.java 这适用于pdfbox 1.8.9,但