Creating form fields

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

31st May 2016
iText PDF

Switch code for this example

CreateJapaneseButton.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/29016333/pushbuttonfield-with-double-byte-character
  11.  */
  12. package com.itextpdf.samples.sandbox.acroforms;
  13.  
  14. import com.itextpdf.forms.PdfAcroForm;
  15. import com.itextpdf.forms.fields.PdfButtonFormField;
  16. import com.itextpdf.forms.fields.PdfFormField;
  17. import com.itextpdf.io.font.PdfEncodings;
  18. import com.itextpdf.kernel.font.PdfFont;
  19. import com.itextpdf.kernel.font.PdfFontFactory;
  20. import com.itextpdf.kernel.geom.Rectangle;
  21. import com.itextpdf.kernel.pdf.PdfDocument;
  22. import com.itextpdf.kernel.pdf.PdfWriter;
  23. import com.itextpdf.samples.GenericTest;
  24. import com.itextpdf.test.annotations.type.SampleTest;
  25.  
  26. import java.io.File;
  27.  
  28. import org.junit.experimental.categories.Category;
  29.  
  30. @Category(SampleTest.class)
  31. public class CreateJapaneseButton extends GenericTest {
  32.     public static final String DEST = "./target/test/resources/sandbox/acroforms/create_japanese_button.pdf";
  33.     public static final String FONT = "./src/test/resources/font/FreeSans.ttf";
  34.     public static final String JAPANESE = "\u3042\u304d\u3089";
  35.  
  36.     public static void main(String[] args) throws Exception {
  37.         File file = new File(DEST);
  38.         file.getParentFile().mkdirs();
  39.         new CreateJapaneseButton().manipulatePdf(DEST);
  40.     }
  41.  
  42.     @Override
  43.     protected void manipulatePdf(String dest) throws Exception {
  44.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  45.  
  46.         PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H);
  47.  
  48.         PdfButtonFormField pushButton = PdfFormField.createPushButton(
  49.                 pdfDoc,
  50.                 new Rectangle(36, 780, 108, 26),
  51.                 "japanese",
  52.                 JAPANESE,
  53.                 font,
  54.                 12f);
  55.  
  56.         PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
  57.         form.addField(pushButton);
  58.  
  59.         pdfDoc.close();
  60.     }
  61. }
FileSelectionExample.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/24830060/send-file-to-server-through-itext-pdf
  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.PdfName;
  17. import com.itextpdf.kernel.pdf.PdfWriter;
  18. import com.itextpdf.kernel.pdf.action.PdfAction;
  19. import com.itextpdf.test.annotations.type.SampleTest;
  20. import com.itextpdf.forms.PdfAcroForm;
  21. import com.itextpdf.forms.fields.PdfFormField;
  22. import com.itextpdf.forms.fields.PdfTextFormField;
  23. import com.itextpdf.samples.GenericTest;
  24.  
  25. import java.io.File;
  26. import java.io.FileOutputStream;
  27.  
  28. import org.junit.experimental.categories.Category;
  29.  
  30. @Category(SampleTest.class)
  31. public class FileSelectionExample extends GenericTest {
  32.     public static final String DEST = "./target/test/resources/sandbox/acroforms/file_selection_example.pdf";
  33.  
  34.     public static void main(String[] args) throws Exception {
  35.         File file = new File(DEST);
  36.         file.getParentFile().mkdirs();
  37.         new FileSelectionExample().manipulatePdf(DEST);
  38.     }
  39.  
  40.     @Override
  41.     protected void manipulatePdf(String dest) throws Exception {
  42.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  43.         PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
  44.  
  45.         PdfTextFormField field = PdfFormField.createText(pdfDoc,
  46.                 new Rectangle(36, 788, 523, 18), "myfile", "");
  47.         field.setFileSelect(true);
  48.         field.setAdditionalAction(PdfName.U, PdfAction.createJavaScript(
  49.                 "this.getField('myfile').browseForFileToSubmit();"
  50.                         + "this.getField('mytitle').setFocus();"));
  51.         form.addField(field);
  52.  
  53.         PdfTextFormField title = PdfFormField.createText(pdfDoc,
  54.                 new Rectangle(36, 752, 523, 18), "mytitle", "");
  55.         form.addField(title);
  56.  
  57.         pdfDoc.close();
  58.     }
  59. }
RadioGroupMultiPage1.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.io.font.FontConstants;
  15. import com.itextpdf.kernel.geom.Rectangle;
  16. import com.itextpdf.kernel.font.PdfFont;
  17. import com.itextpdf.kernel.font.PdfFontFactory;
  18. import com.itextpdf.kernel.pdf.PdfDocument;
  19. import com.itextpdf.kernel.pdf.PdfWriter;
  20. import com.itextpdf.layout.property.TextAlignment;
  21. import com.itextpdf.layout.property.VerticalAlignment;
  22. import com.itextpdf.test.annotations.type.SampleTest;
  23. import com.itextpdf.forms.PdfAcroForm;
  24. import com.itextpdf.forms.fields.PdfButtonFormField;
  25. import com.itextpdf.forms.fields.PdfFormField;
  26. import com.itextpdf.layout.Document;
  27. import com.itextpdf.layout.element.Paragraph;
  28. import com.itextpdf.samples.GenericTest;
  29.  
  30. import java.io.File;
  31.  
  32. import org.junit.experimental.categories.Category;
  33.  
  34. @Category(SampleTest.class)
  35. public class RadioGroupMultiPage1 extends GenericTest {
  36.     public static final String DEST = "./target/test/resources/sandbox/acroforms/radio_group_multi_page1.pdf";
  37.     public static final String[] LANGUAGES = {"English", "German", "French", "Spanish", "Dutch"};
  38.  
  39.     public static void main(String[] args) throws Exception {
  40.         File file = new File(DEST);
  41.         file.getParentFile().mkdirs();
  42.         new RadioGroupMultiPage1().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.         PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA);
  51.         Rectangle rect = new Rectangle(40, 788, 20, 18);
  52.  
  53.         PdfButtonFormField radioGroup = PdfFormField.createRadioGroup(pdfDoc, "Language", "");
  54.  
  55.         for (int page = 1; page
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. }
GenericFields.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/33247348/add-pdfpcell-to-paragraph
  11.  */
  12. package com.itextpdf.samples.sandbox.events;
  13.  
  14. import com.itextpdf.forms.PdfAcroForm;
  15. import com.itextpdf.forms.fields.PdfTextFormField;
  16. import com.itextpdf.kernel.pdf.PdfDocument;
  17. import com.itextpdf.kernel.pdf.PdfWriter;
  18. import com.itextpdf.layout.Document;
  19. import com.itextpdf.layout.element.Paragraph;
  20. import com.itextpdf.layout.element.Text;
  21. import com.itextpdf.layout.renderer.DrawContext;
  22. import com.itextpdf.layout.renderer.TextRenderer;
  23. import com.itextpdf.samples.GenericTest;
  24. import com.itextpdf.test.annotations.type.SampleTest;
  25.  
  26. import java.io.File;
  27.  
  28. import org.junit.experimental.categories.Category;
  29.  
  30. @Category(SampleTest.class)
  31. public class GenericFields extends GenericTest {
  32.     public static final String DEST = "./target/test/resources/sandbox/events/generic_fields.pdf";
  33.  
  34.     public static void main(String[] args) throws Exception {
  35.         File file = new File(DEST);
  36.         file.getParentFile().mkdirs();
  37.         new GenericFields().manipulatePdf(DEST);
  38.     }
  39.  
  40.     @Override
  41.     protected void manipulatePdf(String dest) throws Exception {
  42.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  43.         Document doc = new Document(pdfDoc);
  44.         Paragraph p = new Paragraph();
  45.         p.add("The Effective Date is ");
  46.         Text day = new Text("     ");
  47.         day.setNextRenderer(new FieldTextRenderer(day, "day"));
  48.         p.add(day);
  49.         p.add(" day of ");
  50.         Text month = new Text("     ");
  51.         month.setNextRenderer(new FieldTextRenderer(month, "month"));
  52.         p.add(month);
  53.         p.add(", ");
  54.         Text year = new Text("            ");
  55.         year.setNextRenderer(new FieldTextRenderer(year, "year"));
  56.         p.add(year);
  57.         p.add(" that this will begin.");
  58.         doc.add(p);
  59.         doc.close();
  60.     }
  61.  
  62.  
  63.     protected class FieldTextRenderer extends TextRenderer {
  64.         protected String fieldName;
  65.  
  66.         public FieldTextRenderer(Text textElement, String fieldName) {
  67.             super(textElement);
  68.             this.fieldName = fieldName;
  69.         }
  70.  
  71.         @Override
  72.         public void draw(DrawContext drawContext) {
  73.             PdfTextFormField field = PdfTextFormField.createText(drawContext.getDocument(), getOccupiedAreaBBox(), fieldName);
  74.             PdfAcroForm.getAcroForm(drawContext.getDocument(), true).addField(field);
  75.         }
  76.     }
  77. }
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