Add new AcroForm field to a PDF

时光总嘲笑我的痴心妄想 提交于 2019-12-20 05:30:06

问题


I have used iText to fill data into existing AcroForm fields in a PDF.

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?


回答1:


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 ;-)

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();
}

}

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.



来源:https://stackoverflow.com/questions/27206327/add-new-acroform-field-to-a-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!