- 我有多个 PDF,使用 acroforms 和 pdfbox 填充了多个记录(a.pdf、b.pdf、c[0-9].pdf、d[0-9].pdf、ez.pdf)。里>
- 生成的文件(aflat.pdf、bflat.pdf、c[0-9]flat.pdf、d[0-9]flat.pdf、ezflat.pdf)应具有其形式(字典和 adobe 使用的任何内容)已删除,但填充的字段作为原始文本保存在 pdf 上(setReadOnly 不是我想要的!)。
PdfStamper只能删除字段而不保存其内容,但我发现了一些对 PdfContentByte 的引用作为保存内容的方法。唉,文档太简短,无法理解我应该如何执行此操作。
作为最后的手段,我可以使用 FieldPosition直接在 PDF 上书写。有没有人遇到过这样的问题?怎么解决?
更新:保存单页 b.pdf 会生成有效的 bfilled.pdf,但会生成空白的 bflattened.pdf。保存整个文档解决了问题。
populateB();
try (PDDocument doc = new PDDocument(); FileOutputStream stream = new FileOutputStream("bfilled.pdf")) {
//importing the page will corrupt the fields
/*wrong approach*/doc.importPage((PDPage)pdfDocuments.get(0).getDocumentCatalog().getAllPages().get(0));
/*wrong approach*/doc.save(stream);
//save the whole document instead
pdfDocuments.get(0).save(stream);//<---right approach
}
try (FileOutputStream stream = new FileOutputStream("bflattened.pdf")) {
PdfStamper stamper = new PdfStamper(new PdfReader("bfilled.pdf"), stream);
stamper.setFormFlattening(true);
stamper.close();
}
最佳答案
使用PdfStamper.setFormFlattening(true)
删除字段并将它们写入内容。
关于java - 使用 iText 将包含表单的 PDF 转换为仅包含文本的 PDF(保留数据),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28693690/