Create fields in a table

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

6th November 2015
admin-marketing

Switch code for this example

CreateFormInTable.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/21028286/itext-editable-texfield-is-not-clickable
  4.  */
  5. package sandbox.acroforms;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.ExceptionConverter;
  10. import com.itextpdf.text.Phrase;
  11. import com.itextpdf.text.Rectangle;
  12. import com.itextpdf.text.pdf.PdfContentByte;
  13. import com.itextpdf.text.pdf.PdfFormField;
  14. import com.itextpdf.text.pdf.PdfPCell;
  15. import com.itextpdf.text.pdf.PdfPCellEvent;
  16. import com.itextpdf.text.pdf.PdfPTable;
  17. import com.itextpdf.text.pdf.PdfWriter;
  18. import com.itextpdf.text.pdf.TextField;
  19.  
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import sandbox.WrapToTest;
  24.  
  25. @WrapToTest
  26. public class CreateFormInTable {
  27.    
  28.     class MyCellField implements PdfPCellEvent {
  29.         protected String fieldname;
  30.         public MyCellField(String fieldname) {
  31.             this.fieldname = fieldname;
  32.         }
  33.         public void cellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases) {
  34.             final PdfWriter writer = canvases[0].getPdfWriter();
  35.             final TextField textField = new TextField(writer, rectangle, fieldname);
  36.             try {
  37.                 final PdfFormField field = textField.getTextField();
  38.                 writer.addAnnotation(field);
  39.             } catch (final IOException ioe) {
  40.                 throw new ExceptionConverter(ioe);
  41.             } catch (final DocumentException de) {
  42.                 throw new ExceptionConverter(de);
  43.             }
  44.         }
  45.     }
  46.    
  47.     public static final String DEST = "results/acroforms/form_in_table.pdf";
  48.  
  49.     public static void main(String[] args) throws IOException,
  50.             DocumentException {
  51.         File file = new File(DEST);
  52.         file.getParentFile().mkdirs();
  53.         new CreateFormInTable().createPdf(DEST);
  54.     }
  55.    
  56.     public void createPdf(String dest) throws IOException, DocumentException {
  57.         Document document = new Document();
  58.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  59.         document.open();
  60.         PdfPTable table = new PdfPTable(2);
  61.         PdfPCell cell;
  62.         cell = new PdfPCell(new Phrase("Name:"));
  63.         table.addCell(cell);
  64.         cell = new PdfPCell();
  65.         cell.setCellEvent(new MyCellField("name"));
  66.         table.addCell(cell);
  67.         cell = new PdfPCell(new Phrase("Address"));
  68.         table.addCell(cell);
  69.         cell = new PdfPCell();
  70.         cell.setCellEvent(new MyCellField("address"));
  71.         table.addCell(cell);
  72.         document.add(table);
  73.         document.close();
  74.     }
  75.  
  76. }
CreateRadioInTable.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/29393050/itext-radiogroup-radiobuttons-across-multiple-pdfpcells
  4.  */
  5. package sandbox.acroforms;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.ExceptionConverter;
  10. import com.itextpdf.text.Phrase;
  11. import com.itextpdf.text.Rectangle;
  12. import com.itextpdf.text.pdf.PdfContentByte;
  13. import com.itextpdf.text.pdf.PdfFormField;
  14. import com.itextpdf.text.pdf.PdfPCell;
  15. import com.itextpdf.text.pdf.PdfPCellEvent;
  16. import com.itextpdf.text.pdf.PdfPTable;
  17. import com.itextpdf.text.pdf.PdfWriter;
  18. import com.itextpdf.text.pdf.RadioCheckField;
  19.  
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import sandbox.WrapToTest;
  24.  
  25. @WrapToTest
  26. public class CreateRadioInTable {
  27.    
  28.     class MyCellField implements PdfPCellEvent {
  29.         protected PdfFormField radiogroup;
  30.         protected String value;
  31.         public MyCellField(PdfFormField radiogroup, String value) {
  32.             this.radiogroup = radiogroup;
  33.             this.value = value;
  34.         }
  35.         public void cellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases) {
  36.             final PdfWriter writer = canvases[0].getPdfWriter();
  37.             RadioCheckField radio = new RadioCheckField(writer, rectangle, null, value);
  38.             try {
  39.                 radiogroup.addKid(radio.getRadioField());
  40.             } catch (final IOException ioe) {
  41.                 throw new ExceptionConverter(ioe);
  42.             } catch (final DocumentException de) {
  43.                 throw new ExceptionConverter(de);
  44.             }
  45.         }
  46.     }
  47.    
  48.     public static final String DEST = "results/acroforms/radio_in_table.pdf";
  49.  
  50.     public static void main(String[] args) throws IOException,
  51.             DocumentException {
  52.         File file = new File(DEST);
  53.         file.getParentFile().mkdirs();
  54.         new CreateRadioInTable().createPdf(DEST);
  55.     }
  56.    
  57.     public void createPdf(String dest) throws IOException, DocumentException {
  58.         Document document = new Document();
  59.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  60.         document.open();
  61.         PdfFormField radiogroup = PdfFormField.createRadioButton(writer, true);
  62.         radiogroup.setFieldName("Language");
  63.         PdfPTable table = new PdfPTable(2);
  64.         PdfPCell cell;
  65.         cell = new PdfPCell(new Phrase("English:"));
  66.         table.addCell(cell);
  67.         cell = new PdfPCell();
  68.         cell.setCellEvent(new MyCellField(radiogroup, "english"));
  69.         table.addCell(cell);
  70.         cell = new PdfPCell(new Phrase("French:"));
  71.         table.addCell(cell);
  72.         cell = new PdfPCell();
  73.         cell.setCellEvent(new MyCellField(radiogroup, "french"));
  74.         table.addCell(cell);
  75.         cell = new PdfPCell(new Phrase("Dutch:"));
  76.         table.addCell(cell);
  77.         cell = new PdfPCell();
  78.         cell.setCellEvent(new MyCellField(radiogroup, "dutch"));
  79.         table.addCell(cell);
  80.         document.add(table);
  81.         writer.addAnnotation(radiogroup);
  82.         document.close();
  83.     }
  84.  
  85. }
CheckboxCell.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/21342034/resizing-a-form-field-in-itextsharp
  4.  *
  5.  * Given a cell, how do you add a check box with a specific size at a specific position.
  6.  */
  7. package sandbox.acroforms;
  8.  
  9. import com.itextpdf.text.Document;
  10. import com.itextpdf.text.DocumentException;
  11. import com.itextpdf.text.ExceptionConverter;
  12. import com.itextpdf.text.Rectangle;
  13. import com.itextpdf.text.pdf.PdfContentByte;
  14. import com.itextpdf.text.pdf.PdfPCell;
  15. import com.itextpdf.text.pdf.PdfPCellEvent;
  16. import com.itextpdf.text.pdf.PdfPTable;
  17. import com.itextpdf.text.pdf.PdfWriter;
  18. import com.itextpdf.text.pdf.RadioCheckField;
  19.  
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23.  
  24. import sandbox.WrapToTest;
  25.  
  26. @WrapToTest
  27. public class CheckboxCell {
  28.    
  29.     public static final String DEST = "results/acroforms/checkbox_in_cell.pdf";
  30.    
  31.     public static void main(String[] args) throws IOException, DocumentException {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new CheckboxCell().createPdf(DEST);
  35.     }
  36.    
  37.     public void createPdf(String dest) throws IOException, DocumentException {
  38.         Document document = new Document();
  39.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  40.         document.open();
  41.         // We create a table with 5 columns
  42.         PdfPTable table = new PdfPTable(5);
  43.         PdfPCell cell;
  44.         // We add 5 cells
  45.         for (int i = 0; i < 5; i++) {
  46.             cell = new PdfPCell();
  47.             cell.setCellEvent(new CheckboxCellEvent("cb" + i));
  48.             // We create cell with height 50
  49.             cell.setMinimumHeight(50);
  50.             table.addCell(cell);
  51.         }
  52.         document.add(table);
  53.         document.close();
  54.     }
  55.    
  56.     class CheckboxCellEvent implements PdfPCellEvent {
  57.         // The name of the check box field
  58.         protected String name;
  59.         // We create a cell event
  60.         public CheckboxCellEvent(String name) {
  61.             this.name = name;
  62.         }
  63.         // We create and add the check box field
  64.         public void cellLayout(PdfPCell cell, Rectangle position,
  65.             PdfContentByte[] canvases) {
  66.             PdfWriter writer = canvases[0].getPdfWriter();
  67.             // define the coordinates of the middle
  68.             float x = (position.getLeft() + position.getRight()) / 2;
  69.             float y = (position.getTop() + position.getBottom()) / 2;
  70.             // define the position of a check box that measures 20 by 20
  71.             Rectangle rect = new Rectangle(x - 10, y - 10, x + 10, y + 10);
  72.             // define the check box
  73.             RadioCheckField checkbox = new RadioCheckField(
  74.                     writer, rect, name, "Yes");
  75.             // add the check box as a field
  76.             try {
  77.                 writer.addAnnotation(checkbox.getCheckField());
  78.             } catch (Exception e) {
  79.                 throw new ExceptionConverter(e);
  80.             }
  81.         }
  82.     }
  83. }
CheckboxCell2.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/34439850
  4.  */
  5. package sandbox.acroforms;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.ExceptionConverter;
  10. import com.itextpdf.text.Rectangle;
  11. import com.itextpdf.text.pdf.PdfContentByte;
  12. import com.itextpdf.text.pdf.PdfPCell;
  13. import com.itextpdf.text.pdf.PdfPCellEvent;
  14. import com.itextpdf.text.pdf.PdfPTable;
  15. import com.itextpdf.text.pdf.PdfWriter;
  16. import com.itextpdf.text.pdf.RadioCheckField;
  17.  
  18. import java.io.File;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21.  
  22. import sandbox.WrapToTest;
  23.  
  24. @WrapToTest
  25. public class CheckboxCell2 {
  26.    
  27.     public static final String DEST = "results/acroforms/checkbox_in_cell2.pdf";
  28.    
  29.     public static void main(String[] args) throws IOException, DocumentException {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new CheckboxCell2().createPdf(DEST);
  33.     }
  34.    
  35.     public void createPdf(String dest) throws IOException, DocumentException {
  36.         Document document = new Document();
  37.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  38.         document.open();
  39.         // We create a table with 5 columns
  40.         PdfPTable table = new PdfPTable(6);
  41.         PdfPCell cell;
  42.         // We add 5 cells
  43.         for (int i = 0; i < 6; i++) {
  44.             cell = new PdfPCell();
  45.             cell.setCellEvent(new CheckboxCellEvent("cb" + i, i));
  46.             // We create cell with height 50
  47.             cell.setMinimumHeight(50);
  48.             table.addCell(cell);
  49.         }
  50.         document.add(table);
  51.         document.close();
  52.     }
  53.    
  54.     class CheckboxCellEvent implements PdfPCellEvent {
  55.         // The name of the check box field
  56.         protected String name;
  57.         protected int i;
  58.         // We create a cell event
  59.         public CheckboxCellEvent(String name, int i) {
  60.             this.name = name;
  61.             this.i = i;
  62.         }
  63.         // We create and add the check box field
  64.         public void cellLayout(PdfPCell cell, Rectangle position,
  65.             PdfContentByte[] canvases) {
  66.             PdfWriter writer = canvases[0].getPdfWriter();
  67.             // define the coordinates of the middle
  68.             float x = (position.getLeft() + position.getRight()) / 2;
  69.             float y = (position.getTop() + position.getBottom()) / 2;
  70.             // define the position of a check box that measures 20 by 20
  71.             Rectangle rect = new Rectangle(x - 10, y - 10, x + 10, y + 10);
  72.             // define the check box
  73.             RadioCheckField checkbox = new RadioCheckField(
  74.                     writer, rect, name, "Yes");
  75.             switch(i) {
  76.                 case 0:
  77.                     checkbox.setCheckType(RadioCheckField.TYPE_CHECK);
  78.                     break;
  79.                 case 1:
  80.                     checkbox.setCheckType(RadioCheckField.TYPE_CIRCLE);
  81.                     break;
  82.                 case 2:
  83.                     checkbox.setCheckType(RadioCheckField.TYPE_CROSS);
  84.                     break;
  85.                 case 3:
  86.                     checkbox.setCheckType(RadioCheckField.TYPE_DIAMOND);
  87.                     break;
  88.                 case 4:
  89.                     checkbox.setCheckType(RadioCheckField.TYPE_SQUARE);
  90.                     break;
  91.                 case 5:
  92.                     checkbox.setCheckType(RadioCheckField.TYPE_STAR);
  93.                     break;
  94.             }
  95.             checkbox.setChecked(true);
  96.             // add the check box as a field
  97.             try {
  98.                 writer.addAnnotation(checkbox.getCheckField());
  99.             } catch (Exception e) {
  100.                 throw new ExceptionConverter(e);
  101.             }
  102.         }
  103.     }
  104. }
RadioGroupMultiPage2.java
  1. /*
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/30895930/issue-with-itext-radiocheckfield-when-displayed-on-multiple-pages
  4.  */
  5. package sandbox.acroforms;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Phrase;
  10. import com.itextpdf.text.Rectangle;
  11. import com.itextpdf.text.pdf.PdfContentByte;
  12. import com.itextpdf.text.pdf.PdfFormField;
  13. import com.itextpdf.text.pdf.PdfPCell;
  14. import com.itextpdf.text.pdf.PdfPCellEvent;
  15. import com.itextpdf.text.pdf.PdfPTable;
  16. import com.itextpdf.text.pdf.PdfWriter;
  17. import com.itextpdf.text.pdf.RadioCheckField;
  18.  
  19. import java.io.File;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22.  
  23. /**
  24.  * @author Bruno Lowagie (iText Software)
  25.  */
  26. public class RadioGroupMultiPage2 {
  27.    
  28.     public static final String DEST = "results/acroforms/multipage_radiobutton2.pdf";
  29.     public static void main(String[] args) throws DocumentException, IOException {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new RadioGroupMultiPage2().createPdf(DEST);
  33.     }
  34.     public void createPdf(String dest) throws IOException, DocumentException {
  35.         Document document = new Document();
  36.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  37.         document.open();
  38.         PdfFormField radiogroup = PdfFormField.createRadioButton(writer, true);
  39.         radiogroup.setFieldName("answer");
  40.         PdfPTable table = new PdfPTable(2);
  41.         PdfPCell cell;
  42.         for (int i = 0; i < 25; i++) {
  43.             cell = new PdfPCell(new Phrase("Question " + i));
  44.             table.addCell(cell);
  45.             cell = new PdfPCell(new Phrase("Answer " + i));
  46.             table.addCell(cell);
  47.         }
  48.         for (int i = 0; i
ComboBoxItems.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/28236902/itext-combobox-width-of-selected-option-issue
  4.  */
  5. package sandbox.acroforms;
  6.  
  7. import com.itextpdf.text.BaseColor;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Element;
  11. import com.itextpdf.text.ExceptionConverter;
  12. import com.itextpdf.text.PageSize;
  13. import com.itextpdf.text.Rectangle;
  14. import com.itextpdf.text.pdf.BaseFont;
  15. import com.itextpdf.text.pdf.PdfBorderDictionary;
  16. import com.itextpdf.text.pdf.PdfContentByte;
  17. import com.itextpdf.text.pdf.PdfFormField;
  18. import com.itextpdf.text.pdf.PdfPCell;
  19. import com.itextpdf.text.pdf.PdfPCellEvent;
  20. import com.itextpdf.text.pdf.PdfPTable;
  21. import com.itextpdf.text.pdf.PdfWriter;
  22. import com.itextpdf.text.pdf.TextField;
  23.  
  24. import java.io.File;
  25. import java.io.FileOutputStream;
  26. import java.io.IOException;
  27. import sandbox.WrapToTest;
  28.  
  29. @WrapToTest
  30. public class ComboBoxItems {
  31.  
  32.     class SelectCellEvent implements PdfPCellEvent {
  33.         protected PdfFormField selectGroup;
  34.         protected String name;
  35.         protected String[] exports;
  36.         protected String[] options;
  37.         protected BaseFont font;
  38.        
  39.         public SelectCellEvent(PdfFormField selectGroup, String name, String[] exports, String[] options) throws DocumentException, IOException {
  40.             this.selectGroup = selectGroup;
  41.             this.name = name;
  42.             this.exports = exports;
  43.             this.options = options;
  44.             font = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
  45.             font.setSubset(false);
  46.         }
  47.        
  48.         public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
  49.             PdfWriter writer = canvases[0].getPdfWriter();
  50.             TextField tf = new TextField(writer, position, name);
  51.             tf.setFont(font);
  52.             tf.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED);
  53.             tf.setVisibility(TextField.VISIBLE_BUT_DOES_NOT_PRINT);
  54.             tf.setBorderColor(BaseColor.GRAY);
  55.             tf.setChoiceExports(exports);
  56.             tf.setChoices(options);
  57.             tf.setAlignment(Element.ALIGN_CENTER);
  58.             try {
  59.                 selectGroup.addKid(tf.getComboField());
  60.             } catch (Exception e) {
  61.                 throw new ExceptionConverter(e);
  62.             }
  63.         }
  64.     }
  65.    
  66.     public static final String DEST = "results/acroforms/combobox.pdf";
  67.  
  68.     public static void main(String[] args) throws IOException, DocumentException {
  69.         File file = new File(DEST);
  70.         file.getParentFile().mkdirs();
  71.         new ComboBoxItems().createPdf(DEST);
  72.     }
  73.    
  74.     public void createPdf(String dest) throws IOException, DocumentException {
  75.         Rectangle pagesize = PageSize.LETTER;
  76.         Document document = new Document(pagesize);
  77.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  78.         document.open();
  79.         PdfPTable table = new PdfPTable(2);
  80.         PdfPCell cell;
  81.         //Add rows with selectors
  82.         PdfFormField selectGroup = PdfFormField.createEmpty(writer);
  83.         selectGroup.setFieldName("myCombos");
  84.         String[] options = {"Choose first option", "Choose second option", "Choose third option"};
  85.         String[] exports = {"option1", "option2", "option3"};
  86.         table.addCell("Combobox:");
  87.         cell = new PdfPCell();
  88.         cell.setCellEvent(new SelectCellEvent(selectGroup, "combo1", exports, options));
  89.         cell.setMinimumHeight(20);
  90.         table.addCell(cell);
  91.         document.add(table);
  92.         writer.addAnnotation(selectGroup);
  93.         document.close();
  94.     }
  95.  
  96. }
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