我尝试将包含富文本的pdf表单字段(PDAcroForm.flatten()
)展平。
这样做会丢失格式(粗体,斜体,颜色,大小)。
String inputFileName = "test.pdf";
String val = "<?xml version=\"1.0\"?>"
+ "<body xmlns=\"http://www.w3.org/1999/xhtml\">"
+ "<p style=\"color:#FF0000;font-size:8pt;\">"
+ "<i>Small</i> <b>Red</b> "
+ "</p>"
+ "<p style=\"color:#00FF00;font-size:20pt;\">"
+ "<i>Big</i> <b>Green</b> "
+ "</p>"
+ "</body>";
String valNoFormat = "Small Red\rBig Green\r";
PDDocument pdf_document = PDDocument.load(new File(inputFileName));
PDAcroForm acroForm = pdf_document.getDocumentCatalog().getAcroForm();
PDTextField acroField = (PDTextField)acroForm.getField("example_field_number_one");
acroField.setValue(valNoFormat);
acroField.setRichTextValue(val);
acroForm.setNeedAppearances(true);
pdf_document.save(new File("output01.pdf"));
List<PDField> the_fields = new ArrayList<PDField>();
for (PDField field: pdf_document.getDocumentCatalog().getAcroForm().getFieldTree()) {
the_fields.add(field);
}
System.out.println("Flattening fields: " + Arrays.stream(the_fields.toArray()).map(field -> ((PDField)field).getFullyQualifiedName()).collect(Collectors.joining(", ","[","]")));
acroForm.setNeedAppearances(true);
pdf_document.getDocumentCatalog().getAcroForm().flatten(the_fields, true);
pdf_document.save(new File("output02.pdf"));
通过Adobe Acrobat Pro
10.1.1
通过表单菜单创建了表单元素,并将pdf文件简单保存为test.pdf
。
出于完整性考虑,我将所有内容都上传到了github:
test.pdf
output01.pdf
output02.pdf
问题是,我该如何删除输入字段并使其变平,同时保持内容的样式和字段的自动大小等首选功能?
答案 0 :(得分:0)
也许足以删除可编辑状态:
for (PDField field: pdf_document.getDocumentCatalog().getAcroForm().getFieldTree()) {
field.setReadOnly(true);
}