gpt4 book ai didi

java - 如何在 PDF BOX 中创建按钮?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:22:29 28 4
gpt4 key购买 nike

我想在 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);

最佳答案

以下代码生成了带边框的正确按钮,并正确调用了 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();

关于java - 如何在 PDF BOX 中创建按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32250278/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com