我想在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);
答案 0 :(得分: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();
答案 1 :(得分:2)
以下是“如何在PDFBox 2.0.0中为PDPushButton
设置标题”的解决方案
要为pushbutton
显示字幕值,您需要致电setNormalCaption("captionStr")
的{{1}}。
以下是带有文本颜色更改的按钮标题值的代码:
PDAppearanceCharacteristicsDictionary
这里:
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
try {
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);
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(50));
rect.add(new COSFloat(775));
rect.add(new COSFloat(100));
rect.add(new COSFloat(750));
cosDict.setItem(COSName.RECT, rect);
cosDict.setItem(COSName.FT, COSName.getPDFName("Btn"));
cosDict.setItem(COSName.TYPE, COSName.ANNOT);
cosDict.setItem(COSName.SUBTYPE, COSName.getPDFName("Widget"));
cosDict.setItem(COSName.T, new COSString("ClickMe"));
//cosDict.setItem(COSName.V, new COSString("Click Me"));
cosDict.setItem(COSName.DA, new COSString("/F0 6 Tf 0 g 1 1 1 rg "));
PDPushButton button = new PDPushButton(acroForm);
PDActionJavaScript tfJs = new PDActionJavaScript("validate("+1+");");
PDAnnotationAdditionalActions tfAction = new PDAnnotationAdditionalActions();
tfAction.setU(tfJs);
button.getWidgets().get(0).setActions(tfAction);
// button.setReadOnly(true);
button.getCOSObject().addAll(cosDict);
acroForm.getFields().add(button);
PDAnnotationWidget widget = button.getWidgets().get(0);
PDAppearanceCharacteristicsDictionary fieldAppearance =
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);
fieldAppearance.setBorderColour(blue);
fieldAppearance.setBackground(blue);
fieldAppearance.setNormalCaption("Click Me");
widget.setAppearanceCharacteristics(fieldAppearance);
page.getAnnotations().add(widget);
File file = new File("/path");
System.out.println("File created");
FileOutputStream fOut = new FileOutputStream(file);
doc.save(fOut);
doc.close();
} catch (IOException e) {
e.printStackTrace();
}
在这里:
cosDict.setItem(COSName.V, new COSString("Click Me"));
不行。
为此,您需要添加以下行:
button.setValue("Click Me");
以上示例是PDFBox2.0的解决方案 在PDFBox 1.8.11中,我认为这也将起作用。 :)