Multiline fields

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

31st May 2016
iText PDF

Switch code for this example

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. }
ReadOnlyField.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. package com.itextpdf.samples.sandbox.acroforms;
  9.  
  10. import com.itextpdf.kernel.geom.Rectangle;
  11. import com.itextpdf.io.source.ByteArrayOutputStream;
  12. import com.itextpdf.io.source.RandomAccessSourceFactory;
  13. import com.itextpdf.kernel.font.PdfFontFactory;
  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.kernel.pdf.ReaderProperties;
  18. import com.itextpdf.test.annotations.type.SampleTest;
  19. import com.itextpdf.forms.PdfAcroForm;
  20. import com.itextpdf.forms.fields.PdfFormField;
  21. import com.itextpdf.forms.fields.PdfTextFormField;
  22. import com.itextpdf.samples.GenericTest;
  23.  
  24. import java.io.File;
  25. import java.io.IOException;
  26.  
  27. import org.junit.experimental.categories.Category;
  28.  
  29. @Category(SampleTest.class)
  30. public class ReadOnlyField extends GenericTest {
  31.     public static final String DEST = "./target/test/resources/sandbox/acroforms/read_only_field.pdf";
  32.  
  33.     public static void main(String[] args) throws Exception {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new ReadOnlyField().manipulatePdf(DEST);
  37.     }
  38.  
  39.     @Override
  40.     protected void manipulatePdf(String dest) throws Exception {
  41.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(new RandomAccessSourceFactory().createSource(createForm()),
  42.                 new ReaderProperties()), new PdfWriter(DEST));
  43.         PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
  44.         form.getField("text").setReadOnly(true).setValue("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z");
  45.         pdfDoc.close();
  46.     }
  47.  
  48.     public byte[] createForm() throws IOException {
  49.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  50.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(baos));
  51.  
  52.         Rectangle rect = new Rectangle(36, 770, 104, 36);
  53.         PdfTextFormField tf = PdfFormField.createText(pdfDoc, rect, "text", "text", PdfFontFactory.createFont(), 20f);
  54.         tf.setMultiline(true);
  55.  
  56.         PdfAcroForm.getAcroForm(pdfDoc, true).addField(tf);
  57.  
  58.         pdfDoc.close();
  59.         return baos.toByteArray();
  60.     }
  61. }
ReadOnlyField2.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. package com.itextpdf.samples.sandbox.acroforms;
  9.  
  10. import com.itextpdf.kernel.geom.Rectangle;
  11. import com.itextpdf.io.source.ByteArrayOutputStream;
  12. import com.itextpdf.io.source.RandomAccessSourceFactory;
  13. import com.itextpdf.kernel.font.PdfFontFactory;
  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.kernel.pdf.ReaderProperties;
  18. import com.itextpdf.test.annotations.type.SampleTest;
  19. import com.itextpdf.forms.PdfAcroForm;
  20. import com.itextpdf.forms.fields.PdfFormField;
  21. import com.itextpdf.forms.fields.PdfTextFormField;
  22. import com.itextpdf.samples.GenericTest;
  23.  
  24. import java.io.File;
  25. import java.io.IOException;
  26.  
  27. import org.junit.experimental.categories.Category;
  28.  
  29. @Category(SampleTest.class)
  30. public class ReadOnlyField2 extends GenericTest {
  31.     public static final String DEST = "./target/test/resources/sandbox/acroforms/read_only_field2.pdf";
  32.  
  33.     public static void main(String[] args) throws Exception {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new ReadOnlyField2().manipulatePdf(DEST);
  37.     }
  38.  
  39.     @Override
  40.     protected void manipulatePdf(String dest) throws Exception {
  41.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(new RandomAccessSourceFactory().createSource(createForm()),
  42.                 new ReaderProperties()), new PdfWriter(DEST));
  43.         PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
  44.         form.getField("text1").setReadOnly(true).setValue("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z");
  45.         form.getField("text2").setReadOnly(true).setValue("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z");
  46.         form.getField("text3").setReadOnly(true).setValue("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z");
  47.         form.getField("text4").setReadOnly(true).setValue("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z");
  48.         pdfDoc.close();
  49.     }
  50.  
  51.     public byte[] createForm() throws IOException {
  52.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  53.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(baos));
  54.         PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
  55.  
  56.         Rectangle rect = new Rectangle(36, 770, 108, 36);
  57.         PdfTextFormField tf1 = PdfFormField.createText(pdfDoc, rect,
  58.                 "text1", "text1", PdfFontFactory.createFont(), 18f);
  59.         tf1.setMultiline(true);
  60.         form.addField(tf1);
  61.  
  62.         rect = new Rectangle(148, 770, 108, 36);
  63.         PdfTextFormField tf2 = PdfFormField.createText(pdfDoc, rect,
  64.                 "text2", "text2", PdfFontFactory.createFont(), 18f);
  65.         tf2.setMultiline(true);
  66.         form.addField(tf2);
  67.  
  68.         rect = new Rectangle(36, 724, 108, 36);
  69.         PdfTextFormField tf3 = PdfFormField.createText(pdfDoc, rect,
  70.                 "text3", "text3", PdfFontFactory.createFont(), 18f);
  71.         tf3.setMultiline(true);
  72.         form.addField(tf3);
  73.  
  74.         rect = new Rectangle(148, 727, 108, 33);
  75.         PdfTextFormField tf4 = PdfFormField.createText(pdfDoc, rect,
  76.                 "text4", "text4", PdfFontFactory.createFont(), 18f);
  77.         tf4.setMultiline(true);
  78.         form.addField(tf4);
  79.  
  80.         pdfDoc.close();
  81.         return baos.toByteArray();
  82.     }
  83. }
ReadOnlyField3.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. package com.itextpdf.samples.sandbox.acroforms;
  9.  
  10. import com.itextpdf.kernel.geom.Rectangle;
  11. import com.itextpdf.io.source.ByteArrayOutputStream;
  12. import com.itextpdf.io.source.RandomAccessSourceFactory;
  13. import com.itextpdf.kernel.font.PdfFontFactory;
  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.kernel.pdf.ReaderProperties;
  18. import com.itextpdf.test.annotations.type.SampleTest;
  19. import com.itextpdf.forms.PdfAcroForm;
  20. import com.itextpdf.forms.fields.PdfFormField;
  21. import com.itextpdf.forms.fields.PdfTextFormField;
  22. import com.itextpdf.samples.GenericTest;
  23.  
  24. import java.io.File;
  25. import java.io.IOException;
  26.  
  27. import org.junit.experimental.categories.Category;
  28.  
  29. @Category(SampleTest.class)
  30. public class ReadOnlyField3 extends GenericTest {
  31.     public static final String DEST = "./target/test/resources/sandbox/acroforms/read_only_field3.pdf";
  32.  
  33.     public static void main(String[] args) throws Exception {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new ReadOnlyField3().manipulatePdf(DEST);
  37.     }
  38.  
  39.     @Override
  40.     protected void manipulatePdf(String dest) throws Exception {
  41.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(new RandomAccessSourceFactory().createSource(createForm()),
  42.                 new ReaderProperties()), new PdfWriter(DEST));
  43.         PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
  44.         form.getField("text1").setReadOnly(true).setValue("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z");
  45.         form.getField("text2").setReadOnly(true).setValue("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z");
  46.         form.getField("text3").setReadOnly(true).setValue("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z");
  47.         form.getField("text4").setReadOnly(true).setValue("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z");
  48.         pdfDoc.close();
  49.     }
  50.  
  51.     public byte[] createForm() throws IOException {
  52.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  53.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(baos));
  54.         PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
  55.  
  56.         Rectangle rect = new Rectangle(36, 770, 108, 36);
  57.         PdfTextFormField tf1 = PdfFormField.createText(pdfDoc, rect,
  58.                 "text1", "text1", PdfFontFactory.createFont(), 18f);
  59.         tf1.setMultiline(true);
  60.         form.addField(tf1);
  61.  
  62.         rect = new Rectangle(148, 770, 108, 36);
  63.         PdfTextFormField tf2 = PdfFormField.createText(pdfDoc, rect,
  64.                 "text2", "text2", PdfFontFactory.createFont(), 18f);
  65.         tf2.setMultiline(true);
  66.         form.addField(tf2);
  67.  
  68.         rect = new Rectangle(36, 724, 108, 36);
  69.         PdfTextFormField tf3 = PdfFormField.createText(pdfDoc, rect,
  70.                 "text3", "text3", PdfFontFactory.createFont(), 18f);
  71.         tf3.setMultiline(true);
  72.         form.addField(tf3);
  73.  
  74.         rect = new Rectangle(148, 727, 108, 33);
  75.         PdfTextFormField tf4 = PdfFormField.createText(pdfDoc, rect,
  76.                 "text4", "text4", PdfFontFactory.createFont(), 18f);
  77.         tf4.setMultiline(true);
  78.         form.addField(tf4);
  79.  
  80.         pdfDoc.close();
  81.         return baos.toByteArray();
  82.     }
  83. }
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