Skip to main content
Skip table of contents

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, 36, 30). Here is the code adapted for iText 7:

JAVA
protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
        PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);

        PdfButtonFormField button = PdfFormField.createPushButton(pdfDoc, new Rectangle(36, 700, 36, 30), "post", "POST");
        button.setBackgroundColor(ColorConstants.GRAY);
        button.setValue("POST");
        button.setAction(PdfAction.createSubmitForm("http://itextpdf.com:8180/book/request", null,
                PdfAction.SUBMIT_HTML_FORMAT | PdfAction.SUBMIT_COORDINATES));
        button.setVisibility(PdfFormField.VISIBLE_BUT_DOES_NOT_PRINT);

        form.addField(button);

        pdfDoc.close();
    }

As you can see, you need to create a PdfButtonFormField object and then use a PdfAcroForm instance to add the field.

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

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.