Create fields in a table

This is a code example of iText PDF, discover more.

31st May 2016
iText PDF

Switch code for this example

CheckboxCell.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/21342034/resizing-a-form-field-in-itextsharp
  11.  *
  12.  * Given a cell, how do you add a check box with a specific size at a specific position.
  13.  */
  14. package com.itextpdf.samples.sandbox.acroforms;
  15.  
  16. import com.itextpdf.kernel.geom.Rectangle;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfWriter;
  19. import com.itextpdf.forms.PdfAcroForm;
  20. import com.itextpdf.forms.fields.PdfButtonFormField;
  21. import com.itextpdf.forms.fields.PdfFormField;
  22. import com.itextpdf.layout.Document;
  23. import com.itextpdf.layout.element.Cell;
  24. import com.itextpdf.layout.element.Table;
  25. import com.itextpdf.layout.renderer.CellRenderer;
  26. import com.itextpdf.layout.renderer.DrawContext;
  27. import com.itextpdf.samples.GenericTest;
  28. import com.itextpdf.test.annotations.type.SampleTest;
  29.  
  30. import java.io.File;
  31. import java.io.FileOutputStream;
  32.  
  33. import org.junit.experimental.categories.Category;
  34.  
  35. @Category(SampleTest.class)
  36. public class CheckboxCell extends GenericTest {
  37.     public static final String DEST = "./target/test/resources/sandbox/acroforms/checkbox_cell.pdf";
  38.  
  39.     public static void main(String[] args) throws Exception {
  40.         File file = new File(DEST);
  41.         file.getParentFile().mkdirs();
  42.         new CheckboxCell().manipulatePdf(DEST);
  43.     }
  44.  
  45.     @Override
  46.     protected void manipulatePdf(String dest) throws Exception {
  47.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  48.         Document doc = new Document(pdfDoc);
  49.  
  50.         Table table = new Table(5);
  51.         Cell cell;
  52.         for (int i = 0; i < 5; i++) {
  53.             cell = new Cell();
  54.             cell.setNextRenderer(new CheckboxCellRenderer(cell, "cb" + i));
  55.             cell.setHeight(50);
  56.             table.addCell(cell);
  57.         }
  58.  
  59.         doc.add(table);
  60.         doc.close();
  61.     }
  62.  
  63.  
  64.     private class CheckboxCellRenderer extends CellRenderer {
  65.         protected String name;
  66.  
  67.         public CheckboxCellRenderer(Cell modelElement, String name) {
  68.             super(modelElement);
  69.             this.name = name;
  70.         }
  71.  
  72.         @Override
  73.         public void draw(DrawContext drawContext) {
  74.             float x = (getOccupiedAreaBBox().getLeft() + getOccupiedAreaBBox().getRight()) / 2;
  75.             float y = (getOccupiedAreaBBox().getTop() + getOccupiedAreaBBox().getBottom()) / 2;
  76.             Rectangle rect = new Rectangle(x - 10, y - 10, 20, 20);
  77.             PdfButtonFormField checkBox = PdfFormField.createCheckBox(drawContext.getDocument(), rect, name, "Yes");
  78.             PdfAcroForm.getAcroForm(drawContext.getDocument(), true).addField(checkBox);
  79.         }
  80.     }
  81. }
CheckboxCell2.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/34439850
  11.  */
  12. package com.itextpdf.samples.sandbox.acroforms;
  13.  
  14. import com.itextpdf.kernel.geom.Rectangle;
  15. import com.itextpdf.kernel.pdf.PdfDocument;
  16. import com.itextpdf.kernel.pdf.PdfWriter;
  17. import com.itextpdf.forms.PdfAcroForm;
  18. import com.itextpdf.forms.fields.PdfButtonFormField;
  19. import com.itextpdf.forms.fields.PdfFormField;
  20. import com.itextpdf.layout.Document;
  21. import com.itextpdf.layout.element.Cell;
  22. import com.itextpdf.layout.element.Table;
  23. import com.itextpdf.layout.renderer.CellRenderer;
  24. import com.itextpdf.layout.renderer.DrawContext;
  25. import com.itextpdf.samples.GenericTest;
  26. import com.itextpdf.test.annotations.type.SampleTest;
  27.  
  28. import java.io.File;
  29. import java.io.FileOutputStream;
  30.  
  31. import org.junit.experimental.categories.Category;
  32.  
  33. @Category(SampleTest.class)
  34. public class CheckboxCell2 extends GenericTest {
  35.     public static final String DEST = "./target/test/resources/sandbox/acroforms/checkbox_cell2.pdf";
  36.  
  37.     public static void main(String[] args) throws Exception {
  38.         File file = new File(DEST);
  39.         file.getParentFile().mkdirs();
  40.         new CheckboxCell2().manipulatePdf(DEST);
  41.     }
  42.  
  43.     @Override
  44.     protected void manipulatePdf(String dest) throws Exception {
  45.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  46.         Document doc = new Document(pdfDoc);
  47.  
  48.         Table table = new Table(6);
  49.         Cell cell;
  50.         for (int i = 0; i < 6; i++) {
  51.             cell = new Cell();
  52.             cell.setNextRenderer(new CheckboxCellRenderer(cell, "cb" + i, i));
  53.             cell.setHeight(50);
  54.             table.addCell(cell);
  55.         }
  56.  
  57.         doc.add(table);
  58.         doc.close();
  59.     }
  60.  
  61.  
  62.     private class CheckboxCellRenderer extends CellRenderer {
  63.         // The name of the check box field
  64.         protected String name;
  65.         protected int i;
  66.  
  67.         public CheckboxCellRenderer(Cell modelElement, String name, int i) {
  68.             super(modelElement);
  69.             this.name = name;
  70.             this.i = i;
  71.         }
  72.  
  73.         @Override
  74.         public void draw(DrawContext drawContext) {
  75.             Rectangle position = getOccupiedAreaBBox();
  76.             // define the coordinates of the middle
  77.             float x = (position.getLeft() + position.getRight()) / 2;
  78.             float y = (position.getTop() + position.getBottom()) / 2;
  79.             // define the position of a check box that measures 20 by 20
  80.             Rectangle rect = new Rectangle(x - 10, y - 10, 20, 20);
  81.             // define the check box
  82.             PdfButtonFormField checkBox = PdfFormField.createCheckBox(drawContext.getDocument(), rect, name, "Yes");
  83.             switch (i) {
  84.                 case 0:
  85.                     checkBox.setCheckType(PdfFormField.TYPE_CHECK);
  86.                     // Use this method if you changed any field parameters and didn't use setValue
  87.                     checkBox.regenerateField();
  88.                     break;
  89.                 case 1:
  90.                     checkBox.setCheckType(PdfFormField.TYPE_CIRCLE);
  91.                     checkBox.regenerateField();
  92.                     break;
  93.                 case 2:
  94.                     checkBox.setCheckType(PdfFormField.TYPE_CROSS);
  95.                     checkBox.regenerateField();
  96.                     break;
  97.                 case 3:
  98.                     checkBox.setCheckType(PdfFormField.TYPE_DIAMOND);
  99.                     checkBox.regenerateField();
  100.                     break;
  101.                 case 4:
  102.                     checkBox.setCheckType(PdfFormField.TYPE_SQUARE);
  103.                     checkBox.regenerateField();
  104.                     break;
  105.                 case 5:
  106.                     checkBox.setCheckType(PdfFormField.TYPE_STAR);
  107.                     checkBox.regenerateField();
  108.                     break;
  109.             }
  110.             // add the check box as a field
  111.             PdfAcroForm.getAcroForm(drawContext.getDocument(), true).addField(checkBox);
  112.         }
  113.     }
  114. }
ComboBoxItems.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/28236902/itext-combobox-width-of-selected-option-issue
  11.  */
  12. package com.itextpdf.samples.sandbox.acroforms;
  13.  
  14. import com.itextpdf.forms.PdfAcroForm;
  15. import com.itextpdf.forms.fields.PdfChoiceFormField;
  16. import com.itextpdf.forms.fields.PdfFormField;
  17. import com.itextpdf.io.font.FontConstants;
  18. import com.itextpdf.kernel.PdfException;
  19. import com.itextpdf.kernel.color.Color;
  20. import com.itextpdf.kernel.font.PdfFont;
  21. import com.itextpdf.kernel.font.PdfFontFactory;
  22. import com.itextpdf.kernel.geom.PageSize;
  23. import com.itextpdf.kernel.pdf.PdfDocument;
  24. import com.itextpdf.kernel.pdf.PdfWriter;
  25. import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
  26. import com.itextpdf.layout.Document;
  27. import com.itextpdf.layout.element.Cell;
  28. import com.itextpdf.layout.element.Paragraph;
  29. import com.itextpdf.layout.element.Table;
  30. import com.itextpdf.layout.renderer.CellRenderer;
  31. import com.itextpdf.layout.renderer.DrawContext;
  32. import com.itextpdf.samples.GenericTest;
  33. import com.itextpdf.test.annotations.type.SampleTest;
  34.  
  35. import org.junit.experimental.categories.Category;
  36.  
  37. import java.io.File;
  38. import java.io.FileOutputStream;
  39. import java.io.IOException;
  40.  
  41. @Category(SampleTest.class)
  42. public class ComboBoxItems extends GenericTest {
  43.     public static final String DEST = "./target/test/resources/sandbox/acroforms/combo_box_items.pdf";
  44.  
  45.     public static void main(String[] args) throws Exception {
  46.         File file = new File(DEST);
  47.         file.getParentFile().mkdirs();
  48.         new ComboBoxItems().manipulatePdf(DEST);
  49.     }
  50.  
  51.     @Override
  52.     protected void manipulatePdf(String dest) throws Exception {
  53.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  54.         Document doc = new Document(pdfDoc, new PageSize(612, 792));
  55.  
  56.         Table table = new Table(2);
  57.         Cell cell;
  58.         // Add rows with selectors
  59.         String[] options = {"Choose first option", "Choose second option", "Choose third option"};
  60.         String[] exports = {"option1", "option2", "option3"};
  61.         table.addCell(new Cell().add("Combobox:"));
  62.         cell = new Cell();
  63.         cell.setNextRenderer(new SelectCellRenderer(cell, "Choose first option", exports, options));
  64.         cell.setHeight(20);
  65.         table.addCell(cell);
  66.         doc.add(table);
  67.  
  68.         doc.close();
  69.     }
  70.  
  71.  
  72.     private class SelectCellRenderer extends CellRenderer {
  73.         protected String name;
  74.         protected String[] exports;
  75.         protected String[] options;
  76.  
  77.         public SelectCellRenderer(Cell modelElement, String name, String[] exports, String[] options) {
  78.             super(modelElement);
  79.             this.name = name;
  80.             this.exports = exports;
  81.             this.options = options;
  82.         }
  83.  
  84.         @Override
  85.         public void draw(DrawContext drawContext) {
  86.             PdfFont font;
  87.             try {
  88.                 font = PdfFontFactory.createFont(FontConstants.HELVETICA);
  89.             } catch (IOException e) {
  90.                 throw new PdfException(e);
  91.             }
  92.             String[][] optionsArray = new String[options.length][];
  93.             for (int i = 0; i < options.length; i++) {
  94.                 optionsArray[i] = new String[2];
  95.                 optionsArray[i][0] = exports[i];
  96.                 optionsArray[i][1] = options[i];
  97.             }
  98.             PdfAcroForm form = PdfAcroForm.getAcroForm(drawContext.getDocument(), true);
  99.             PdfChoiceFormField choice = PdfFormField.createComboBox(drawContext.getDocument(), getOccupiedAreaBBox(),
  100.                     name, name, optionsArray);
  101.             choice.setFont(font);
  102.             choice.getWidgets().get(0).setBorderStyle(PdfAnnotation.STYLE_BEVELED);
  103.             choice.setVisibility(PdfFormField.VISIBLE_BUT_DOES_NOT_PRINT);
  104.             choice.setBorderColor(Color.GRAY);
  105.             choice.setJustification(PdfFormField.ALIGN_CENTER);
  106.             form.addField(choice);
  107.         }
  108.     }
  109. }
CreateFormInTable.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/21028286/itext-editable-texfield-is-not-clickable
  11.  */
  12. package com.itextpdf.samples.sandbox.acroforms;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfWriter;
  16. import com.itextpdf.forms.PdfAcroForm;
  17. import com.itextpdf.forms.fields.PdfFormField;
  18. import com.itextpdf.forms.fields.PdfTextFormField;
  19. import com.itextpdf.layout.Document;
  20. import com.itextpdf.layout.element.Cell;
  21. import com.itextpdf.layout.element.Table;
  22. import com.itextpdf.layout.renderer.CellRenderer;
  23. import com.itextpdf.layout.renderer.DrawContext;
  24. import com.itextpdf.samples.GenericTest;
  25. import com.itextpdf.test.annotations.type.SampleTest;
  26.  
  27. import java.io.File;
  28. import java.io.FileOutputStream;
  29.  
  30. import org.junit.experimental.categories.Category;
  31.  
  32. @Category(SampleTest.class)
  33. public class CreateFormInTable extends GenericTest {
  34.     public static final String DEST = "./target/test/resources/sandbox/acroforms/create_form_in_table.pdf";
  35.  
  36.     public static void main(String[] args) throws Exception {
  37.         File file = new File(DEST);
  38.         file.getParentFile().mkdirs();
  39.         new CreateFormInTable().manipulatePdf(DEST);
  40.     }
  41.  
  42.     @Override
  43.     protected void manipulatePdf(String dest) throws Exception {
  44.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  45.         Document doc = new Document(pdfDoc);
  46.  
  47.         Table table = new Table(2);
  48.         Cell cell;
  49.         cell = new Cell().add("Name:");
  50.         table.addCell(cell);
  51.         cell = new Cell();
  52.         cell.setNextRenderer(new MyCellRenderer(cell, "name"));
  53.         table.addCell(cell);
  54.         cell = new Cell().add("Address");
  55.         table.addCell(cell);
  56.         cell = new Cell();
  57.         cell.setNextRenderer(new MyCellRenderer(cell, "address"));
  58.         table.addCell(cell);
  59.         doc.add(table);
  60.  
  61.         doc.close();
  62.     }
  63.  
  64.  
  65.     private class MyCellRenderer extends CellRenderer {
  66.         protected String fieldName;
  67.  
  68.         public MyCellRenderer(Cell modelElement, String fieldName) {
  69.             super(modelElement);
  70.             this.fieldName = fieldName;
  71.         }
  72.  
  73.         @Override
  74.         public void draw(DrawContext drawContext) {
  75.             super.draw(drawContext);
  76.             PdfTextFormField field = PdfFormField.createText(drawContext.getDocument(), getOccupiedAreaBBox(), fieldName, "");
  77.             PdfAcroForm form = PdfAcroForm.getAcroForm(drawContext.getDocument(), true);
  78.             form.addField(field);
  79.         }
  80.     }
  81. }
CreateRadioInTable.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/29393050/itext-radiogroup-radiobuttons-across-multiple-pdfpcells
  11.  */
  12. package com.itextpdf.samples.sandbox.acroforms;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfWriter;
  16. import com.itextpdf.forms.PdfAcroForm;
  17. import com.itextpdf.forms.fields.PdfButtonFormField;
  18. import com.itextpdf.forms.fields.PdfFormField;
  19. import com.itextpdf.layout.Document;
  20. import com.itextpdf.layout.element.Cell;
  21. import com.itextpdf.layout.element.Table;
  22. import com.itextpdf.layout.renderer.CellRenderer;
  23. import com.itextpdf.layout.renderer.DrawContext;
  24. import com.itextpdf.samples.GenericTest;
  25. import com.itextpdf.test.annotations.type.SampleTest;
  26.  
  27. import java.io.File;
  28. import java.io.FileOutputStream;
  29.  
  30. import org.junit.experimental.categories.Category;
  31.  
  32. @Category(SampleTest.class)
  33. public class CreateRadioInTable extends GenericTest {
  34.     public static final String DEST = "./target/test/resources/sandbox/acroforms/create_radio_in_table.pdf";
  35.  
  36.     public static void main(String[] args) throws Exception {
  37.         File file = new File(DEST);
  38.         file.getParentFile().mkdirs();
  39.         new CreateRadioInTable().manipulatePdf(DEST);
  40.     }
  41.  
  42.     @Override
  43.     protected void manipulatePdf(String dest) throws Exception {
  44.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  45.         Document doc = new Document(pdfDoc);
  46.  
  47.         PdfButtonFormField group = PdfFormField.createRadioGroup(pdfDoc, "Language", "English");
  48.         Table table = new Table(2);
  49.         Cell cell;
  50.         cell = new Cell().add("English");
  51.         table.addCell(cell);
  52.         cell = new Cell();
  53.         cell.setNextRenderer(new MyCellRenderer(cell, group, "english"));
  54.         table.addCell(cell);
  55.         cell = new Cell().add("French");
  56.         table.addCell(cell);
  57.         cell = new Cell();
  58.         cell.setNextRenderer(new MyCellRenderer(cell, group, "french"));
  59.         table.addCell(cell);
  60.         cell = new Cell().add("Dutch");
  61.         table.addCell(cell);
  62.         cell = new Cell();
  63.         cell.setNextRenderer(new MyCellRenderer(cell, group, "dutch"));
  64.         table.addCell(cell);
  65.         doc.add(table);
  66.  
  67.         PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
  68.         form.addField(group);
  69.  
  70.         doc.close();
  71.     }
  72.  
  73.  
  74.     private class MyCellRenderer extends CellRenderer {
  75.         protected String value;
  76.         protected PdfButtonFormField radioGroup;
  77.  
  78.         public MyCellRenderer(Cell modelElement, PdfButtonFormField radioGroup, String fieldName) {
  79.             super(modelElement);
  80.             this.radioGroup = radioGroup;
  81.             this.value = fieldName;
  82.         }
  83.  
  84.         @Override
  85.         public void draw(DrawContext drawContext) {
  86.             PdfFormField.createRadioButton(drawContext.getDocument(), getOccupiedAreaBBox(), radioGroup, value);
  87.         }
  88.     }
  89. }
RadioGroupMultiPage2.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * This example was written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/30895930/issue-with-itext-radiocheckfield-when-displayed-on-multiple-pages
  11.  */
  12. package com.itextpdf.samples.sandbox.acroforms;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfWriter;
  16. import com.itextpdf.test.annotations.type.SampleTest;
  17. import com.itextpdf.forms.PdfAcroForm;
  18. import com.itextpdf.forms.fields.PdfButtonFormField;
  19. import com.itextpdf.forms.fields.PdfFormField;
  20. import com.itextpdf.layout.Document;
  21. import com.itextpdf.layout.element.Cell;
  22. import com.itextpdf.layout.element.Paragraph;
  23. import com.itextpdf.layout.element.Table;
  24. import com.itextpdf.layout.renderer.CellRenderer;
  25. import com.itextpdf.layout.renderer.DrawContext;
  26. import com.itextpdf.samples.GenericTest;
  27.  
  28. import java.io.File;
  29. import java.io.FileOutputStream;
  30.  
  31. import org.junit.experimental.categories.Category;
  32.  
  33. @Category(SampleTest.class)
  34. public class RadioGroupMultiPage2 extends GenericTest {
  35.     public static final String DEST = "./target/test/resources/sandbox/acroforms/radio_group_multi_page2.pdf";
  36.  
  37.     public static void main(String[] args) throws Exception {
  38.         File file = new File(DEST);
  39.         file.getParentFile().mkdirs();
  40.         new RadioGroupMultiPage2().manipulatePdf(DEST);
  41.     }
  42.  
  43.     @Override
  44.     protected void manipulatePdf(String dest) throws Exception {
  45.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  46.         Document doc = new Document(pdfDoc);
  47.  
  48.         PdfButtonFormField radioGroup = PdfFormField.createRadioGroup(pdfDoc, "answer", "answer 1");
  49.  
  50.         Table table = new Table(2);
  51.         Cell cell;
  52.         for (int i = 0; i < 25; i++) {
  53.             cell = new Cell().add(new Paragraph("Question " + i));
  54.             table.addCell(cell);
  55.             cell = new Cell().add(new Paragraph("Answer " + i));
  56.             table.addCell(cell);
  57.         }
  58.         for (int i = 0; i < 25; i++) {
  59.             cell = new Cell().add(new Paragraph("Radio: " + i));
  60.             table.addCell(cell);
  61.             cell = new Cell();
  62.             cell.setNextRenderer(new MyCellRenderer(cell, radioGroup, "answer " + i));
  63.             table.addCell(cell);
  64.         }
  65.         doc.add(table);
  66.  
  67.         PdfAcroForm.getAcroForm(pdfDoc, true).addField(radioGroup);
  68.         pdfDoc.close();
  69.     }
  70.  
  71.  
  72.     private class MyCellRenderer extends CellRenderer {
  73.         protected PdfButtonFormField radioGroup;
  74.         protected String value;
  75.  
  76.         public MyCellRenderer(Cell modelElement, PdfButtonFormField radioGroup, String value) {
  77.             super(modelElement);
  78.             this.radioGroup = radioGroup;
  79.             this.value = value;
  80.         }
  81.  
  82.         @Override
  83.         public void draw(DrawContext drawContext) {
  84.             PdfDocument document = drawContext.getDocument();
  85.             PdfFormField field = PdfFormField.createRadioButton(document, getOccupiedAreaBBox(), radioGroup, value);
  86.             PdfAcroForm.getAcroForm(document, true).addFieldAppearanceToPage(field, document.getPage(getOccupiedArea().getPageNumber()));
  87.         }
  88.     }
  89. }
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