将新的AcroForm字段添加到PDF [英] Add new AcroForm field to a PDF
问题描述
我使用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 。它在由新矩形(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
, C> 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!