PDFBox中saveIncremental多部件表单失效问题求助
多Widget的PDTextField使用saveIncremental赋值失效问题
我使用PDFBox 2.0.24版本,创建了一个包含两个Widget的PDTextField,在使用saveIncremental设置字段值时无法生效。填充值并保存后,文件中的表单字段直接消失,试过不使用objectsToWrite参数也没用。
创建表单的示例代码
try (FileOutputStream output = new FileOutputStream("src/main/resources/Sample_form.pdf"); PDDocument document = PDDocument.load(new File("src/main/resources/Sample.pdf"))) { PDFont font = PDType1Font.HELVETICA; PDResources resources = new PDResources(); resources.put(COSName.HELV, font); PDAcroForm acroForm = new PDAcroForm(document); document.getDocumentCatalog().setAcroForm(acroForm); acroForm.setDefaultResources(resources); String defaultAppearanceString = "/Helv 0 Tf 0 g"; acroForm.setDefaultAppearance(defaultAppearanceString); PDTextField textBox = new PDTextField(acroForm); textBox.setPartialName("SampleFieldMultiwidget"); defaultAppearanceString = "/Helv 12 Tf 0 0 1 rg"; textBox.setDefaultAppearance(defaultAppearanceString); acroForm.getFields().add(textBox); PDPage page1=document.getPage(0); PDPage page2=document.getPage(1); PDAnnotationWidget widget1 = new PDAnnotationWidget(); PDRectangle rect = new PDRectangle(50, 750, 250, 50); widget1.setRectangle(rect); widget1.setPage(page1); widget1.setParent(textBox); PDAnnotationWidget widget2 = new PDAnnotationWidget(); PDRectangle rect2 = new PDRectangle(200, 650, 100, 50); widget2.setRectangle(rect2); widget2.setPage(page2); widget2.setParent(textBox); List<PDAnnotationWidget> widgets = new ArrayList<>(); widgets.add(widget1); widgets.add(widget2); textBox.setWidgets(widgets); widget1.setPrinted(true); widget2.setPrinted(true); page1.getAnnotations().add(widget1); page2.getAnnotations().add(widget2); document.save(output); } catch (Exception e) { throw new RuntimeException(e); }
填充字段的代码
try (FileOutputStream output = new FileOutputStream("src/main/resources/Sample_form_filled.pdf"); PDDocument document = PDDocument.load(new File("src/main/resources/Sample_form.pdf"))) { PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm(); PDTextField field = (PDTextField)acroForm.getField("SampleFieldMultiwidget"); field.setValue("Sample Value"); field.setReadOnly(true); COSDictionary fieldDictionary = field.getCOSObject(); fieldDictionary.setNeedToBeUpdated(true); COSArray cosArray = (COSArray)field.getCOSObject().getDictionaryObject(COSName.KIDS); cosArray.setNeedToBeUpdated(true); for (int i = 0; i < cosArray.size(); i++) { COSObject cosObject = (COSObject) cosArray.get(i); COSDictionary dictionary = (COSDictionary) cosObject.getDictionaryObject(COSName.AP); dictionary.setNeedToBeUpdated(true); COSStream stream = (COSStream) dictionary.getDictionaryObject(COSName.N); stream.setNeedToBeUpdated(true); COSDictionary dictionary2 = (COSDictionary) cosObject.getDictionaryObject(COSName.PARENT); while (dictionary2 != null) { dictionary2.setNeedToBeUpdated(true); dictionary2 = (COSDictionary) dictionary2.getDictionaryObject(COSName.PARENT); } } Set<COSDictionary> objectsToWrite = new HashSet<>(); for (PDPage pdPage: document.getPages()) { objectsToWrite.add(pdPage.getCOSObject()); objectsToWrite.add(field.getCOSObject()); } //Added - post comment objectsToWrite.add(field.getWidgets().get(0).getAppearance().getCOSObject()); objectsToWrite.add((COSDictionary) field.getWidgets().get(0).getAppearance().getNormalAppearance().getCOSObject()); objectsToWrite.add(field.getWidgets().get(1).getAppearance().getCOSObject()); objectsToWrite.add((COSDictionary) field.getWidgets().get(1).getAppearance().getNormalAppearance().getCOSObject()); document.saveIncremental(output,objectsToWrite); } catch (Exception e) { throw new RuntimeException(e); }
补充信息
补充了示例文件
问题更新
问题已解决,遗漏了设置Widget外观,现在可在所有查看器中正常渲染。谢谢!
内容的提问来源于stack exchange,提问作者Vas K
相关产品推荐
相关产品推荐

