iText pdf library
Website search

Multiline fields

Examples with multi-line text fields.

MultiLineField.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * Example written by Bruno Lowagie in answer to
 * http://stackoverflow.com/questions/30233783/alignment-issue-with-multiline-text-form-fields-in-itextsharp
 */
package com.itextpdf.samples.sandbox.acroforms;
 
import com.itextpdf.io.source.IRandomAccessSource;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.io.source.ByteArrayOutputStream;
import com.itextpdf.io.source.RandomAccessSourceFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.forms.fields.PdfTextFormField;
import com.itextpdf.kernel.pdf.ReaderProperties;
 
import java.io.File;
 
public class MultiLineField {
    public static final String DEST = "./target/sandbox/acroforms/multi_line_field.pdf";
 
    public static final String FIELD_NAME = "text";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
 
        new MultiLineField().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws Exception {
        
        // createForm() method creates a temporary document in the memory,
        // which then will be used as a source while writing to a real document
        byte[] content = createForm();
        IRandomAccessSource source = new RandomAccessSourceFactory().createSource(content);
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(source, new ReaderProperties()), new PdfWriter(dest));
        PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
 
        form.getField(FIELD_NAME).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");
 
        // If no fields have been explicitly included, then all fields are flattened.
        // Otherwise only the included fields are flattened.
        form.flattenFields();
 
        pdfDoc.close();
    }
 
    public byte[] createForm() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(baos));
        Rectangle rect = new Rectangle(36, 720, 108, 86);
 
        PdfTextFormField textFormField = PdfFormField.createText(pdfDoc, rect, FIELD_NAME, "text");
 
        // Being set as true, the field can contain multiple lines of text;
        // if false, the field's text is restricted to a single line.
        textFormField.setMultiline(true);
        PdfAcroForm.getAcroForm(pdfDoc, true).addField(textFormField);
 
        pdfDoc.close();
 
        return baos.toByteArray();
    }
}
ReadOnlyField.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.acroforms;
 
import com.itextpdf.io.source.IRandomAccessSource;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.io.source.ByteArrayOutputStream;
import com.itextpdf.io.source.RandomAccessSourceFactory;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.ReaderProperties;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.forms.fields.PdfTextFormField;
 
import java.io.File;
import java.io.IOException;
 
public class ReadOnlyField {
    public static final String DEST = "./target/sandbox/acroforms/read_only_field.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
 
        new ReadOnlyField().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws Exception {
 
        // createForm() method creates a temporary document in the memory,
        // which then will be used as a source while writing to a real document
        byte[] content = createForm();
        IRandomAccessSource source = new RandomAccessSourceFactory().createSource(content);
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(source, new ReaderProperties()), new PdfWriter(dest));
        PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
 
        form.getField("text")
 
                // Method sets the flag, specifying whether or not the field can be changed.
                .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");
 
        pdfDoc.close();
    }
 
    public byte[] createForm() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(baos));
        PdfFont font = PdfFontFactory.createFont();
        PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
 
        Rectangle rect = new Rectangle(36, 770, 104, 36);
        PdfTextFormField textField = PdfFormField.createText(pdfDoc, rect, "text",
                "text", font, 20f);
 
        // Being set as true, the field can contain multiple lines of text;
        // if false, the field's text is restricted to a single line.
        textField.setMultiline(true);
        form.addField(textField);
 
        pdfDoc.close();
 
        return baos.toByteArray();
    }
}
ReadOnlyField2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.acroforms;
 
import com.itextpdf.io.source.IRandomAccessSource;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.io.source.ByteArrayOutputStream;
import com.itextpdf.io.source.RandomAccessSourceFactory;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.ReaderProperties;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.forms.fields.PdfTextFormField;
 
import java.io.File;
import java.io.IOException;
 
public class ReadOnlyField2 {
    public static final String DEST = "./target/sandbox/acroforms/read_only_field2.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
 
        new ReadOnlyField2().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws Exception {
 
        // createForm() method creates a temporary document in the memory,
        // which then will be used as a source while writing to a real document
        byte[] content = createForm();
        IRandomAccessSource source = new RandomAccessSourceFactory().createSource(content);
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(source, new ReaderProperties()), new PdfWriter(dest));
        PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
 
        form.getField("text1")
 
                // Method sets the flag, specifying whether or not the field can be changed.
                .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");
 
        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");
 
        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");
 
        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");
 
        pdfDoc.close();
    }
 
    public byte[] createForm() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(baos));
        PdfFont font = PdfFontFactory.createFont();
        PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
 
        Rectangle rect = new Rectangle(36, 770, 108, 36);
        PdfTextFormField textField1 = PdfFormField.createText(pdfDoc, rect, "text1",
                "text1", font, 18f);
 
        // Being set as true, the field can contain multiple lines of text;
        // if false, the field's text is restricted to a single line.
        textField1.setMultiline(true);
        form.addField(textField1);
 
        rect = new Rectangle(148, 770, 108, 36);
        PdfTextFormField textField2 = PdfFormField.createText(pdfDoc, rect, "text2",
                "text2", font, 18f);
        textField2.setMultiline(true);
        form.addField(textField2);
 
        rect = new Rectangle(36, 724, 108, 36);
        PdfTextFormField textField3 = PdfFormField.createText(pdfDoc, rect, "text3",
                "text3", font, 18f);
        textField3.setMultiline(true);
        form.addField(textField3);
 
        rect = new Rectangle(148, 727, 108, 33);
        PdfTextFormField textField4 = PdfFormField.createText(pdfDoc, rect, "text4",
                "text4", font, 18f);
        textField4.setMultiline(true);
        form.addField(textField4);
 
        pdfDoc.close();
 
        return baos.toByteArray();
    }
}
ReadOnlyField3.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.acroforms;
 
import com.itextpdf.io.source.IRandomAccessSource;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.io.source.ByteArrayOutputStream;
import com.itextpdf.io.source.RandomAccessSourceFactory;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.ReaderProperties;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.forms.fields.PdfTextFormField;
 
import java.io.File;
import java.io.IOException;
 
public class ReadOnlyField3 {
    public static final String DEST = "./target/sandbox/acroforms/read_only_field3.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
 
        new ReadOnlyField3().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws Exception {
 
        // createForm() method creates a temporary document in the memory,
        // which then will be used as a source while writing to a real document
        byte[] content = createForm();
        IRandomAccessSource source = new RandomAccessSourceFactory().createSource(content);
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(source, new ReaderProperties()), new PdfWriter(dest));
        PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
 
        //  Set a flag specifying whether to construct appearance streams and appearance dictionaries
        //  for all widget annotations in the document.
        form.setNeedAppearances(true);
 
        form.getField("text1")
 
                // Method sets the flag, specifying whether or not the field can be changed.
                .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");
 
        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");
 
        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");
 
        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");
 
        pdfDoc.close();
    }
 
    public byte[] createForm() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(baos));
        PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
        PdfFont font = PdfFontFactory.createFont();
 
        Rectangle rect = new Rectangle(36, 770, 108, 36);
        PdfTextFormField textField1 = PdfFormField.createText(pdfDoc, rect,
                "text1", "text1", font, 18f);
 
        // Being set as true, the field can contain multiple lines of text;
        // if false, the field's text is restricted to a single line.
        textField1.setMultiline(true);
        form.addField(textField1);
 
        rect = new Rectangle(148, 770, 108, 36);
        PdfTextFormField textField2 = PdfFormField.createText(pdfDoc, rect,
                "text2", "text2", font, 18f);
        textField2.setMultiline(true);
        form.addField(textField2);
 
        rect = new Rectangle(36, 724, 108, 36);
        PdfTextFormField textField3 = PdfFormField.createText(pdfDoc, rect,
                "text3", "text3", font, 18f);
        textField3.setMultiline(true);
        form.addField(textField3);
 
        rect = new Rectangle(148, 727, 108, 33);
        PdfTextFormField textField4 = PdfFormField.createText(pdfDoc, rect,
                "text4", "text4", font, 18f);
        textField4.setMultiline(true);
        form.addField(textField4);
 
        pdfDoc.close();
 
        return baos.toByteArray();
    }
}

Click the following link to see the legacy example for iText 5. Except for security fixes, iText 5 is no longer being developed.



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