我当前正在尝试将按钮添加到现有的pdf页面,单击该按钮将关闭当前选项卡。我已经使用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?
-更新- 看来,如果在打开文档后保存文档,则可以单击按钮将其关闭,没有问题。也许另一种解决方案是在生成后正确保存它?不确定如何运作。
答案 0 :(得分:2)
删除/ NeedAppearances条目是正确的,但是您还需要在此处创建外观:黑色边框。我还添加了文字。
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);