Filling out forms

31st May 2016
iText

These examples were written in answer to questions such as:

FillFormSpecialChars.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
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2019 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/20401125/overlapping-characters-in-text-field-itext-pdf
 * <p/>
 * Sometimes you need to change the font of a field.
 */
package com.itextpdf.samples.sandbox.acroforms;
 
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
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 java.io.File;
 
public class FillFormSpecialChars {
    public static final String DEST = "./target/sandbox/acroforms/fill_form_special_chars.pdf";
    public static final String FONT = "./src/test/resources/font/FreeSans.ttf";
    public static final String SRC = "./src/test/resources/pdfs/test.pdf";
    public static final String VALUE = "\u011b\u0161\u010d\u0159\u017e\u00fd\u00e1\u00ed\u00e9";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new FillFormSpecialChars().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
 
        PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
        form.setGenerateAppearance(true);
 
        PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H);
 
        form.getField("test").setValue(VALUE, font, 12f);
        form.getField("test2").setValue(VALUE, font, 12f);
 
        pdfDoc.close();
    }
}
FillFormSpecialChars2.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
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2019 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/31335715/cannot-show-special-character-in-acro-form-field
 * <p>
 * Sometimes you need to change the font of a field.
 */
package com.itextpdf.samples.sandbox.acroforms;
 
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
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.licensekey.LicenseKey;
 
import java.io.File;
 
public class FillFormSpecialChars2 {
    public static final String DEST = "./target/sandbox/acroforms/fill_form_special_chars2.pdf";
    public static final String FONT = "./src/test/resources/font/FreeSans.ttf";
    public static final String SRC = "./src/test/resources/pdfs/form.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new FillFormSpecialChars2().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
 
        PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
        form.setGenerateAppearance(true);
 
        PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H);
        form.getField("Name").setValue("\u04e711111", font, 12f);
 
        form.flattenFields();
        pdfDoc.close();
    }
}
FormatFields.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
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2019 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 the following question:
 * http://stackoverflow.com/questions/27711035/itextsharp-acrofields-format-as-number
 */
package com.itextpdf.samples.sandbox.acroforms;
 
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.forms.PdfAcroForm;
 
import java.io.File;
 
public class FormatFields {
    public static final String DEST = "./target/sandbox/acroforms/format_fields.pdf";
    public static final String SRC = "./src/test/resources/pdfs/form.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new FormatFields().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
        PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
        form.getField("Name").setValue("1.0", "100%");
        form.getField("Company").setValue("1217000.000000", "$1,217,000");
        pdfDoc.close();
    }
}
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
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2019 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.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;
import java.io.IOException;
 
public class MultiLineField {
    public static final String DEST = "./target/sandbox/acroforms/multi_line_field.pdf";
    public static final String SRC = "./src/test/resources/pdfs/multinewline.pdf";
 
    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 {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(new RandomAccessSourceFactory().createSource(createForm()),
                new ReaderProperties()), new PdfWriter(DEST));
        PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
        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");
        form.flattenFields();
        pdfDoc.close();
    }
 
    public byte[] createForm() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(baos));
 
        Rectangle rect = new Rectangle(36, 720, 108, 86);
        PdfTextFormField tf = PdfFormField.createText(pdfDoc, rect, "text", "text");
        tf.setMultiline(true);
 
        PdfAcroForm.getAcroForm(pdfDoc, true).addField(tf);
 
        pdfDoc.close();
        return baos.toByteArray();
    }
}
FillFormSpecialChars.cs
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
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2019 iText Group NV
Authors: iText Software.
 
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
 
using System;
using System.IO;
using iText.Forms;
using iText.IO.Font;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
 
namespace iText.Samples.Sandbox.Acroforms
{
    public class FillFormSpecialChars
    {
        public static readonly String DEST = "../../results/sandbox/acroforms/fill_form_special_chars.pdf";
 
        public static readonly String FONT = "../../resources/font/FreeSans.ttf";
        public static readonly String SRC = "../../resources/pdfs/test.pdf";
 
        // ěščřžýáíé characters
        public const String VALUE = "\u011b\u0161\u010d\u0159\u017e\u00fd\u00e1\u00ed\u00e9";
 
        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
 
            new FillFormSpecialChars().ManipulatePdf(DEST);
        }
 
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true);
 
            // Being set as true, this parameter is responsible to generate an appearance Stream
            // while flattening for all form fields that don't have one. Generating appearances will
            // slow down form flattening, but otherwise Acrobat might render the pdf on its own rules.
            form.SetGenerateAppearance(true);
 
            PdfFont font = PdfFontFactory.CreateFont(FONT, PdfEncodings.IDENTITY_H);
            form.GetField("test").SetValue(VALUE, font, 12f);
            form.GetField("test2").SetValue(VALUE, font, 12f);
 
            pdfDoc.Close();
        }
    }
}
FillFormSpecialChars2.cs
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
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2019 iText Group NV
Authors: iText Software.
 
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
 
using System;
using System.IO;
using iText.Forms;
using iText.IO.Font;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
 
namespace iText.Samples.Sandbox.Acroforms
{
    public class FillFormSpecialChars2
    {
        public static readonly String DEST = "../../results/sandbox/acroforms/fill_form_special_chars2.pdf";
 
        public static readonly String FONT = "../../resources/font/FreeSans.ttf";
        public static readonly String SRC = "../../resources/pdfs/form.pdf";
 
        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
 
            new FillFormSpecialChars2().ManipulatePdf(DEST);
        }
 
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true);
 
            // This method tells to generate an appearance Stream while flattening for all form fields that don't have one.
            // Generating appearances will slow down form flattening,
            // but otherwise the results can be unexpected in Acrobat.
            form.SetGenerateAppearance(true);
 
            PdfFont font = PdfFontFactory.CreateFont(FONT, PdfEncodings.IDENTITY_H);
 
            // ӧ character is used here
            form.GetField("Name").SetValue("\u04e711111", font, 12f);
 
            // If no fields have been explicitly included, then all fields are flattened.
            // Otherwise only the included fields are flattened.
            form.FlattenFields();
 
            pdfDoc.Close();
        }
    }
}
FormatFields.cs
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
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2019 iText Group NV
Authors: iText Software.
 
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
 
using System;
using System.IO;
using iText.Forms;
using iText.Kernel.Pdf;
 
namespace iText.Samples.Sandbox.Acroforms
{
    public class FormatFields
    {
        public static readonly String DEST = "../../results/sandbox/acroforms/format_fields.pdf";
 
        public static readonly String SRC = "../../resources/pdfs/form.pdf";
 
        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
 
            new FormatFields().ManipulatePdf(DEST);
        }
 
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true);
 
            // The second parameter sets how the field's value will be displayed in the resultant pdf.
            // If the second parameter is null, then actual value will be shown.
            form.GetField("Name").SetValue("1.0", "100%");
            form.GetField("Company").SetValue("1217000.000000", "$1,217,000");
 
            pdfDoc.Close();
        }
    }
}
MultiLineField.cs
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
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2019 iText Group NV
Authors: iText Software.
 
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
 
using System;
using System.IO;
using iText.Forms;
using iText.Forms.Fields;
using iText.IO.Source;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
 
namespace iText.Samples.Sandbox.Acroforms
{
    public class MultiLineField
    {
        public static readonly String DEST = "../../results/sandbox/acroforms/multi_line_field.pdf";
 
        public static readonly String FIELD_NAME = "text";
 
        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
 
            new MultiLineField().ManipulatePdf(DEST);
        }
 
        protected void ManipulatePdf(String dest)
        {
            
            // 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 virtual 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.ToArray();
        }
    }
}

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