Filling out forms

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

31st May 2016
iText PDF

Switch code for this example

FillFormSpecialChars.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/20401125/overlapping-characters-in-text-field-itext-pdf
  11.  *
  12.  * Sometimes you need to change the font of a field.
  13.  */
  14. package com.itextpdf.samples.sandbox.acroforms;
  15.  
  16. import com.itextpdf.forms.PdfAcroForm;
  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.pdf.PdfDocument;
  21. import com.itextpdf.kernel.pdf.PdfReader;
  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.  
  31. @Category(SampleTest.class)
  32. public class FillFormSpecialChars extends GenericTest {
  33.     public static final String DEST = "./target/test/resources/sandbox/acroforms/fill_form_special_chars.pdf";
  34.     public static final String FONT = "./src/test/resources/font/FreeSans.ttf";
  35.     public static final String SRC = "./src/test/resources/pdfs/test.pdf";
  36.     public static final String VALUE = "\u011b\u0161\u010d\u0159\u017e\u00fd\u00e1\u00ed\u00e9";
  37.  
  38.     public static void main(String[] args) throws Exception {
  39.         File file = new File(DEST);
  40.         file.getParentFile().mkdirs();
  41.         new FillFormSpecialChars().manipulatePdf(DEST);
  42.     }
  43.  
  44.     @Override
  45.     protected void manipulatePdf(String dest) throws Exception {
  46.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  47.  
  48.         PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
  49.         form.setGenerateAppearance(true);
  50.  
  51.         PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H);
  52.  
  53.         form.getField("test").setValue(VALUE, font, 12f);
  54.         form.getField("test2").setValue(VALUE, font, 12f);
  55.  
  56.         pdfDoc.close();
  57.     }
  58. }
FillFormSpecialChars2.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/31335715/cannot-show-special-character-in-acro-form-field
  11.  *
  12.  * Sometimes you need to change the font of a field.
  13.  */
  14. package com.itextpdf.samples.sandbox.acroforms;
  15.  
  16. import com.itextpdf.forms.PdfAcroForm;
  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.pdf.PdfDocument;
  21. import com.itextpdf.kernel.pdf.PdfReader;
  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.  
  31. @Category(SampleTest.class)
  32. public class FillFormSpecialChars2 extends GenericTest {
  33.     public static final String DEST = "./target/test/resources/sandbox/acroforms/fill_form_special_chars2.pdf";
  34.     public static final String FONT = "./src/test/resources/font/FreeSans.ttf";
  35.     public static final String SRC = "./src/test/resources/pdfs/form.pdf";
  36.  
  37.     public static void main(String[] args) throws Exception {
  38.         File file = new File(DEST);
  39.         file.getParentFile().mkdirs();
  40.         new FillFormSpecialChars2().manipulatePdf(DEST);
  41.     }
  42.  
  43.     @Override
  44.     protected void manipulatePdf(String dest) throws Exception {
  45.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  46.  
  47.         PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
  48.         form.setGenerateAppearance(true);
  49.  
  50.         PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H);
  51.         form.getField("Name").setValue("\u04e711111", font, 12f);
  52.  
  53.         form.flattenFields();
  54.         pdfDoc.close();
  55.     }
  56. }
FillHybridForm.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/29702986/how-to-check-a-checkbox-in-pdf-file-with-the-same-variable-name-with-itext-and-j
  11.  */
  12. package com.itextpdf.samples.sandbox.acroforms;
  13.  
  14. import com.itextpdf.forms.PdfAcroForm;
  15. import com.itextpdf.kernel.pdf.PdfDocument;
  16. import com.itextpdf.kernel.pdf.PdfReader;
  17. import com.itextpdf.kernel.pdf.PdfWriter;
  18. import com.itextpdf.samples.GenericTest;
  19. import com.itextpdf.test.annotations.type.SampleTest;
  20.  
  21. import org.junit.experimental.categories.Category;
  22.  
  23. import java.io.File;
  24. import java.io.FileInputStream;
  25. import java.io.FileOutputStream;
  26.  
  27. @Category(SampleTest.class)
  28. public class FillHybridForm extends GenericTest {
  29.     public static final String DEST = "./target/test/resources/sandbox/acroforms/fill_hybrid_form.pdf";
  30.     public static final String SRC = "./src/test/resources/pdfs/f8966.pdf";
  31.  
  32.     public static void main(String[] args) throws Exception {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new FillHybridForm().manipulatePdf(DEST);
  36.     }
  37.  
  38.     @Override
  39.     protected void manipulatePdf(String dest) throws Exception {
  40.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  41.  
  42.         PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
  43.         form.removeXfaForm();
  44.  
  45.         form.getField("topmostSubform[0].Page2[0].p2_cb01[0]").setValue("1");
  46.         form.getField("topmostSubform[0].Page2[0].p2_cb01[1]").setValue("2");
  47.         form.getField("topmostSubform[0].Page2[0].p2_cb01[2]").setValue("3");
  48.         form.getField("topmostSubform[0].Page2[0].p2_cb01[3]").setValue("4");
  49.         form.getField("topmostSubform[0].Page2[0].p2_cb01[4]").setValue("5");
  50.         form.getField("topmostSubform[0].Page2[0].p2_cb01[5]").setValue("6");
  51.  
  52.         pdfDoc.close();
  53.     }
  54. }
FormatFields.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 the following question:
  10.  * http://stackoverflow.com/questions/27711035/itextsharp-acrofields-format-as-number
  11.  */
  12. package com.itextpdf.samples.sandbox.acroforms;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfReader;
  16. import com.itextpdf.kernel.pdf.PdfWriter;
  17. import com.itextpdf.test.annotations.type.SampleTest;
  18. import com.itextpdf.forms.PdfAcroForm;
  19. import com.itextpdf.samples.GenericTest;
  20.  
  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.io.FileOutputStream;
  24.  
  25. import org.junit.experimental.categories.Category;
  26.  
  27. @Category(SampleTest.class)
  28. public class FormatFields extends GenericTest {
  29.     public static final String DEST = "./target/test/resources/sandbox/acroforms/format_fields.pdf";
  30.     public static final String SRC = "./src/test/resources/pdfs/form.pdf";
  31.  
  32.     public static void main(String[] args) throws Exception {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new FormatFields().manipulatePdf(DEST);
  36.     }
  37.  
  38.     @Override
  39.     protected void manipulatePdf(String dest) throws Exception {
  40.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  41.         PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
  42.         form.getField("Name").setValue("1.0", "100%");
  43.         form.getField("Company").setValue("1217000.000000", "$1,217,000");
  44.         pdfDoc.close();
  45.     }
  46. }
MultiLineField.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/30233783/alignment-issue-with-multiline-text-form-fields-in-itextsharp
  11.  */
  12. package com.itextpdf.samples.sandbox.acroforms;
  13.  
  14. import com.itextpdf.kernel.geom.Rectangle;
  15. import com.itextpdf.io.source.ByteArrayOutputStream;
  16. import com.itextpdf.io.source.RandomAccessSourceFactory;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfReader;
  19. import com.itextpdf.kernel.pdf.PdfWriter;
  20. import com.itextpdf.forms.PdfAcroForm;
  21. import com.itextpdf.forms.fields.PdfFormField;
  22. import com.itextpdf.forms.fields.PdfTextFormField;
  23. import com.itextpdf.kernel.pdf.ReaderProperties;
  24. import com.itextpdf.samples.GenericTest;
  25. import com.itextpdf.test.annotations.type.SampleTest;
  26.  
  27. import java.io.File;
  28. import java.io.IOException;
  29.  
  30. import org.junit.experimental.categories.Category;
  31.  
  32. @Category(SampleTest.class)
  33. public class MultiLineField extends GenericTest {
  34.     public static final String DEST = "./target/test/resources/sandbox/acroforms/multi_line_field.pdf";
  35.     public static final String SRC = "./src/test/resources/pdfs/multinewline.pdf";
  36.  
  37.     public static void main(String[] args) throws Exception {
  38.         File file = new File(DEST);
  39.         file.getParentFile().mkdirs();
  40.         new MultiLineField().manipulatePdf(DEST);
  41.     }
  42.  
  43.     @Override
  44.     protected void manipulatePdf(String dest) throws Exception {
  45.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(new RandomAccessSourceFactory().createSource(createForm()),
  46.                 new ReaderProperties()), new PdfWriter(DEST));
  47.         PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
  48.         form.getField("text").setValue("A B C D E F\nG H I J K L M N\nO P Q R S T U\r\nV W X Y Z\n\nAlphabet street");
  49.         form.flattenFields();
  50.         pdfDoc.close();
  51.     }
  52.  
  53.     public byte[] createForm() throws IOException {
  54.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  55.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(baos));
  56.  
  57.         Rectangle rect = new Rectangle(36, 720, 108, 86);
  58.         PdfTextFormField tf = PdfFormField.createText(pdfDoc, rect, "text", "text");
  59.         tf.setMultiline(true);
  60.  
  61.         PdfAcroForm.getAcroForm(pdfDoc, true).addField(tf);
  62.  
  63.         pdfDoc.close();
  64.         return baos.toByteArray();
  65.     }
  66. }
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