Creating form fields

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

6th November 2015
admin-marketing

Switch code for this example

CreateJapaneseButton.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/29016333/pushbuttonfield-with-double-byte-character
  4.  */
  5. package sandbox.acroforms;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Rectangle;
  10. import com.itextpdf.text.pdf.BaseFont;
  11. import com.itextpdf.text.pdf.PdfWriter;
  12. import com.itextpdf.text.pdf.PushbuttonField;
  13.  
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;
  18.  
  19. @WrapToTest
  20. public class CreateJapaneseButton {
  21.    
  22.     public static final String DEST = "results/acroforms/japanese_button.pdf";
  23.     public static final String JAPANESE = "\u3042\u304d\u3089";
  24.     public static final String FONT = "resources/fonts/FreeSans.ttf";
  25.  
  26.     public static void main(String[] args) throws IOException,
  27.             DocumentException {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new CreateJapaneseButton().createPdf(DEST);
  31.     }
  32.    
  33.     public void createPdf(String dest) throws IOException, DocumentException {
  34.         Document document = new Document();
  35.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  36.         document.open();
  37.         PushbuttonField button = new PushbuttonField(writer, new Rectangle(36, 780, 144, 806), "japanese");
  38.         BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  39.         button.setFont(bf);
  40.         button.setText(JAPANESE);
  41.         writer.addAnnotation(button.getField());
  42.         document.close();
  43.     }
  44.  
  45. }
FileSelectionExample.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/24830060/send-file-to-server-through-itext-pdf
  4.  */
  5.  
  6. package sandbox.acroforms;
  7.  
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Rectangle;
  11. import com.itextpdf.text.pdf.PdfAction;
  12. import com.itextpdf.text.pdf.PdfFormField;
  13. import com.itextpdf.text.pdf.PdfName;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15. import com.itextpdf.text.pdf.TextField;
  16.  
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import sandbox.WrapToTest;
  21.  
  22. @WrapToTest
  23. public class FileSelectionExample {
  24.    
  25.     public static final String DEST = "results/acroforms/file_selection.pdf";
  26.    
  27.     public static void main(String[] args) throws IOException, DocumentException {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new FileSelectionExample().createPdf(DEST);
  31.     }
  32.    
  33.     public void createPdf(String dest) throws IOException, DocumentException {
  34.         Document document = new Document();
  35.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  36.         document.open();
  37.         TextField file = new TextField(writer, new Rectangle(36, 788, 559, 806), "myfile");
  38.         file.setOptions(TextField.FILE_SELECTION);
  39.         PdfFormField upload = file.getTextField();
  40.         upload.setAdditionalActions(PdfName.U,
  41.                 PdfAction.javaScript(
  42.                     "this.getField('myfile').browseForFileToSubmit();"
  43.                     + "this.getField('mytitle').setFocus();",
  44.                     writer));
  45.         writer.addAnnotation(upload);
  46.         TextField title = new TextField(writer, new Rectangle(36, 752, 559, 770), "mytitle");
  47.         writer.addAnnotation(title.getTextField());
  48.         document.close();
  49.     }
  50. }
RadioGroupMultiPage1.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.Element;
  10. import com.itextpdf.text.Rectangle;
  11. import com.itextpdf.text.pdf.BaseFont;
  12. import com.itextpdf.text.pdf.GrayColor;
  13. import com.itextpdf.text.pdf.PdfContentByte;
  14. import com.itextpdf.text.pdf.PdfFormField;
  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. /**
  23.  * @author Bruno Lowagie (iText Software)
  24.  */
  25. public class RadioGroupMultiPage1 {
  26.    
  27.     public static final String DEST = "results/acroforms/multipage_radiobutton1.pdf";
  28.     /** Possible values of a Choice field. */
  29.     public static final String[] LANGUAGES = { "English", "German", "French", "Spanish", "Dutch" };
  30.  
  31.     public static void main(String[] args) throws DocumentException, IOException {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new RadioGroupMultiPage1().createPdf(DEST);
  35.     }
  36.     public void createPdf(String dest) throws IOException, DocumentException {
  37.         // step 1
  38.         Document document = new Document();
  39.         // step 2
  40.         PdfWriter writer =
  41.             PdfWriter.getInstance(document, new FileOutputStream(dest));
  42.         // step 3
  43.         document.open();
  44.         // step 4
  45.         PdfContentByte cb = writer.getDirectContent();
  46.         BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
  47.         // create a radio field spanning different pages
  48.         PdfFormField radiogroup = PdfFormField.createRadioButton(writer, true);
  49.         radiogroup.setFieldName("language");
  50.         Rectangle rect = new Rectangle(40, 806, 60, 788);
  51.         RadioCheckField radio;
  52.         PdfFormField radiofield;
  53.         for (int page = 0; page < LANGUAGES.length; ) {
  54.             radio = new RadioCheckField(writer, rect, null, LANGUAGES[page]);
  55.             radio.setBackgroundColor(new GrayColor(0.8f));
  56.             radiofield = radio.getRadioField();
  57.             radiofield.setPlaceInPage(++page);
  58.             radiogroup.addKid(radiofield);
  59.         }
  60.         writer.addAnnotation(radiogroup);
  61.         // add the content
  62.         for (int i = 0; i < LANGUAGES.length; i++) {
  63.             cb.beginText();
  64.             cb.setFontAndSize(bf, 18);
  65.             cb.showTextAligned(Element.ALIGN_LEFT, LANGUAGES[i], 70, 790, 0);
  66.             cb.endText();
  67.             document.newPage();
  68.         }
  69.         // step 5
  70.         document.close();
  71.     }
  72. }
GenericFields.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/33247348/add-pdfpcell-to-paragraph
  4.  */
  5. package sandbox.events;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.ExceptionConverter;
  11. import com.itextpdf.text.Paragraph;
  12. import com.itextpdf.text.Rectangle;
  13. import com.itextpdf.text.pdf.PdfPageEventHelper;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15. import com.itextpdf.text.pdf.TextField;
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import sandbox.WrapToTest;
  20.  
  21. /**
  22.  * @author Bruno Lowagie (iText Software)
  23.  */
  24. @WrapToTest
  25. public class GenericFields {
  26.     public static final String DEST = "results/events/generic_fields.pdf";
  27.    
  28.     public class FieldChunk extends PdfPageEventHelper {
  29.         @Override
  30.         public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
  31.             TextField field = new TextField(writer, rect, text);
  32.             try {
  33.                 writer.addAnnotation(field.getTextField());
  34.             } catch (IOException ex) {
  35.                 throw new ExceptionConverter(ex);
  36.             } catch (DocumentException ex) {
  37.                 throw new ExceptionConverter(ex);
  38.             }
  39.         }
  40.     }
  41.    
  42.     public static void main(String[] args) throws IOException, DocumentException {
  43.         File file = new File(DEST);
  44.         file.getParentFile().mkdirs();
  45.         new GenericFields().createPdf(DEST);
  46.     }
  47.    
  48.     public void createPdf(String dest) throws IOException, DocumentException {
  49.         Document document = new Document();
  50.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  51.         writer.setPageEvent(new FieldChunk());
  52.         document.open();
  53.         Paragraph p = new Paragraph();
  54.         p.add("The Effective Date is ");
  55.         Chunk day = new Chunk("     ");
  56.         day.setGenericTag("day");
  57.         p.add(day);
  58.         p.add(" day of ");
  59.         Chunk month = new Chunk("     ");
  60.         month.setGenericTag("month");
  61.         p.add(month);
  62.         p.add(", ");
  63.         Chunk year = new Chunk("            ");
  64.         year.setGenericTag("year");
  65.         p.add(year);
  66.         p.add(" that this will begin.");
  67.         document.add(p);
  68.         document.close();
  69.     }
  70. }
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