Skip to main content
Skip table of contents

Create fields in a table

These examples were written in answer to questions such as:


checkboxcell

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.CheckBoxFormFieldBuilder;
import com.itextpdf.forms.fields.PdfFormCreator;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
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.Table;
import com.itextpdf.layout.properties.UnitValue;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.IRenderer;

import java.io.File;

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

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

        new CheckboxCell().manipulatePdf(DEST);
    }

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

        Table table = new Table(UnitValue.createPercentArray(5)).useAllAvailableWidth();
        for (int i = 0; i < 5; i++) {
            Cell cell = new Cell();

            // Custom renderer creates checkbox in the current cell
            cell.setNextRenderer(new CheckboxCellRenderer(cell, "cb" + i));
            cell.setHeight(50);
            table.addCell(cell);
        }

        doc.add(table);

        doc.close();
    }


    private class CheckboxCellRenderer extends CellRenderer {

        // The name of the check box field
        protected String name;

        public CheckboxCellRenderer(Cell modelElement, String name) {
            super(modelElement);
            this.name = name;
        }

        // 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 CheckboxCellRenderer((Cell) modelElement, name);
        }

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

            // Define the coordinates of the middle
            float x = (getOccupiedAreaBBox().getLeft() + getOccupiedAreaBBox().getRight()) / 2;
            float y = (getOccupiedAreaBBox().getTop() + getOccupiedAreaBBox().getBottom()) / 2;

            // Define the position of a check box that measures 20 by 20
            Rectangle rect = new Rectangle(x - 10, y - 10, 20, 20);

            // The 4th parameter is the initial value of checkbox: 'Yes' - checked, 'Off' - unchecked
            // By default, checkbox value type is cross.
            PdfButtonFormField checkBox = new CheckBoxFormFieldBuilder(drawContext.getDocument(), name)
                    .setWidgetRectangle(rect).createCheckBox();
            checkBox.setValue("Yes");
            form.addField(checkBox);
        }
    }
}

C#

C#
using System;
using System.IO;
using iText.Forms;
using iText.Forms.Fields;
using iText.Kernel.Geom;
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 CheckboxCell
    {
        public static readonly String DEST = "results/sandbox/acroforms/checkbox_cell.pdf";
        
        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new CheckboxCell().ManipulatePdf(DEST);
        }
        
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);
            
            Table table = new Table(UnitValue.CreatePercentArray(5)).UseAllAvailableWidth();
            for (int i = 0; i < 5; i++)
            {
                Cell cell = new Cell();
                
                // Custom renderer creates checkbox in the current cell
                cell.SetNextRenderer(new CheckboxCellRenderer(cell, "cb" + i));
                cell.SetHeight(50);
                table.AddCell(cell);
            }

            doc.Add(table);
            
            doc.Close();
        }

        private class CheckboxCellRenderer : CellRenderer
        {
            // The name of the check box field
            protected internal String name;

            public CheckboxCellRenderer(Cell modelElement, String name)
                : base(modelElement)
            {
                this.name = name;
            }            
            
            // 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 CheckboxCellRenderer((Cell) modelElement, name);
            }

            public override void Draw(DrawContext drawContext)
            {
                PdfAcroForm form = PdfFormCreator.GetAcroForm(drawContext.GetDocument(), true);
                
                // Define the coordinates of the middle
                float x = (GetOccupiedAreaBBox().GetLeft() + GetOccupiedAreaBBox().GetRight()) / 2;
                float y = (GetOccupiedAreaBBox().GetTop() + GetOccupiedAreaBBox().GetBottom()) / 2;
                
                // Define the position of a check box that measures 20 by 20
                Rectangle rect = new Rectangle(x - 10, y - 10, 20, 20);
                
                // The 4th parameter is the initial value of checkbox: 'Yes' - checked, 'Off' - unchecked
                // By default, checkbox value type is cross.
                PdfButtonFormField checkBox = new CheckBoxFormFieldBuilder(drawContext.GetDocument(), name)
                    .SetWidgetRectangle(rect).CreateCheckBox();
                checkBox.SetValue("Yes");
                form.AddField(checkBox);
            }
        }
    }
}


checkboxcell2

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.CheckBoxFormFieldBuilder;
import com.itextpdf.forms.fields.PdfFormCreator;
import com.itextpdf.forms.fields.properties.CheckBoxType;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
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.Table;
import com.itextpdf.layout.properties.UnitValue;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.IRenderer;

import java.io.File;

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

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

        new CheckboxCell2().manipulatePdf(DEST);
    }

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

        Table table = new Table(UnitValue.createPercentArray(6)).useAllAvailableWidth();
        for (int i = 0; i < 6; i++) {
            Cell cell = new Cell();

            // Custom renderer creates checkbox in the current cell
            cell.setNextRenderer(new CheckboxCellRenderer(cell, "cb" + i, i));
            cell.setHeight(50);
            table.addCell(cell);
        }

        doc.add(table);
        doc.close();
    }


    private class CheckboxCellRenderer extends CellRenderer {

        // The name of the check box field
        protected String name;
        protected int checkboxTypeIndex;

        public CheckboxCellRenderer(Cell modelElement, String name, int checkboxTypeIndex) {
            super(modelElement);
            this.name = name;
            this.checkboxTypeIndex = checkboxTypeIndex;
        }

        // 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 CheckboxCellRenderer((Cell) modelElement, name, checkboxTypeIndex);
        }

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

            // Define the coordinates of the middle
            float x = (position.getLeft() + position.getRight()) / 2;
            float y = (position.getTop() + position.getBottom()) / 2;

            // Define the position of a check box that measures 20 by 20
            Rectangle rect = new Rectangle(x - 10, y - 10, 20, 20);

            // The 4th parameter is the initial value of checkbox: 'Yes' - checked, 'Off' - unchecked
            // By default, checkbox value type is cross.
            PdfButtonFormField checkBox = new CheckBoxFormFieldBuilder(drawContext.getDocument(), name)
                    .setWidgetRectangle(rect).createCheckBox();
            checkBox.setValue("Yes");
            switch (checkboxTypeIndex) {
                case 0:
                    checkBox.setCheckType(CheckBoxType.CHECK);

                    // Use this method if you changed any field parameters and didn't use setValue
                    checkBox.regenerateField();
                    break;
                case 1:
                    checkBox.setCheckType(CheckBoxType.CIRCLE);
                    checkBox.regenerateField();
                    break;
                case 2:
                    checkBox.setCheckType(CheckBoxType.CROSS);
                    checkBox.regenerateField();
                    break;
                case 3:
                    checkBox.setCheckType(CheckBoxType.DIAMOND);
                    checkBox.regenerateField();
                    break;
                case 4:
                    checkBox.setCheckType(CheckBoxType.SQUARE);
                    checkBox.regenerateField();
                    break;
                case 5:
                    checkBox.setCheckType(CheckBoxType.STAR);
                    checkBox.regenerateField();
                    break;
            }

            form.addField(checkBox);
        }
    }
}

C#

C#
using System;
using System.IO;
using iText.Forms;
using iText.Forms.Fields;
using iText.Forms.Fields.Properties;
using iText.Kernel.Geom;
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 CheckboxCell2
    {
        public static readonly String DEST = "results/sandbox/acroforms/checkbox_cell2.pdf";

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

            new CheckboxCell2().ManipulatePdf(DEST);
        }

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

            Table table = new Table(UnitValue.CreatePercentArray(6)).UseAllAvailableWidth();
            for (int i = 0; i < 6; i++)
            {
                Cell cell = new Cell();

                // Custom renderer creates checkbox in the current cell
                cell.SetNextRenderer(new CheckboxCellRenderer(cell, "cb" + i, i));
                cell.SetHeight(50);
                table.AddCell(cell);
            }

            doc.Add(table);
            doc.Close();
        }

        private class CheckboxCellRenderer : CellRenderer
        {
            // The name of the check box field
            protected String name;
            protected int checkboxTypeIndex;

            public CheckboxCellRenderer(Cell modelElement, String name, int checkboxTypeIndex)
                : base(modelElement)
            {
                this.name = name;
                this.checkboxTypeIndex = checkboxTypeIndex;
            }            
            
            // 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 CheckboxCellRenderer((Cell) modelElement, name, checkboxTypeIndex);
            }

            public override void Draw(DrawContext drawContext)
            {
                Rectangle position = GetOccupiedAreaBBox();
                PdfAcroForm form = PdfFormCreator.GetAcroForm(drawContext.GetDocument(), true);

                // Define the coordinates of the middle
                float x = (position.GetLeft() + position.GetRight()) / 2;
                float y = (position.GetTop() + position.GetBottom()) / 2;

                // Define the position of a check box that measures 20 by 20
                Rectangle rect = new Rectangle(x - 10, y - 10, 20, 20);

                // The 4th parameter is the initial value of checkbox: 'Yes' - checked, 'Off' - unchecked
                // By default, checkbox value type is cross.
                PdfButtonFormField checkBox = new CheckBoxFormFieldBuilder(drawContext.GetDocument(), this.name)
                    .SetWidgetRectangle(rect).CreateCheckBox();
                checkBox.SetValue("Yes");
                switch (checkboxTypeIndex)
                {
                    case 0:
                    {
                        checkBox.SetCheckType(CheckBoxType.CHECK);

                        // Use this method if you changed any field parameters and didn't use setValue
                        checkBox.RegenerateField();
                        break;
                    }

                    case 1:
                    {
                        checkBox.SetCheckType(CheckBoxType.CIRCLE);
                        checkBox.RegenerateField();
                        break;
                    }

                    case 2:
                    {
                        checkBox.SetCheckType(CheckBoxType.CROSS);
                        checkBox.RegenerateField();
                        break;
                    }

                    case 3:
                    {
                        checkBox.SetCheckType(CheckBoxType.DIAMOND);
                        checkBox.RegenerateField();
                        break;
                    }

                    case 4:
                    {
                        checkBox.SetCheckType(CheckBoxType.SQUARE);
                        checkBox.RegenerateField();
                        break;
                    }

                    case 5:
                    {
                        checkBox.SetCheckType(CheckBoxType.STAR);
                        checkBox.RegenerateField();
                        break;
                    }
                }

                form.AddField(checkBox);
            }
        }
    }
}


comboboxitems

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.ChoiceFormFieldBuilder;
import com.itextpdf.forms.fields.PdfChoiceFormField;
import com.itextpdf.forms.fields.PdfFormCreator;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.forms.fields.PdfFormAnnotation;
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.exceptions.PdfException;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
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.properties.HorizontalAlignment;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.UnitValue;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.IRenderer;

import java.io.File;
import java.io.IOException;

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

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

        new ComboBoxItems().manipulatePdf(DEST);
    }

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

        Table table = new Table(UnitValue.createPercentArray(2)).useAllAvailableWidth();
        table.addCell(new Cell().add(new Paragraph("Combobox:")));
        Cell cell = new Cell();

        // Add rows with selectors
        String[] options = {"Choose first option", "Choose second option", "Choose third option"};
        String[] exports = {"option1", "option2", "option3"};

        // The renderer creates combobox in the current cell
        cell.setNextRenderer(new SelectCellRenderer(cell, "Choose first option", exports, options));
        cell.setHeight(20);
        table.addCell(cell);
        doc.add(table);

        doc.close();
    }


    private class SelectCellRenderer extends CellRenderer {
        protected String name;
        protected String[] exports;
        protected String[] options;

        public SelectCellRenderer(Cell modelElement, String name, String[] exports, String[] options) {
            super(modelElement);
            this.name = name;
            this.exports = exports;
            this.options = options;
        }

        // 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 SelectCellRenderer((Cell) modelElement, name, exports, options);
        }

        @Override
        public void draw(DrawContext drawContext) {
            PdfFont font;
            try {
                font = PdfFontFactory.createFont(StandardFonts.HELVETICA);
            } catch (IOException e) {
                throw new PdfException(e);
            }

            String[][] optionsArray = new String[options.length][];
            for (int i = 0; i < options.length; i++) {
                optionsArray[i] = new String[2];
                optionsArray[i][0] = exports[i];
                optionsArray[i][1] = options[i];
            }

            PdfAcroForm form = PdfFormCreator.getAcroForm(drawContext.getDocument(), true);

            // The 3rd parameter is the combobox name, the 4th parameter is the combobox's initial value
            PdfChoiceFormField choice = new ChoiceFormFieldBuilder(drawContext.getDocument(), name)
                    .setWidgetRectangle(getOccupiedAreaBBox()).setOptions(optionsArray).createComboBox();
            choice.setValue(name);
            choice.setFont(font);
            choice.getWidgets().get(0).setBorderStyle(PdfAnnotation.STYLE_BEVELED);
            choice.getFirstFormAnnotation().setVisibility(PdfFormAnnotation.VISIBLE_BUT_DOES_NOT_PRINT);
            choice.getFirstFormAnnotation().setBorderColor(ColorConstants.GRAY);
            choice.setJustification(TextAlignment.CENTER);
            form.addField(choice);
        }
    }
}

C#

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

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

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

            new ComboBoxItems().ManipulatePdf(DEST);
        }

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

            Table table = new Table(UnitValue.CreatePercentArray(2)).UseAllAvailableWidth();
            table.AddCell(new Cell().Add(new Paragraph("Combobox:")));
            Cell cell = new Cell();

            // Add rows with selectors
            String[] options = {"Choose first option", "Choose second option", "Choose third option"};
            String[] exports = {"option1", "option2", "option3"};
            
            // The renderer creates combobox in the current cell
            cell.SetNextRenderer(new SelectCellRenderer(cell, "Choose first option", exports, options));
            cell.SetHeight(20);
            table.AddCell(cell);
            doc.Add(table);

            doc.Close();
        }

        private class SelectCellRenderer : CellRenderer
        {
            protected String name;
            protected String[] exports;
            protected String[] options;

            public SelectCellRenderer(Cell modelElement, String name, String[] exports, String
                [] options)
                : base(modelElement)
            {
                this.name = name;
                this.exports = exports;
                this.options = options;
            }            
            
            // 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 SelectCellRenderer((Cell) modelElement, name, exports, options);
            }

            public override void Draw(DrawContext drawContext)
            {
                PdfFont font;
                try
                {
                    font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);
                }
                catch (IOException e)
                {
                    throw new PdfException(e);
                }

                String[][] optionsArray = new String[options.Length][];
                for (int i = 0; i < options.Length; i++)
                {
                    optionsArray[i] = new String[2];
                    optionsArray[i][0] = exports[i];
                    optionsArray[i][1] = options[i];
                }

                PdfAcroForm form = PdfFormCreator.GetAcroForm(drawContext.GetDocument(), true);

                // The 3rd parameter is the combobox name, the 4th parameter is the combobox's initial value
                PdfChoiceFormField choice = new ChoiceFormFieldBuilder(drawContext.GetDocument(), name)
                    .SetWidgetRectangle(GetOccupiedAreaBBox()).SetOptions(optionsArray).CreateComboBox();
                choice.SetValue(name);
                choice.SetFont(font);
                choice.GetWidgets()[0].SetBorderStyle(PdfAnnotation.STYLE_BEVELED);
                choice.GetFirstFormAnnotation().SetVisibility(PdfFormAnnotation.VISIBLE_BUT_DOES_NOT_PRINT);
                choice.GetFirstFormAnnotation().SetBorderColor(ColorConstants.GRAY);
                choice.SetJustification(TextAlignment.CENTER);
                form.AddField(choice);
            }
        }
    }
}


createformintable

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.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.forms.fields.PdfTextFormField;
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.properties.UnitValue;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.IRenderer;

import java.io.File;

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

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

        new CreateFormInTable().manipulatePdf(DEST);
    }

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

        Table table = new Table(UnitValue.createPercentArray(2)).useAllAvailableWidth();
        Cell cell = new Cell().add(new Paragraph("Name:"));
        table.addCell(cell);

        cell = new Cell();
        cell.setNextRenderer(new CreateFormFieldRenderer(cell, "name"));
        table.addCell(cell);

        cell = new Cell().add(new Paragraph("Address"));
        table.addCell(cell);

        cell = new Cell();
        cell.setNextRenderer(new CreateFormFieldRenderer(cell, "address"));
        table.addCell(cell);

        doc.add(table);

        doc.close();
    }


    private class CreateFormFieldRenderer extends CellRenderer {
        protected String fieldName;

        public CreateFormFieldRenderer(Cell modelElement, String fieldName) {
            super(modelElement);
            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 CreateFormFieldRenderer((Cell) modelElement, fieldName);
        }

        @Override
        public void draw(DrawContext drawContext) {
            super.draw(drawContext);

            PdfTextFormField field = new TextFormFieldBuilder(drawContext.getDocument(), fieldName)
                    .setWidgetRectangle(getOccupiedAreaBBox()).createText();
            field.setValue("");
            PdfAcroForm form = PdfFormCreator.getAcroForm(drawContext.getDocument(), true);
            form.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.Properties;
using iText.Layout.Renderer;

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

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

            new CreateFormInTable().ManipulatePdf(DEST);
        }

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

            Table table = new Table(UnitValue.CreatePercentArray(2)).UseAllAvailableWidth();

            Cell cell = new Cell().Add(new Paragraph("Name:"));
            table.AddCell(cell);

            cell = new Cell();
            cell.SetNextRenderer(new CreateFormFieldRenderer(cell, "name"));
            table.AddCell(cell);

            cell = new Cell().Add(new Paragraph("Address"));
            table.AddCell(cell);

            cell = new Cell();
            cell.SetNextRenderer(new CreateFormFieldRenderer(cell, "address"));
            table.AddCell(cell);

            doc.Add(table);

            doc.Close();
        }

        private class CreateFormFieldRenderer : CellRenderer
        {
            protected String fieldName;

            public CreateFormFieldRenderer(Cell modelElement, String fieldName)
                : base(modelElement)
            {
                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 CreateFormFieldRenderer((Cell) modelElement, fieldName);
            }

            public override void Draw(DrawContext drawContext)
            {
                base.Draw(drawContext);

                PdfTextFormField field = new TextFormFieldBuilder(drawContext.GetDocument(), fieldName)
                    .SetWidgetRectangle(GetOccupiedAreaBBox()).CreateText();
                field.SetValue("");
                PdfAcroForm form = PdfFormCreator.GetAcroForm(drawContext.GetDocument(), true);
                form.AddField(field);
            }
        }
    }
}


createradiointable

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.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.properties.UnitValue;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.IRenderer;

import java.io.File;

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

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

        new CreateRadioInTable().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
        PdfButtonFormField radioGroup = new RadioFormFieldBuilder(pdfDoc, "Language").createRadioGroup();
        radioGroup.setValue("");

        Table table = new Table(UnitValue.createPercentArray(2)).useAllAvailableWidth();
        Cell cell = new Cell().add(new Paragraph("English"));
        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, "english"));
        table.addCell(cell);

        cell = new Cell().add(new Paragraph("French"));
        table.addCell(cell);

        cell = new Cell();
        cell.setNextRenderer(new AddRadioButtonRenderer(cell, radioGroup, "french"));
        table.addCell(cell);

        cell = new Cell().add(new Paragraph("Dutch"));
        table.addCell(cell);

        cell = new Cell();
        cell.setNextRenderer(new AddRadioButtonRenderer(cell, radioGroup, "dutch"));
        table.addCell(cell);

        doc.add(table);

        form.addField(radioGroup);

        doc.close();
    }


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

        public AddRadioButtonRenderer(Cell modelElement, PdfButtonFormField radioGroup, String fieldName) {
            super(modelElement);
            this.radioGroup = radioGroup;
            this.value = 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 AddRadioButtonRenderer((Cell) modelElement, radioGroup, value);
        }

        @Override
        public void draw(DrawContext drawContext) {

            // Create a radio button that is added to a radio group.
            PdfFormAnnotation radio = new RadioFormFieldBuilder(drawContext.getDocument(), this.value)
                    .createRadioButton(value,getOccupiedAreaBBox());
            this.radioGroup.addKid(radio);
        }
    }
}

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 CreateRadioInTable
    {
        public static readonly String DEST = "results/sandbox/acroforms/create_radio_in_table.pdf";
        
        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            
            new CreateRadioInTable().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, "Language");
            PdfButtonFormField group = builder.CreateRadioGroup();
            group.SetValue("");
            
            Table table = new Table(UnitValue.CreatePercentArray(2)).UseAllAvailableWidth();
            Cell cell = new Cell().Add(new Paragraph("English"));
            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, group, "english", builder));
            table.AddCell(cell);

            cell = new Cell().Add(new Paragraph("French"));
            table.AddCell(cell);

            cell = new Cell();
            cell.SetNextRenderer(new AddRadioButtonRenderer(cell, group, "french", builder));
            table.AddCell(cell);

            cell = new Cell().Add(new Paragraph("Dutch"));
            table.AddCell(cell);

            cell = new Cell();
            cell.SetNextRenderer(new AddRadioButtonRenderer(cell, group, "dutch", builder));
            table.AddCell(cell);
            
            doc.Add(table);
            
            form.AddField(group);
            
            doc.Close();
        }

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

            public AddRadioButtonRenderer(Cell modelElement, PdfButtonFormField radioGroup,
                String fieldName, RadioFormFieldBuilder builder) : base(modelElement)
            {
                this.radioGroup = radioGroup;
                this.builder = builder;
                this.value = 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 AddRadioButtonRenderer((Cell) modelElement, radioGroup, value, this.builder);
            }

            public override void Draw(DrawContext drawContext)
            {
                // Create a radio button that is added to a radio group.
               radioGroup.AddKid(builder 
                    .CreateRadioButton( value, GetOccupiedAreaBBox()));
            }
        }
    }
}


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()));
            }
        }
    }
}
JavaScript errors detected

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

If this problem persists, please contact our support.