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

##GITHUB:https://github.com/itext/i7js-examples/blob/develop/src/main/java/com/itextpdf/samples/sandbox/acroforms/AddField.java##

##GITHUB:https://github.com/itext/i7ns-samples/blob/develop/itext/itext.samples/itext/samples/sandbox/acroforms/AddField.cs##


addfieldandkids

##GITHUB:https://github.com/itext/i7js-examples/blob/develop/src/main/java/com/itextpdf/samples/sandbox/stamper/AddFieldAndKids.java##

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.