问题描述
我使用 iText 将数据填充到 PDF 中现有的 AcroForm 字段中.
I have used iText to fill data into existing AcroForm fields in a PDF.
我现在正在寻找一种将新的 AcroForm 字段添加到 PDF 的解决方案.iText 这可能吗?如果是这样,我该怎么做?
I am now looking for a solution to add new AcroForm fields to a PDF. Is this possible with iText? If so, how can I do this?
推荐答案
这在官方文档中有记载,更具体地说,在 SubmitForm 示例中.使用 iText 等工具时,应先阅读官方文档 ;-)
This is documented in the official documentation, more specifically in the SubmitForm example. When using a tool such as iText, you should read the official documentation first ;-)
无论如何,我已经为您编写了一个名为 AddField 的简单示例.它在 new Rectangle(36, 700, 72, 730)
定义的特定位置添加一个按钮字段.
Anyway, I've written you a simple example called AddField. It adds a button field at a specific position defined by new Rectangle(36, 700, 72, 730)
.
public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
PushbuttonField button = new PushbuttonField(
stamper.getWriter(), new Rectangle(36, 700, 72, 730), "post");
button.setText("POST");
button.setBackgroundColor(new GrayColor(0.7f));
button.setVisibility(PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT);
PdfFormField submit = button.getField();
submit.setAction(PdfAction.createSubmitForm(
"http://itextpdf.com:8180/book/request", null,
PdfAction.SUBMIT_HTML_FORMAT | PdfAction.SUBMIT_COORDINATES));
stamper.addAnnotation(submit, 1);
stamper.close();
}
}
如您所见,您需要创建一个 PdfFormField
对象(使用辅助类,例如 PushbuttonField
、TextField
、...)然后使用 PdfStamper
的 addAnnotation()
方法将字段添加到特定页面.
As you can see, you need to create a PdfFormField
object (using helper classes such as PushbuttonField
, TextField
,...) and then use PdfStamper
's addAnnotation()
method to add the field to a specific page.
这篇关于将新的 AcroForm 字段添加到 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!