我使用iText将数据填充到PDF中的现有AcroForm字段中 .
我现在正在寻找一种解决方案,将新的AcroForm字段添加到PDF . iText可以实现吗?如果是这样,我该怎么做?
这在official documentation中有记录,更具体地说是在SubmitForm示例中 . 使用iText等工具时,您应首先阅读官方文档;-)
无论如何,我给你写了一个名为AddField的简单例子 . 它在 new Rectangle(36, 700, 72, 730) 定义的特定位置添加一个按钮字段 .
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() 方法将字段添加到特定页面 .
PdfFormField
PushbuttonField
TextField
PdfStamper
addAnnotation()
1 回答
这在official documentation中有记录,更具体地说是在SubmitForm示例中 . 使用iText等工具时,您应首先阅读官方文档;-)
无论如何,我给你写了一个名为AddField的简单例子 . 它在
new Rectangle(36, 700, 72, 730)
定义的特定位置添加一个按钮字段 .}
如您所见,您需要创建一个
PdfFormField
对象(使用辅助类,如PushbuttonField
,TextField
,...),然后使用PdfStamper
的addAnnotation()
方法将字段添加到特定页面 .