Skip to main content
Skip table of contents

Adding fields to an existing form

Example to answer Click How can I add a new AcroForm field to a PDF?


addfield

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.acroforms;

import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfButtonFormField;
import com.itextpdf.forms.fields.PdfFormAnnotation;
import com.itextpdf.forms.fields.PdfFormCreator;
import com.itextpdf.forms.fields.PushButtonFormFieldBuilder;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.action.PdfAction;

import java.io.File;

public class AddField {
    public static final String DEST = "./target/sandbox/acroforms/add_field.pdf";

    public static final String SRC = "./src/main/resources/pdfs/form.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new AddField().manipulatePdf(DEST);
    }

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

        PdfButtonFormField button = new PushButtonFormFieldBuilder(pdfDoc, "post")
                .setWidgetRectangle(new Rectangle(36, 700, 36, 30)).setCaption("POST").createPushButton();
        button.getFirstFormAnnotation().setBackgroundColor(ColorConstants.GRAY);
        button.setValue("POST");

        // The second parameter is optional, it declares which fields to include in the submission or which to exclude,
        // depending on the setting of the Include/Exclude flag.
        button.getFirstFormAnnotation().setAction(PdfAction.createSubmitForm("http://itextpdf.com:8180/book/request", null,
                PdfAction.SUBMIT_HTML_FORMAT | PdfAction.SUBMIT_COORDINATES));
        button.getFirstFormAnnotation().setVisibility(PdfFormAnnotation.VISIBLE_BUT_DOES_NOT_PRINT);
        form.addField(button);

        pdfDoc.close();
    }
}

C#

C#
using System;
using System.IO;
using iText.Forms;
using iText.Forms.Fields;
using iText.Kernel.Colors;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Action;

namespace iText.Samples.Sandbox.Acroforms
{
    public class AddField
    {
        public static readonly String DEST = "results/sandbox/acroforms/add_field.pdf";

        public static readonly String SRC = "../../../resources/pdfs/form.pdf";

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new AddField().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            PdfAcroForm form = PdfFormCreator.GetAcroForm(pdfDoc, true);

            PdfButtonFormField button = new PushButtonFormFieldBuilder(pdfDoc, "post")
                .SetWidgetRectangle(new Rectangle(36, 700, 36, 30)).SetCaption("POST").CreatePushButton();
            button.GetFirstFormAnnotation().SetBackgroundColor(ColorConstants.GRAY);
            button.SetValue("POST");

            // The second parameter is optional, it declares which fields to include in the submission or which to exclude,
            // depending on the setting of the Include/Exclude flag.
            button.GetFirstFormAnnotation().SetAction(PdfAction.CreateSubmitForm("http://itextpdf.com:8180/book/request", null,
                PdfAction.SUBMIT_HTML_FORMAT | PdfAction.SUBMIT_COORDINATES));
            button.GetFirstFormAnnotation().SetVisibility(PdfFormAnnotation.VISIBLE_BUT_DOES_NOT_PRINT);
            form.AddField(button);

            pdfDoc.Close();
        }
    }
}



addfieldandkids

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.stamper;

import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.NonTerminalFormFieldBuilder;
import com.itextpdf.forms.fields.PdfFormCreator;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.forms.fields.PdfTextFormField;
import com.itextpdf.forms.fields.TextFormFieldBuilder;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;

import java.io.File;

public class AddFieldAndKids {
    public static final String DEST = "./target/sandbox/stamper/add_field_and_kids.pdf";
    public static final String SRC = "./src/main/resources/pdfs/hello.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new AddFieldAndKids().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));

        PdfFormField personal =
                new NonTerminalFormFieldBuilder(pdfDoc, "personal").createNonTerminalFormField();
        PdfTextFormField name = new TextFormFieldBuilder(pdfDoc, "name")
                .setWidgetRectangle(new Rectangle(36, 760, 108, 30)).createText();
        name.setValue("");
        personal.addKid(name);
        PdfTextFormField password = new TextFormFieldBuilder(pdfDoc, "password")
                .setWidgetRectangle(new Rectangle(150, 760, 300, 30)).createText();
        password.setValue("");
        personal.addKid(password);

        PdfFormCreator.getAcroForm(pdfDoc, true).addField(personal, pdfDoc.getFirstPage());
        pdfDoc.close();
    }
}

C#

CPP
using System;
using System.IO;
using iText.Forms;
using iText.Forms.Fields;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;

namespace iText.Samples.Sandbox.Stamper 
{
    public class AddFieldAndKids {
        public static readonly String DEST = "results/sandbox/stamper/add_field_and_kids.pdf";
        public static readonly String SRC = "../../../resources/pdfs/hello.pdf";

        public static void Main(String[] args) 
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            
            new AddFieldAndKids().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest) 
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            
            PdfFormField personal = new NonTerminalFormFieldBuilder(pdfDoc, "personal")
                .CreateNonTerminalFormField();
            PdfTextFormField name = new TextFormFieldBuilder(pdfDoc, "name")
                .SetWidgetRectangle(new Rectangle(36, 760, 108, 30)).CreateText();
            name.SetValue("");
            personal.AddKid(name);
            PdfTextFormField password = new TextFormFieldBuilder(pdfDoc, "password")
                .SetWidgetRectangle(new Rectangle(150, 760, 300, 30)).CreateText();
            password.SetValue("");
            personal.AddKid(password);
            PdfFormCreator.GetAcroForm(pdfDoc, true).AddField(personal, pdfDoc.GetFirstPage());
            
            pdfDoc.Close();
        }
    }
}


JavaScript errors detected

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

If this problem persists, please contact our support.