Filling out forms

Tags: formstext fieldfield formattingiText 7

These examples were written in answer to questions such as:

Files: 
/*
 
    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
 * <p/>
 * Sometimes you need to change the font of a field.
 */
package com.itextpdf.samples.sandbox.acroforms;
 
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.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 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, 12);
        form.getField("test2").setValue(VALUE, font, 12);
 
        pdfDoc.close();
    }
}
/*
 
    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.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.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 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, 12);
 
        form.flattenFields();
        pdfDoc.close();
    }
}
/*
 
    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();
    }
}
/*
 
    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();
    }
}
/*
 
    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();
    }
}
Resources: 
File nameRaw URLUpdated
FreeSans.ttfFreeSans.ttf2016-08-04 3:45 pm
f8966.pdff8966.pdf2016-08-10 11:15 am
form.pdfform.pdf2016-08-09 3:19 pm
test.pdftest.pdf2016-08-10 11:15 am