Create fields in a table

31st May 2016
iText
CheckboxCell.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2019 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/21342034/resizing-a-form-field-in-itextsharp
 * <p>
 * Given a cell, how do you add a check box with a specific size at a specific position.
 */
package com.itextpdf.samples.sandbox.acroforms;
 
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.property.UnitValue;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;
 
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();
        Cell cell;
        for (int i = 0; i < 5; i++) {
            cell = new Cell();
            cell.setNextRenderer(new CheckboxCellRenderer(cell, "cb" + i));
            cell.setHeight(50);
            table.addCell(cell);
        }
 
        doc.add(table);
        doc.close();
    }
 
 
    private class CheckboxCellRenderer extends CellRenderer {
        protected String name;
 
        public CheckboxCellRenderer(Cell modelElement, String name) {
            super(modelElement);
            this.name = name;
        }
 
        @Override
        public void draw(DrawContext drawContext) {
            float x = (getOccupiedAreaBBox().getLeft() + getOccupiedAreaBBox().getRight()) / 2;
            float y = (getOccupiedAreaBBox().getTop() + getOccupiedAreaBBox().getBottom()) / 2;
            Rectangle rect = new Rectangle(x - 10, y - 10, 20, 20);
            PdfButtonFormField checkBox = PdfFormField.createCheckBox(drawContext.getDocument(), rect, name, "Yes");
            PdfAcroForm.getAcroForm(drawContext.getDocument(), true).addField(checkBox);
        }
    }
}
CheckboxCell2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2019 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/34439850
 */
package com.itextpdf.samples.sandbox.acroforms;
 
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.property.UnitValue;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;
 
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();
        Cell cell;
        for (int i = 0; i < 6; i++) {
            cell = new 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 i;
 
        public CheckboxCellRenderer(Cell modelElement, String name, int i) {
            super(modelElement);
            this.name = name;
            this.i = i;
        }
 
        @Override
        public void draw(DrawContext drawContext) {
            Rectangle position = getOccupiedAreaBBox();
            // 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);
            // define the check box
            PdfButtonFormField checkBox = PdfFormField.createCheckBox(drawContext.getDocument(), rect, name, "Yes");
            switch (i) {
                case 0:
                    checkBox.setCheckType(PdfFormField.TYPE_CHECK);
                    // Use this method if you changed any field parameters and didn't use setValue
                    checkBox.regenerateField();
                    break;
                case 1:
                    checkBox.setCheckType(PdfFormField.TYPE_CIRCLE);
                    checkBox.regenerateField();
                    break;
                case 2:
                    checkBox.setCheckType(PdfFormField.TYPE_CROSS);
                    checkBox.regenerateField();
                    break;
                case 3:
                    checkBox.setCheckType(PdfFormField.TYPE_DIAMOND);
                    checkBox.regenerateField();
                    break;
                case 4:
                    checkBox.setCheckType(PdfFormField.TYPE_SQUARE);
                    checkBox.regenerateField();
                    break;
                case 5:
                    checkBox.setCheckType(PdfFormField.TYPE_STAR);
                    checkBox.regenerateField();
                    break;
            }
            // add the check box as a field
            PdfAcroForm.getAcroForm(drawContext.getDocument(), true).addField(checkBox);
        }
    }
}
ComboBoxItems.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2019 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/28236902/itext-combobox-width-of-selected-option-issue
 */
package com.itextpdf.samples.sandbox.acroforms;
 
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfChoiceFormField;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.kernel.PdfException;
import com.itextpdf.kernel.colors.ColorConstants;
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.property.UnitValue;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;
 
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();
        Cell cell;
        // Add rows with selectors
        String[] options = {"Choose first option", "Choose second option", "Choose third option"};
        String[] exports = {"option1", "option2", "option3"};
        table.addCell(new Cell().add(new Paragraph("Combobox:")));
        cell = new 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;
        }
 
        @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 = PdfAcroForm.getAcroForm(drawContext.getDocument(), true);
            PdfChoiceFormField choice = PdfFormField.createComboBox(drawContext.getDocument(), getOccupiedAreaBBox(),
                    name, name, optionsArray);
            choice.setFont(font);
            choice.getWidgets().get(0).setBorderStyle(PdfAnnotation.STYLE_BEVELED);
            choice.setVisibility(PdfFormField.VISIBLE_BUT_DOES_NOT_PRINT);
            choice.setBorderColor(ColorConstants.GRAY);
            choice.setJustification(PdfFormField.ALIGN_CENTER);
            form.addField(choice);
        }
    }
}
CreateFormInTable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2019 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/21028286/itext-editable-texfield-is-not-clickable
 */
package com.itextpdf.samples.sandbox.acroforms;
 
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.property.UnitValue;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;
 
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;
        cell = new Cell().add(new Paragraph("Name:"));
        table.addCell(cell);
        cell = new Cell();
        cell.setNextRenderer(new MyCellRenderer(cell, "name"));
        table.addCell(cell);
        cell = new Cell().add(new Paragraph("Address"));
        table.addCell(cell);
        cell = new Cell();
        cell.setNextRenderer(new MyCellRenderer(cell, "address"));
        table.addCell(cell);
        doc.add(table);
 
        doc.close();
    }
 
 
    private class MyCellRenderer extends CellRenderer {
        protected String fieldName;
 
        public MyCellRenderer(Cell modelElement, String fieldName) {
            super(modelElement);
            this.fieldName = fieldName;
        }
 
        @Override
        public void draw(DrawContext drawContext) {
            super.draw(drawContext);
            PdfTextFormField field = PdfFormField.createText(drawContext.getDocument(), getOccupiedAreaBBox(), fieldName, "");
            PdfAcroForm form = PdfAcroForm.getAcroForm(drawContext.getDocument(), true);
            form.addField(field);
        }
    }
}
CreateRadioInTable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2019 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/29393050/itext-radiogroup-radiobuttons-across-multiple-pdfpcells
 */
package com.itextpdf.samples.sandbox.acroforms;
 
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.property.UnitValue;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;
 
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);
 
        PdfButtonFormField group = PdfFormField.createRadioGroup(pdfDoc, "Language", "English");
        Table table = new Table(UnitValue.createPercentArray(2)).useAllAvailableWidth();
        Cell cell;
        cell = new Cell().add(new Paragraph("English"));
        table.addCell(cell);
        cell = new Cell();
        cell.setNextRenderer(new MyCellRenderer(cell, group, "english"));
        table.addCell(cell);
        cell = new Cell().add(new Paragraph("French"));
        table.addCell(cell);
        cell = new Cell();
        cell.setNextRenderer(new MyCellRenderer(cell, group, "french"));
        table.addCell(cell);
        cell = new Cell().add(new Paragraph("Dutch"));
        table.addCell(cell);
        cell = new Cell();
        cell.setNextRenderer(new MyCellRenderer(cell, group, "dutch"));
        table.addCell(cell);
        doc.add(table);
 
        PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
        form.addField(group);
 
        doc.close();
    }
 
 
    private class MyCellRenderer extends CellRenderer {
        protected String value;
        protected PdfButtonFormField radioGroup;
 
        public MyCellRenderer(Cell modelElement, PdfButtonFormField radioGroup, String fieldName) {
            super(modelElement);
            this.radioGroup = radioGroup;
            this.value = fieldName;
        }
 
        @Override
        public void draw(DrawContext drawContext) {
            PdfFormField.createRadioButton(drawContext.getDocument(), getOccupiedAreaBBox(), radioGroup, value);
        }
    }
}
RadioGroupMultiPage2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2019 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * This example was written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/30895930/issue-with-itext-radiocheckfield-when-displayed-on-multiple-pages
 */
package com.itextpdf.samples.sandbox.acroforms;
 
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.property.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 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);
 
        PdfButtonFormField radioGroup = PdfFormField.createRadioGroup(pdfDoc, "answer", "answer 1");
 
        Table table = new Table(UnitValue.createPercentArray(2)).useAllAvailableWidth();
        Cell cell;
        for (int i = 0; i < 25; i++) {
            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 = new Cell().add(new Paragraph("Radio: " + i));
            table.addCell(cell);
            cell = new Cell();
            cell.setNextRenderer(new MyCellRenderer(cell, radioGroup, "answer " + i));
            table.addCell(cell);
        }
        doc.add(table);
 
        PdfAcroForm.getAcroForm(pdfDoc, true).addField(radioGroup);
        pdfDoc.close();
    }
 
 
    private class MyCellRenderer extends CellRenderer {
        protected PdfButtonFormField radioGroup;
        protected String value;
 
        public MyCellRenderer(Cell modelElement, PdfButtonFormField radioGroup, String value) {
            super(modelElement);
            this.radioGroup = radioGroup;
            this.value = value;
        }
 
        @Override
        public void draw(DrawContext drawContext) {
            PdfDocument document = drawContext.getDocument();
            PdfFormField field = PdfFormField.createRadioButton(document, getOccupiedAreaBBox(), radioGroup, value);
            PdfAcroForm.getAcroForm(document, true).addFieldAppearanceToPage(field, document.getPage(getOccupiedArea().getPageNumber()));
        }
    }
}
CheckboxCell.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2019 iText Group NV
Authors: iText Software.
 
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
 
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 = PdfAcroForm.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 = PdfFormField.CreateCheckBox(drawContext.GetDocument(), rect, name, "Yes");
                form.AddField(checkBox);
            }
        }
    }
}
CheckboxCell2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2019 iText Group NV
Authors: iText Software.
 
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
 
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 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 = PdfAcroForm.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 =
                    PdfFormField.CreateCheckBox(drawContext.GetDocument(), rect, this.name, "Yes");
                switch (checkboxTypeIndex)
                {
                    case 0:
                    {
                        checkBox.SetCheckType(PdfFormField.TYPE_CHECK);
 
                        // Use this method if you changed any field parameters and didn't use setValue
                        checkBox.RegenerateField();
                        break;
                    }
 
                    case 1:
                    {
                        checkBox.SetCheckType(PdfFormField.TYPE_CIRCLE);
                        checkBox.RegenerateField();
                        break;
                    }
 
                    case 2:
                    {
                        checkBox.SetCheckType(PdfFormField.TYPE_CROSS);
                        checkBox.RegenerateField();
                        break;
                    }
 
                    case 3:
                    {
                        checkBox.SetCheckType(PdfFormField.TYPE_DIAMOND);
                        checkBox.RegenerateField();
                        break;
                    }
 
                    case 4:
                    {
                        checkBox.SetCheckType(PdfFormField.TYPE_SQUARE);
                        checkBox.RegenerateField();
                        break;
                    }
 
                    case 5:
                    {
                        checkBox.SetCheckType(PdfFormField.TYPE_STAR);
                        checkBox.RegenerateField();
                        break;
                    }
                }
 
                form.AddField(checkBox);
            }
        }
    }
}
ComboBoxItems.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2019 iText Group NV
Authors: iText Software.
 
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
 
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.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 = PdfAcroForm.GetAcroForm(drawContext.GetDocument(), true);
 
                // The 3rd parameter is the combobox name, the 4th parameter is the combobox's initial value
                PdfChoiceFormField choice = PdfFormField.CreateComboBox(drawContext.GetDocument(),
                    GetOccupiedAreaBBox(), name, name, optionsArray);
                choice.SetFont(font);
                choice.GetWidgets()[0].SetBorderStyle(PdfAnnotation.STYLE_BEVELED);
                choice.SetVisibility(PdfFormField.VISIBLE_BUT_DOES_NOT_PRINT);
                choice.SetBorderColor(ColorConstants.GRAY);
                choice.SetJustification(PdfFormField.ALIGN_CENTER);
                form.AddField(choice);
            }
        }
    }
}
CreateFormInTable.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2019 iText Group NV
Authors: iText Software.
 
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
 
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 = PdfFormField.CreateText(drawContext.GetDocument(),
                    GetOccupiedAreaBBox(), fieldName, "");
                PdfAcroForm form = PdfAcroForm.GetAcroForm(drawContext.GetDocument(), true);
                form.AddField(field);
            }
        }
    }
}
CreateRadioInTable.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2019 iText Group NV
Authors: iText Software.
 
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
 
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 = PdfAcroForm.GetAcroForm(pdfDoc, true);
            
            // Radio buttons will be added to this radio group
            PdfButtonFormField group = PdfFormField.CreateRadioGroup(pdfDoc, "Language", "");
            
            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"));
            table.AddCell(cell);
            
            cell = new Cell().Add(new Paragraph("French"));
            table.AddCell(cell);
            
            cell = new Cell();
            cell.SetNextRenderer(new AddRadioButtonRenderer(cell, group, "french"));
            table.AddCell(cell);
            
            cell = new Cell().Add(new Paragraph("Dutch"));
            table.AddCell(cell);
            
            cell = new Cell();
            cell.SetNextRenderer(new AddRadioButtonRenderer(cell, group, "dutch"));
            table.AddCell(cell);
            
            doc.Add(table);
            
            form.AddField(group);
            
            doc.Close();
        }
 
        private class AddRadioButtonRenderer : CellRenderer
        {
            protected String value;
            protected PdfButtonFormField radioGroup;
 
            public AddRadioButtonRenderer(Cell modelElement, PdfButtonFormField radioGroup,
                String fieldName) : base(modelElement)
            {
                this.radioGroup = radioGroup;
                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);
            }
 
            public override void Draw(DrawContext drawContext)
            {
                // Create a radio button that is added to a radio group.
                PdfFormField.CreateRadioButton(drawContext.GetDocument(), GetOccupiedAreaBBox(), radioGroup,
                    value);
            }
        }
    }
}
RadioGroupMultiPage2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2019 iText Group NV
Authors: iText Software.
 
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
 
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 = PdfAcroForm.GetAcroForm(pdfDoc, true);
 
            // Radio buttons will be added to this radio group
            PdfButtonFormField radioGroup = PdfFormField.CreateRadioGroup(pdfDoc, "answer", "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 = PdfAcroForm.GetAcroForm(document, true);
 
                // Create a radio button that is added to a radio group.
                PdfFormField field = PdfFormField.CreateRadioButton(document, GetOccupiedAreaBBox(),
                    radioGroup, value);
 
                // 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, document.GetPage(GetOccupiedArea().GetPageNumber()));
            }
        }
    }
}

Click the following link to see the legacy example for iText 5. Except for security fixes, iText 5 is no longer being developed.



Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now