Create fields in a table
These examples were written in answer to questions such as:
Click How to distribute the radio buttons of a radio field across multiple PdfPCells?
Click Why does iText enter a cross symbol when CheckType style is check mark?
createformintable
JAVA
JAVA
/**
* Example written by Bruno Lowagie in answer to:
* http://stackoverflow.com/questions/21028286/itext-editable-texfield-is-not-clickable
*/
package sandbox.acroforms;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfFormField;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.TextField;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
@WrapToTest
public class CreateFormInTable {
class MyCellField implements PdfPCellEvent {
protected String fieldname;
public MyCellField(String fieldname) {
this.fieldname = fieldname;
}
public void cellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases) {
final PdfWriter writer = canvases[0].getPdfWriter();
final TextField textField = new TextField(writer, rectangle, fieldname);
try {
final PdfFormField field = textField.getTextField();
writer.addAnnotation(field);
} catch (final IOException ioe) {
throw new ExceptionConverter(ioe);
} catch (final DocumentException de) {
throw new ExceptionConverter(de);
}
}
}
public static final String DEST = "results/acroforms/form_in_table.pdf";
public static void main(String[] args) throws IOException,
DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new CreateFormInTable().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfPTable table = new PdfPTable(2);
PdfPCell cell;
cell = new PdfPCell(new Phrase("Name:"));
table.addCell(cell);
cell = new PdfPCell();
cell.setCellEvent(new MyCellField("name"));
table.addCell(cell);
cell = new PdfPCell(new Phrase("Address"));
table.addCell(cell);
cell = new PdfPCell();
cell.setCellEvent(new MyCellField("address"));
table.addCell(cell);
document.add(table);
document.close();
}
}
createradiointable
JAVA
JAVA
/**
* Example written by Bruno Lowagie in answer to:
* http://stackoverflow.com/questions/29393050/itext-radiogroup-radiobuttons-across-multiple-pdfpcells
*/
package sandbox.acroforms;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfFormField;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.RadioCheckField;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
@WrapToTest
public class CreateRadioInTable {
class MyCellField implements PdfPCellEvent {
protected PdfFormField radiogroup;
protected String value;
public MyCellField(PdfFormField radiogroup, String value) {
this.radiogroup = radiogroup;
this.value = value;
}
public void cellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases) {
final PdfWriter writer = canvases[0].getPdfWriter();
RadioCheckField radio = new RadioCheckField(writer, rectangle, null, value);
try {
radiogroup.addKid(radio.getRadioField());
} catch (final IOException ioe) {
throw new ExceptionConverter(ioe);
} catch (final DocumentException de) {
throw new ExceptionConverter(de);
}
}
}
public static final String DEST = "results/acroforms/radio_in_table.pdf";
public static void main(String[] args) throws IOException,
DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new CreateRadioInTable().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfFormField radiogroup = PdfFormField.createRadioButton(writer, true);
radiogroup.setFieldName("Language");
PdfPTable table = new PdfPTable(2);
PdfPCell cell;
cell = new PdfPCell(new Phrase("English:"));
table.addCell(cell);
cell = new PdfPCell();
cell.setCellEvent(new MyCellField(radiogroup, "english"));
table.addCell(cell);
cell = new PdfPCell(new Phrase("French:"));
table.addCell(cell);
cell = new PdfPCell();
cell.setCellEvent(new MyCellField(radiogroup, "french"));
table.addCell(cell);
cell = new PdfPCell(new Phrase("Dutch:"));
table.addCell(cell);
cell = new PdfPCell();
cell.setCellEvent(new MyCellField(radiogroup, "dutch"));
table.addCell(cell);
document.add(table);
writer.addAnnotation(radiogroup);
document.close();
}
}
checkboxcell
JAVA
JAVA
/**
* Example written by Bruno Lowagie in answer to:
* http://stackoverflow.com/questions/21342034/resizing-a-form-field-in-itextsharp
*
* Given a cell, how do you add a check box with a specific size at a specific position.
*/
package sandbox.acroforms;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.RadioCheckField;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
@WrapToTest
public class CheckboxCell {
public static final String DEST = "results/acroforms/checkbox_in_cell.pdf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new CheckboxCell().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
// We create a table with 5 columns
PdfPTable table = new PdfPTable(5);
PdfPCell cell;
// We add 5 cells
for (int i = 0; i < 5; i++) {
cell = new PdfPCell();
cell.setCellEvent(new CheckboxCellEvent("cb" + i));
// We create cell with height 50
cell.setMinimumHeight(50);
table.addCell(cell);
}
document.add(table);
document.close();
}
class CheckboxCellEvent implements PdfPCellEvent {
// The name of the check box field
protected String name;
// We create a cell event
public CheckboxCellEvent(String name) {
this.name = name;
}
// We create and add the check box field
public void cellLayout(PdfPCell cell, Rectangle position,
PdfContentByte[] canvases) {
PdfWriter writer = canvases[0].getPdfWriter();
// 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, x + 10, y + 10);
// define the check box
RadioCheckField checkbox = new RadioCheckField(
writer, rect, name, "Yes");
// add the check box as a field
try {
writer.addAnnotation(checkbox.getCheckField());
} catch (Exception e) {
throw new ExceptionConverter(e);
}
}
}
}
checkboxcell2
JAVA
JAVA
/**
* Example written by Bruno Lowagie in answer to:
* http://stackoverflow.com/questions/34439850
*/
package sandbox.acroforms;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.RadioCheckField;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
@WrapToTest
public class CheckboxCell2 {
public static final String DEST = "results/acroforms/checkbox_in_cell2.pdf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new CheckboxCell2().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
// We create a table with 5 columns
PdfPTable table = new PdfPTable(6);
PdfPCell cell;
// We add 5 cells
for (int i = 0; i < 6; i++) {
cell = new PdfPCell();
cell.setCellEvent(new CheckboxCellEvent("cb" + i, i));
// We create cell with height 50
cell.setMinimumHeight(50);
table.addCell(cell);
}
document.add(table);
document.close();
}
class CheckboxCellEvent implements PdfPCellEvent {
// The name of the check box field
protected String name;
protected int i;
// We create a cell event
public CheckboxCellEvent(String name, int i) {
this.name = name;
this.i = i;
}
// We create and add the check box field
public void cellLayout(PdfPCell cell, Rectangle position,
PdfContentByte[] canvases) {
PdfWriter writer = canvases[0].getPdfWriter();
// 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, x + 10, y + 10);
// define the check box
RadioCheckField checkbox = new RadioCheckField(
writer, rect, name, "Yes");
switch(i) {
case 0:
checkbox.setCheckType(RadioCheckField.TYPE_CHECK);
break;
case 1:
checkbox.setCheckType(RadioCheckField.TYPE_CIRCLE);
break;
case 2:
checkbox.setCheckType(RadioCheckField.TYPE_CROSS);
break;
case 3:
checkbox.setCheckType(RadioCheckField.TYPE_DIAMOND);
break;
case 4:
checkbox.setCheckType(RadioCheckField.TYPE_SQUARE);
break;
case 5:
checkbox.setCheckType(RadioCheckField.TYPE_STAR);
break;
}
checkbox.setChecked(true);
// add the check box as a field
try {
writer.addAnnotation(checkbox.getCheckField());
} catch (Exception e) {
throw new ExceptionConverter(e);
}
}
}
}
radiogroupmultipage2
JAVA
JAVA
/*
* 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 sandbox.acroforms;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfFormField;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.RadioCheckField;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author Bruno Lowagie (iText Software)
*/
public class RadioGroupMultiPage2 {
public static final String DEST = "results/acroforms/multipage_radiobutton2.pdf";
public static void main(String[] args) throws DocumentException, IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new RadioGroupMultiPage2().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfFormField radiogroup = PdfFormField.createRadioButton(writer, true);
radiogroup.setFieldName("answer");
PdfPTable table = new PdfPTable(2);
PdfPCell cell;
for (int i = 0; i < 25; i++) {
cell = new PdfPCell(new Phrase("Question " + i));
table.addCell(cell);
cell = new PdfPCell(new Phrase("Answer " + i));
table.addCell(cell);
}
for (int i = 0; i <25; i++) {
cell = new PdfPCell(new Phrase("Radio: " + i));
table.addCell(cell);
cell = new PdfPCell();
cell.setCellEvent(new MyCellField(writer, radiogroup, "answer" + i));
table.addCell(cell);
}
document.add(table);
writer.addAnnotation(radiogroup);
document.close();
}
class MyCellField implements PdfPCellEvent {
protected PdfWriter writer;
protected PdfFormField radiogroup;
protected String value;
public MyCellField(PdfWriter writer, PdfFormField radiogroup, String value) {
this.writer = writer;
this.radiogroup = radiogroup;
this.value = value;
}
public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
try {
RadioCheckField radio = new RadioCheckField(writer, position, null, value);
PdfFormField field = radio.getRadioField();
writer.addAnnotation(field);
radiogroup.addKid(field);
} catch (IOException ex) {
// do nothing
} catch (DocumentException ex) {
// do nothing
}
}
}
}
comboboxitems
JAVA
JAVA
/**
* Example written by Bruno Lowagie in answer to:
* http://stackoverflow.com/questions/28236902/itext-combobox-width-of-selected-option-issue
*/
package sandbox.acroforms;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfBorderDictionary;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfFormField;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.TextField;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
@WrapToTest
public class ComboBoxItems {
class SelectCellEvent implements PdfPCellEvent {
protected PdfFormField selectGroup;
protected String name;
protected String[] exports;
protected String[] options;
protected BaseFont font;
public SelectCellEvent(PdfFormField selectGroup, String name, String[] exports, String[] options) throws DocumentException, IOException {
this.selectGroup = selectGroup;
this.name = name;
this.exports = exports;
this.options = options;
font = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
font.setSubset(false);
}
public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
PdfWriter writer = canvases[0].getPdfWriter();
TextField tf = new TextField(writer, position, name);
tf.setFont(font);
tf.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED);
tf.setVisibility(TextField.VISIBLE_BUT_DOES_NOT_PRINT);
tf.setBorderColor(BaseColor.GRAY);
tf.setChoiceExports(exports);
tf.setChoices(options);
tf.setAlignment(Element.ALIGN_CENTER);
try {
selectGroup.addKid(tf.getComboField());
} catch (Exception e) {
throw new ExceptionConverter(e);
}
}
}
public static final String DEST = "results/acroforms/combobox.pdf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new ComboBoxItems().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Rectangle pagesize = PageSize.LETTER;
Document document = new Document(pagesize);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfPTable table = new PdfPTable(2);
PdfPCell cell;
//Add rows with selectors
PdfFormField selectGroup = PdfFormField.createEmpty(writer);
selectGroup.setFieldName("myCombos");
String[] options = {"Choose first option", "Choose second option", "Choose third option"};
String[] exports = {"option1", "option2", "option3"};
table.addCell("Combobox:");
cell = new PdfPCell();
cell.setCellEvent(new SelectCellEvent(selectGroup, "combo1", exports, options));
cell.setMinimumHeight(20);
table.addCell(cell);
document.add(table);
writer.addAnnotation(selectGroup);
document.close();
}
}