gpt4 book ai didi

java - PDFBox 避免 关闭前是否要保存更改

转载 作者:行者123 更新时间:2023-11-30 01:50:24 25 4
gpt4 key购买 nike

我目前正在尝试向现有 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?

--更新--看来,如果您在打开文档后保存该文档,则可以单击该按钮将其关闭,没有问题。也许另一种解决方案是在生成它后正确保存它?不确定那会如何工作。

最佳答案

删除/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);

关于java - PDFBox 避免 关闭前是否要保存更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56267694/

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