Skip to main content
Skip table of contents

Creating form fields

These examples were written in answer to questions such as:


createjapanesebutton

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.PdfFormCreator;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.forms.fields.PushButtonFormFieldBuilder;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;

import java.io.File;

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

    public static final String FONT = "./src/main/resources/font/FreeSans.ttf";

    // あ き ら characters
    public static final String JAPANESE_TEXT = "\u3042\u304d\u3089";

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

        new CreateJapaneseButton().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H);
        PdfAcroForm form = PdfFormCreator.getAcroForm(pdfDoc, true);

        // Define the position of a button that measures 108 by 26
        Rectangle rect = new Rectangle(36, 780, 108, 26);
        PdfButtonFormField pushButton = new PushButtonFormFieldBuilder(pdfDoc, "japanese")
                .setWidgetRectangle(rect).setCaption(JAPANESE_TEXT)
                .createPushButton();
        pushButton.setFont(font);
        pushButton.setFontSize(12f);
        form.addField(pushButton);

        pdfDoc.close();
    }
}

C#

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

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

        public static readonly String FONT = "../../../resources/font/FreeSans.ttf";

        // あ き ら characters
        public const String JAPANESE_TEXT = "\u3042\u304d\u3089";

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

            new CreateJapaneseButton().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            PdfFont font = PdfFontFactory.CreateFont(FONT, PdfEncodings.IDENTITY_H);
            PdfAcroForm form = PdfFormCreator.GetAcroForm(pdfDoc, true);

            // Define the position of a button that measures 108 by 26
            Rectangle rect = new Rectangle(36, 780, 108, 26);
            PdfButtonFormField pushButton = new PushButtonFormFieldBuilder(pdfDoc, "japanese")
                .SetWidgetRectangle(rect).SetCaption(JAPANESE_TEXT).CreatePushButton();
            pushButton.SetFont(font).SetFontSize(12f);
            form.AddField(pushButton);

            pdfDoc.Close();
        }
    }
}


fileselectionexample

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.fields.PdfFormCreator;
import com.itextpdf.forms.fields.TextFormFieldBuilder;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.action.PdfAction;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.forms.fields.PdfTextFormField;

import java.io.File;

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

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

        new FileSelectionExample().manipulatePdf(DEST);
    }

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

        PdfTextFormField field = new TextFormFieldBuilder(pdfDoc, "myfile")
                .setWidgetRectangle(new Rectangle(36, 788, 523, 18)).createText();
        field.setValue("");

        // If true is passed, then the text entered in the field will represent the pathname of a file
        // whose contents are to be submitted as the value of the field.
        field.setFileSelect(true);

        // When the mouse is released inside the annotation's area (that's what PdfName.U stands for),
        // then the focus will be set on the "mytitle" field.
        field.setAdditionalAction(PdfName.U,
                PdfAction.createJavaScript("this.getField('myfile').browseForFileToSubmit();"
                        + "this.getField('mytitle').setFocus();"));
        form.addField(field);

        PdfTextFormField title = new TextFormFieldBuilder(pdfDoc, "mytitle")
                .setWidgetRectangle(new Rectangle(36, 752, 523, 18)).createText();
        title.setValue("");
        form.addField(title);

        pdfDoc.close();
    }
}

C#

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

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

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

            new FileSelectionExample().ManipulatePdf(DEST);
        }

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

            PdfTextFormField field = new TextFormFieldBuilder(pdfDoc, "myfile")
                .SetWidgetRectangle(new Rectangle(36, 788, 523, 18)).CreateText();
            field.SetValue("");

            // If true is passed, then the text entered in the field will represent the pathname of a file
            // whose contents are to be submitted as the value of the field.
            field.SetFileSelect(true);

            // When the mouse is released inside the annotation's area (that's what PdfName.U stands for),
            // then the focus will be set on the "mytitle" field.
            field.SetAdditionalAction(PdfName.U, PdfAction.CreateJavaScript(
                "this.getField('myfile').browseForFileToSubmit();"
                + "this.getField('mytitle').setFocus();"));
            form.AddField(field);

            PdfTextFormField title = new TextFormFieldBuilder(pdfDoc, "mytitle")
                .SetWidgetRectangle(new Rectangle(36, 752, 523, 18)).CreateText();
            title.SetValue("");
            form.AddField(title);

            pdfDoc.Close();
        }
    }
}

radiogroupmultipage1

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.fields.PdfFormAnnotation;
import com.itextpdf.forms.fields.PdfFormCreator;
import com.itextpdf.forms.fields.RadioFormFieldBuilder;
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.VerticalAlignment;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfButtonFormField;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;

import java.io.File;

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

    public static final String[] LANGUAGES = {"English", "German", "French", "Spanish", "Dutch"};

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

        new RadioGroupMultiPage1().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
        PdfFont font = PdfFontFactory.createFont(StandardFonts.HELVETICA);
        Rectangle rect = new Rectangle(40, 788, 20, 18);
        PdfAcroForm form = PdfFormCreator.getAcroForm(pdfDoc, true);

        String formFieldName = "Language";
        // Radio buttons will be added to this radio group
        PdfButtonFormField radioGroup = new RadioFormFieldBuilder(pdfDoc,formFieldName ).createRadioGroup();
        radioGroup.setValue("");

        for (int page = 1; page <= LANGUAGES.length; page++) {
            pdfDoc.addNewPage();

            // Create a radio button that is added to a radio group.
            PdfFormAnnotation field = new RadioFormFieldBuilder(pdfDoc, formFieldName)
                    .createRadioButton( LANGUAGES[page - 1], rect)
                    .setBorderWidth(1)
                    .setPage(page)
                    .setBorderColor(ColorConstants.BLACK);
            radioGroup.addKid(field);
            // Method specifies on which page the form field's widget must be shown.
            doc.showTextAligned(new Paragraph(LANGUAGES[page - 1]).setFont(font).setFontSize(18),
                    70, 786, page, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
        }

        form.addField(radioGroup);

        doc.close();
    }
}

C#

C#
using System;
using System.IO;
using iText.Forms;
using iText.Forms.Fields;
using iText.IO.Font.Constants;
using iText.Kernel.Colors;
using iText.Kernel.Font;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;

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

        public static readonly String[] LANGUAGES = {"English", "German", "French", "Spanish", "Dutch"};

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

            new RadioGroupMultiPage1().ManipulatePdf(DEST);
        }
        
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);
            PdfFont font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);
            Rectangle rect = new Rectangle(40, 788, 20, 18);
            PdfAcroForm form = PdfFormCreator.GetAcroForm(pdfDoc, true);
            
            // Radio buttons will be added to this radio group
            string formfieldName = "Language";
            RadioFormFieldBuilder builder = new RadioFormFieldBuilder(pdfDoc, formfieldName);
            PdfButtonFormField radioGroup = builder.CreateRadioGroup();
            radioGroup.SetValue("");
            
            for (int page = 1; page <= LANGUAGES.Length; page++)
            {
                pdfDoc.AddNewPage();

                // Create a radio button that is added to a radio group.
                PdfFormAnnotation field = builder
                    .CreateRadioButton(LANGUAGES[page - 1], rect)
                    .SetBorderWidth(1)
                    .SetPage(page)
                    .SetBorderColor(ColorConstants.BLACK);

                radioGroup.AddKid(field);
                
                // Method specifies on which page the form field's widget must be shown.
                doc.ShowTextAligned(new Paragraph(LANGUAGES[page - 1]).SetFont(font).SetFontSize(18),
                    70, 786, page, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
            }

            form.AddField(radioGroup);
            
            doc.Close();
        }
    }
}

radiogroupmultipage2

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.fields.PdfFormAnnotation;
import com.itextpdf.forms.fields.PdfFormCreator;
import com.itextpdf.forms.fields.RadioFormFieldBuilder;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.properties.UnitValue;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfButtonFormField;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.IRenderer;

import java.io.File;

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

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

        new RadioGroupMultiPage2().manipulatePdf(DEST);
    }

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

        // Radio buttons will be added to this radio group
        RadioFormFieldBuilder builder = new RadioFormFieldBuilder(pdfDoc, "answer");
        PdfButtonFormField radioGroup = builder.createRadioGroup();
        radioGroup.setValue("answer 1");

        Table table = new Table(UnitValue.createPercentArray(2)).useAllAvailableWidth();
        for (int i = 0; i < 25; i++) {
            Cell cell = new Cell().add(new Paragraph("Question " + i));
            table.addCell(cell);

            cell = new Cell().add(new Paragraph("Answer " + i));
            table.addCell(cell);
        }

        for (int i = 0; i < 25; i++) {
            Cell cell = new Cell().add(new Paragraph("Radio: " + i));
            table.addCell(cell);

            cell = new Cell();

            // The renderer creates radio button for the current radio group in the current cell
            cell.setNextRenderer(new AddRadioButtonRenderer(cell, radioGroup, "answer " + i));
            table.addCell(cell);
        }

        doc.add(table);

        form.addField(radioGroup);

        pdfDoc.close();
    }


    private class AddRadioButtonRenderer extends CellRenderer {
        protected PdfButtonFormField radioGroup;
        protected String value;

        public AddRadioButtonRenderer(Cell modelElement, PdfButtonFormField radioGroup, String value) {
            super(modelElement);
            this.radioGroup = radioGroup;
            this.value = value;
        }

        // If a renderer overflows on the next area, iText uses #getNextRenderer() method to create a new renderer for the overflow part.
        // If #getNextRenderer() isn't overridden, the default method will be used and thus the default rather than the custom
        // renderer will be created
        @Override
        public IRenderer getNextRenderer() {
            return new AddRadioButtonRenderer((Cell) modelElement, radioGroup, value);
        }

        @Override
        public void draw(DrawContext drawContext) {
            PdfDocument document = drawContext.getDocument();
            PdfAcroForm form = PdfFormCreator.getAcroForm(document, true);

            // Create a radio button that is added to a radio group.
            PdfFormAnnotation field = new RadioFormFieldBuilder(document, null)
                    .createRadioButton( value, getOccupiedAreaBBox());
            radioGroup.addKid(field);
            // This method merges field with its annotation and place it on the given page.
            // This method won't work if the field has no or more than one widget annotations.
            form.addFieldAppearanceToPage(field.getParentField(), document.getPage(getOccupiedArea().getPageNumber()));
        }
    }
}

C#

C#
using System;
using System.IO;
using iText.Forms;
using iText.Forms.Fields;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using iText.Layout.Renderer;

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

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

            new RadioGroupMultiPage2().ManipulatePdf(DEST);
        }

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

            // Radio buttons will be added to this radio group
            RadioFormFieldBuilder builder = new RadioFormFieldBuilder(pdfDoc, "answer");
            PdfButtonFormField radioGroup = builder.CreateRadioGroup();
            radioGroup.SetValue("answer 1");

            Table table = new Table(UnitValue.CreatePercentArray(2)).UseAllAvailableWidth();
            for (int i = 0; i < 25; i++)
            {
                Cell cell = new Cell().Add(new Paragraph("Question " + i));
                table.AddCell(cell);

                cell = new Cell().Add(new Paragraph("Answer " + i));
                table.AddCell(cell);
            }

            for (int i = 0; i < 25; i++)
            {
                Cell cell = new Cell().Add(new Paragraph("Radio: " + i));
                table.AddCell(cell);

                cell = new Cell();

                // The renderer creates radio button for the current radio group in the current cell
                cell.SetNextRenderer(new AddRadioButtonRenderer(cell, radioGroup, "answer " + i));
                table.AddCell(cell);
            }

            doc.Add(table);

            form.AddField(radioGroup);

            pdfDoc.Close();
        }

        private class AddRadioButtonRenderer : CellRenderer
        {
            protected PdfButtonFormField radioGroup;
            protected String value;

            public AddRadioButtonRenderer(Cell modelElement, PdfButtonFormField radioGroup, String value)
                : base(modelElement)
            {
                this.radioGroup = radioGroup;
                this.value = value;
            }            
            
            // If renderer overflows on the next area, iText uses getNextRender() method to create a renderer for the overflow part.
            // If getNextRenderer isn't overriden, the default method will be used and thus a default rather than custom
            // renderer will be created
            public override IRenderer GetNextRenderer()
            {
                return new AddRadioButtonRenderer((Cell) modelElement, radioGroup, value);
            }

            public override void Draw(DrawContext drawContext)
            {
                PdfDocument document = drawContext.GetDocument();
                PdfAcroForm form = PdfFormCreator.GetAcroForm(document, true);

                // Create a radio button that is added to a radio group.
                PdfFormAnnotation field = new RadioFormFieldBuilder(document, null)
                    .CreateRadioButton( value, GetOccupiedAreaBBox());

                radioGroup.AddKid(field);
                // This method merges field with its annotation and place it on the given page.
                // This method won't work if the field has no or more than one widget annotations.
                 form.AddFieldAppearanceToPage(field.GetParentField(), document.GetPage(GetOccupiedArea().GetPageNumber()));
            }
        }
    }
}


genericfields

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.events;

import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormCreator;
import com.itextpdf.forms.fields.PdfTextFormField;
import com.itextpdf.forms.fields.TextFormFieldBuilder;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.IRenderer;
import com.itextpdf.layout.renderer.TextRenderer;

import java.io.File;

public class GenericFields {
    public static final String DEST = "./target/sandbox/events/generic_fields.pdf";

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

        new GenericFields().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);

        Paragraph p = new Paragraph();
        p.add("The Effective Date is ");

        Text day = new Text("     ");
        day.setNextRenderer(new FieldTextRenderer(day, "day"));
        p.add(day);
        p.add(" day of ");

        Text month = new Text("     ");
        month.setNextRenderer(new FieldTextRenderer(month, "month"));
        p.add(month);
        p.add(", ");

        Text year = new Text("            ");
        year.setNextRenderer(new FieldTextRenderer(year, "year"));
        p.add(year);
        p.add(" that this will begin.");

        doc.add(p);

        doc.close();
    }


    private static class FieldTextRenderer extends TextRenderer {
        protected String fieldName;

        public FieldTextRenderer(Text textElement, String fieldName) {
            super(textElement);
            this.fieldName = fieldName;
        }

        // If a renderer overflows on the next area, iText uses #getNextRenderer() method to create a new renderer for the overflow part.
        // If #getNextRenderer() isn't overridden, the default method will be used and thus the default rather than the custom
        // renderer will be created
        @Override
        public IRenderer getNextRenderer() {
            return new FieldTextRenderer((Text) modelElement, fieldName);
        }

        @Override
        public void draw(DrawContext drawContext) {
            PdfTextFormField field = new TextFormFieldBuilder(drawContext.getDocument(), fieldName)
                    .setWidgetRectangle(getOccupiedAreaBBox()).createText();
            PdfFormCreator.getAcroForm(drawContext.getDocument(), true)
                    .addField(field);
        }
    }
}

C#

C#
using System;
using System.IO;
using iText.Forms;
using iText.Forms.Fields;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Renderer;

namespace iText.Samples.Sandbox.Events
{
    public class GenericFields
    {
        public static readonly String DEST = "results/sandbox/events/generic_fields.pdf";

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

            new GenericFields().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);

            Paragraph p = new Paragraph();
            p.Add("The Effective Date is ");

            Text day = new Text("     ");
            day.SetNextRenderer(new FieldTextRenderer(day, "day"));
            p.Add(day);
            p.Add(" day of ");

            Text month = new Text("     ");
            month.SetNextRenderer(new FieldTextRenderer(month, "month"));
            p.Add(month);
            p.Add(", ");

            Text year = new Text("            ");
            year.SetNextRenderer(new FieldTextRenderer(year, "year"));
            p.Add(year);
            p.Add(" that this will begin.");

            doc.Add(p);

            doc.Close();
        }

        private class FieldTextRenderer : TextRenderer
        {
            protected String fieldName;

            public FieldTextRenderer(Text textElement, String fieldName) : base(textElement)
            {
                this.fieldName = fieldName;
            }

            // If renderer overflows on the next area, iText uses getNextRender() method to create a renderer for the overflow part.
            // If getNextRenderer isn't overriden, the default method will be used and thus a default rather than custom
            // renderer will be created
            public override IRenderer GetNextRenderer()
            {
                return new FieldTextRenderer((Text) modelElement, fieldName);
            }

            public override void Draw(DrawContext drawContext)
            {
                PdfTextFormField field = new TextFormFieldBuilder(drawContext.GetDocument(), fieldName)
                    .SetWidgetRectangle(GetOccupiedAreaBBox()).CreateText();
                PdfFormCreator.GetAcroForm(drawContext.GetDocument(), true)
                    .AddField(field);
            }
        }
    }
}
JavaScript errors detected

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

If this problem persists, please contact our support.