Filling out forms
These examples were written in answer to questions such as:
fillformspecialchars
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.acroforms;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormCreator;
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/main/resources/font/FreeSans.ttf";
public static final String SRC = "./src/main/resources/pdfs/test.pdf";
// ěščřžýáíé characters
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 = PdfFormCreator.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
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.acroforms;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormCreator;
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 FillFormSpecialChars2 {
public static final String DEST = "./target/sandbox/acroforms/fill_form_special_chars2.pdf";
public static final String FONT = "./src/main/resources/font/FreeSans.ttf";
public static final String SRC = "./src/main/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 = PdfFormCreator.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
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.acroforms;
import com.itextpdf.forms.fields.PdfFormCreator;
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/main/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 = PdfFormCreator.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
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.acroforms;
import com.itextpdf.forms.fields.PdfFormCreator;
import com.itextpdf.forms.fields.TextFormFieldBuilder;
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 = PdfFormCreator.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 = new TextFormFieldBuilder(pdfDoc, FIELD_NAME)
.setWidgetRectangle(rect).createText();
textFormField.setValue("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);
PdfFormCreator.getAcroForm(pdfDoc, true).addField(textFormField);
pdfDoc.close();
return baos.toByteArray();
}
}