Multiline fields

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

7th November 2015
admin-marketing

Switch code for this example

MultiLineField.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to
  3.  * http://stackoverflow.com/questions/30233783/alignment-issue-with-multiline-text-form-fields-in-itextsharp
  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.AcroFields;
  11. import com.itextpdf.text.pdf.PdfReader;
  12. import com.itextpdf.text.pdf.PdfStamper;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14. import com.itextpdf.text.pdf.TextField;
  15.  
  16. import java.io.ByteArrayOutputStream;
  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 MultiLineField {
  24.    
  25.     public static final String DEST = "results/acroforms/multinewline.pdf";
  26.    
  27.     public static void main(String[] args) throws IOException, DocumentException {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new MultiLineField().createPdf(DEST);
  31.     }
  32.        
  33.     public void createPdf(String filename) throws DocumentException, IOException {
  34.         PdfReader reader = new PdfReader(createForm());
  35.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(filename));
  36.         AcroFields form = stamper.getAcroFields();
  37.         form.setField("text", "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");
  38.         stamper.setFormFlattening(true);
  39.         stamper.close();
  40.     }
  41.    
  42.     public byte[] createForm() throws DocumentException, IOException {
  43.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  44.         Document document = new Document();
  45.         PdfWriter writer = PdfWriter.getInstance(document, baos);
  46.         document.open();
  47.         Rectangle rect = new Rectangle(36, 720, 144, 806);
  48.         TextField tf = new TextField(writer, rect, "text");
  49.         tf.setOptions(TextField.MULTILINE);
  50.         writer.addAnnotation(tf.getTextField());
  51.         document.close();
  52.         return baos.toByteArray();
  53.     }
  54. }
MultiLineFieldCount.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to
  3.  * http://stackoverflow.com/questions/33808187/get-row-count-of-a-multiline-acrofiled
  4.  */
  5. package sandbox.acroforms;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.Rectangle;
  9. import com.itextpdf.text.pdf.AcroFields;
  10. import com.itextpdf.text.pdf.BaseFont;
  11. import com.itextpdf.text.pdf.PRIndirectReference;
  12. import com.itextpdf.text.pdf.PdfDictionary;
  13. import com.itextpdf.text.pdf.PdfName;
  14. import com.itextpdf.text.pdf.PdfReader;
  15. import com.itextpdf.text.pdf.PdfStamper;
  16. import com.itextpdf.text.pdf.PdfString;
  17.  
  18. import java.io.File;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import sandbox.WrapToTest;
  22.  
  23. @WrapToTest
  24. public class MultiLineFieldCount {
  25.    
  26.     public static final String SRC = "resources/pdfs/multiline.pdf";
  27.     public static final String DEST = "results/acroforms/multiline.pdf";
  28.    
  29.     public static void main(String[] args) throws IOException, DocumentException {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new MultiLineFieldCount().manipulatePdf(SRC, DEST);
  33.     }
  34.        
  35.     public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
  36.         PdfReader reader = new PdfReader(src);
  37.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  38.         AcroFields form = stamper.getAcroFields();
  39.         StringBuilder sb = new StringBuilder();
  40.         for (String name : form.getFields().keySet()) {
  41.             int n = getInformation(form, name);
  42.             for (int i = 0; i < n; i++) {
  43.                 sb.append(" *");
  44.             }
  45.             String filler = sb.toString();
  46.             form.setField(name, name + filler);
  47.         }
  48.         stamper.setFormFlattening(true);
  49.         stamper.close();
  50.         reader.close();
  51.     }
  52.    
  53.     public int getInformation(AcroFields form, String name) {
  54.         form.getFieldItem(name);
  55.         AcroFields.Item item = form.getFieldItem(name);
  56.         PdfDictionary dict = item.getMerged(0);
  57.         PdfString da = dict.getAsString(PdfName.DA);
  58.         Object[] da_values = AcroFields.splitDAelements(da.toUnicodeString());
  59.         if (da_values == null) {
  60.             System.out.println("No default appearance");
  61.         }
  62.         BaseFont bf = null;
  63.         String font = (String)da_values[AcroFields.DA_FONT];
  64.         if (font != null) {
  65.             PdfDictionary dr = dict.getAsDict(PdfName.DR);
  66.             if (dr != null) {
  67.                 PdfDictionary fontDict = dr.getAsDict(PdfName.FONT);
  68.                 bf = BaseFont.createFont((PRIndirectReference)fontDict.get(new PdfName(font)));
  69.             }
  70.         }
  71.         if (bf == null) {
  72.             System.out.println("No BaseFont");
  73.         }
  74.         else {
  75.             System.out.println("Basefont: " + bf.getPostscriptFontName());
  76.             System.out.println("Size: " + da_values[AcroFields.DA_SIZE]);
  77.             Float size = (Float)da_values[AcroFields.DA_SIZE];
  78.             if (size == 0)
  79.                 return 1000;
  80.             Rectangle rect = form.getFieldPositions(name).get(0).position;
  81.             float factor = bf.getFontDescriptor(BaseFont.BBOXURY, 1) - bf.getFontDescriptor(BaseFont.BBOXLLY, 1);
  82.             int rows = Math.round(rect.getHeight() / (size * factor) + 0.5f);
  83.             int columns = Math.round(rect.getWidth() / bf.getWidthPoint(" *", size) + 0.5f);
  84.             System.out.println("height: " + rect.getHeight() + "; width: " + rect.getWidth());
  85.             System.out.println("rows: " + rows + "; columns: " + columns);
  86.             return rows * columns;
  87.         }
  88.         return 1000;
  89.     }
  90. }
ReadOnlyField.java
  1. /*
  2.  * Example written by Bruno Lowagie.
  3.  */
  4. package sandbox.acroforms;
  5.  
  6. import com.itextpdf.text.Document;
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.Rectangle;
  9. import com.itextpdf.text.pdf.AcroFields;
  10. import com.itextpdf.text.pdf.PdfReader;
  11. import com.itextpdf.text.pdf.PdfStamper;
  12. import com.itextpdf.text.pdf.PdfWriter;
  13. import com.itextpdf.text.pdf.TextField;
  14.  
  15. import java.io.ByteArrayOutputStream;
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import sandbox.WrapToTest;
  20.  
  21. @WrapToTest
  22. public class ReadOnlyField {
  23.    
  24.     public static final String DEST = "results/acroforms/multilineField.pdf";
  25.    
  26.     public static void main(String[] args) throws IOException, DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new ReadOnlyField().createPdf(DEST);
  30.     }
  31.        
  32.     public void createPdf(String filename) throws DocumentException, IOException {
  33.         PdfReader reader = new PdfReader(createForm());
  34.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(filename));
  35.         AcroFields form = stamper.getAcroFields();
  36.         form.setFieldProperty("text", "setfflags", TextField.READ_ONLY, null);
  37.         form.setField("text", "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");
  38.         stamper.close();
  39.     }
  40.    
  41.     public byte[] createForm() throws DocumentException, IOException {
  42.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  43.         Document document = new Document();
  44.         PdfWriter writer = PdfWriter.getInstance(document, baos);
  45.         document.open();
  46.         Rectangle rect = new Rectangle(36, 770, 144, 806);
  47.         TextField tf = new TextField(writer, rect, "text");
  48.         tf.setFontSize(20);
  49.         tf.setOptions(TextField.MULTILINE);
  50.         writer.addAnnotation(tf.getTextField());
  51.         document.close();
  52.         return baos.toByteArray();
  53.     }
  54. }
ReadOnlyField2.java
  1. /*
  2.  * Example written by Bruno Lowagie.
  3.  */
  4. package sandbox.acroforms;
  5.  
  6. import com.itextpdf.text.Document;
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.Rectangle;
  9. import com.itextpdf.text.pdf.AcroFields;
  10. import com.itextpdf.text.pdf.PdfFormField;
  11. import com.itextpdf.text.pdf.PdfReader;
  12. import com.itextpdf.text.pdf.PdfStamper;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14. import com.itextpdf.text.pdf.TextField;
  15.  
  16. import java.io.ByteArrayOutputStream;
  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 ReadOnlyField2 {
  24.    
  25.     public static final String DEST = "results/acroforms/multilineField2.pdf";
  26.    
  27.     public static void main(String[] args) throws IOException, DocumentException {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new ReadOnlyField2().createPdf(DEST);
  31.     }
  32.        
  33.     public void createPdf(String filename) throws DocumentException, IOException {
  34.         PdfReader reader = new PdfReader(createForm());
  35.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(filename));
  36.         AcroFields form = stamper.getAcroFields();
  37.         form.setFieldProperty("text1", "setfflags", TextField.READ_ONLY, null);
  38.         form.setField("text1", "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");
  39.         form.setFieldProperty("text2", "setfflags", TextField.READ_ONLY, null);
  40.         form.setField("text2", "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");
  41.         form.setFieldProperty("text3", "setfflags", TextField.READ_ONLY, null);
  42.         form.setField("text3", "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");
  43.         form.setFieldProperty("text4", "setfflags", TextField.READ_ONLY, null);
  44.         form.setField("text4", "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.         stamper.close();
  46.     }
  47.    
  48.     public byte[] createForm() throws DocumentException, IOException {
  49.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  50.         Document document = new Document();
  51.         PdfWriter writer = PdfWriter.getInstance(document, baos);
  52.         document.open();
  53.         Rectangle rect = new Rectangle(36, 770, 144, 806);
  54.         writer.addAnnotation(getTextField(writer, rect, "text1"));
  55.         rect = new Rectangle(148, 770, 256, 806);
  56.         writer.addAnnotation(getTextField(writer, rect, "text2"));
  57.         rect = new Rectangle(36, 724, 144, 760);
  58.         writer.addAnnotation(getTextField(writer, rect, "text3"));
  59.         rect = new Rectangle(148, 727, 256, 760);
  60.         writer.addAnnotation(getTextField(writer, rect, "text4"));
  61.         document.close();
  62.         return baos.toByteArray();
  63.     }
  64.  
  65.     public PdfFormField getTextField(PdfWriter writer, Rectangle rect, String name) throws IOException, DocumentException {
  66.         TextField tf = new TextField(writer, rect, name);
  67.         tf.setFontSize(16);
  68.         tf.setOptions(TextField.MULTILINE);
  69.         return tf.getTextField();
  70.     }
  71. }
ReadOnlyField3.java
  1. /*
  2.  * Example written by Bruno Lowagie.
  3.  */
  4. package sandbox.acroforms;
  5.  
  6. import com.itextpdf.text.Document;
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.Rectangle;
  9. import com.itextpdf.text.pdf.AcroFields;
  10. import com.itextpdf.text.pdf.PdfFormField;
  11. import com.itextpdf.text.pdf.PdfReader;
  12. import com.itextpdf.text.pdf.PdfStamper;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14. import com.itextpdf.text.pdf.TextField;
  15.  
  16. import java.io.ByteArrayOutputStream;
  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 ReadOnlyField3 {
  24.    
  25.     public static final String DEST = "results/acroforms/multilineField3.pdf";
  26.    
  27.     public static void main(String[] args) throws IOException, DocumentException {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new ReadOnlyField3().createPdf(DEST);
  31.     }
  32.        
  33.     public void createPdf(String filename) throws DocumentException, IOException {
  34.         PdfReader reader = new PdfReader(createForm());
  35.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(filename));
  36.         AcroFields form = stamper.getAcroFields();
  37.         form.setGenerateAppearances(false);
  38.         form.setFieldProperty("text1", "setfflags", TextField.READ_ONLY, null);
  39.         form.setField("text1", "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");
  40.         form.setFieldProperty("text2", "setfflags", TextField.READ_ONLY, null);
  41.         form.setField("text2", "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");
  42.         form.setFieldProperty("text3", "setfflags", TextField.READ_ONLY, null);
  43.         form.setField("text3", "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");
  44.         form.setFieldProperty("text4", "setfflags", TextField.READ_ONLY, null);
  45.         form.setField("text4", "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.         stamper.close();
  47.     }
  48.    
  49.     public byte[] createForm() throws DocumentException, IOException {
  50.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  51.         Document document = new Document();
  52.         PdfWriter writer = PdfWriter.getInstance(document, baos);
  53.         document.open();
  54.         Rectangle rect = new Rectangle(36, 770, 144, 806);
  55.         writer.addAnnotation(getTextField(writer, rect, "text1"));
  56.         rect = new Rectangle(148, 770, 256, 806);
  57.         writer.addAnnotation(getTextField(writer, rect, "text2"));
  58.         rect = new Rectangle(36, 724, 144, 760);
  59.         writer.addAnnotation(getTextField(writer, rect, "text3"));
  60.         rect = new Rectangle(148, 727, 256, 760);
  61.         writer.addAnnotation(getTextField(writer, rect, "text4"));
  62.         document.close();
  63.         return baos.toByteArray();
  64.     }
  65.  
  66.     public PdfFormField getTextField(PdfWriter writer, Rectangle rect, String name) throws IOException, DocumentException {
  67.         TextField tf = new TextField(writer, rect, name);
  68.         tf.setFontSize(16);
  69.         tf.setOptions(TextField.MULTILINE);
  70.         return tf.getTextField();
  71.     }
  72. }
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