Filling out forms

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

31st May 2016
admin-marketing

Switch code for this example

FillFormSpecialChars.java
/*

    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV

*/

/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/20401125/overlapping-characters-in-text-field-itext-pdf
 * 

* 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.samples.GenericTest; import com.itextpdf.test.annotations.type.SampleTest; import java.io.File; import org.junit.experimental.categories.Category; @Category(SampleTest.class) public class FillFormSpecialChars extends GenericTest { public static final String DEST = "./target/test/resources/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); } @Override 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
/*

    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV

*/

/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/31335715/cannot-show-special-character-in-acro-form-field
 * 

* 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.samples.GenericTest; import com.itextpdf.test.annotations.type.SampleTest; import java.io.File; import org.junit.experimental.categories.Category; @Category(SampleTest.class) public class FillFormSpecialChars2 extends GenericTest { public static final String DEST = "./target/test/resources/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); } @Override 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(); } }

FillHybridForm.java
/*

    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV

*/

/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/29702986/how-to-check-a-checkbox-in-pdf-file-with-the-same-variable-name-with-itext-and-j
 */
package com.itextpdf.samples.sandbox.acroforms;

import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;

import org.junit.experimental.categories.Category;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

@Category(SampleTest.class)
public class FillHybridForm extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/acroforms/fill_hybrid_form.pdf";
    public static final String SRC = "./src/test/resources/pdfs/f8966.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new FillHybridForm().manipulatePdf(DEST);
    }

    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));

        PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
        form.removeXfaForm();

        form.getField("topmostSubform[0].Page2[0].p2_cb01[0]").setValue("1");
        form.getField("topmostSubform[0].Page2[0].p2_cb01[1]").setValue("2");
        form.getField("topmostSubform[0].Page2[0].p2_cb01[2]").setValue("3");
        form.getField("topmostSubform[0].Page2[0].p2_cb01[3]").setValue("4");
        form.getField("topmostSubform[0].Page2[0].p2_cb01[4]").setValue("5");
        form.getField("topmostSubform[0].Page2[0].p2_cb01[5]").setValue("6");

        pdfDoc.close();
    }
}
FormatFields.java
/*

    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV

*/

/**
 * 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.test.annotations.type.SampleTest;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.samples.GenericTest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.junit.experimental.categories.Category;

@Category(SampleTest.class)
public class FormatFields extends GenericTest {
    public static final String DEST = "./target/test/resources/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);
    }

    @Override
    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
/*

    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV

*/

/**
 * 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 com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;

import java.io.File;
import java.io.IOException;

import org.junit.experimental.categories.Category;

@Category(SampleTest.class)
public class MultiLineField extends GenericTest {
    public static final String DEST = "./target/test/resources/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);
    }

    @Override
    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();
    }
}
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