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

使用PDFBox 2.x在PDF上放一个按钮,第1张

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

第二种解决方案(合并)将无法工作,因为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可以找到创建按钮的更好版本。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5430674.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存