How can I add a new AcroForm field to a PDF?

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?

Posted on StackOverflow on Nov 30, 2014 by Peter

This is documented in the official documentation, more specifically in the SubmitForm example. Additionally, I've written 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.

Click this link if you want to see how to answer this question in iText 7.



Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now