这记录在官方文档,更具体地说,在提交表格例子。使用iText等工具时,应先阅读官方文档;-)
不管怎样,我给你写了一个简单的例子,叫做AddField。它在由定义的特定位置添加一个按钮字段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
's addAnnotation()
方法将字段添加到特定页面。