Filling out forms
These examples were written in answer to questions such as:
multilinefield
JAVA
JAVA
/*
* Example written by Bruno Lowagie in answer to
* http://stackoverflow.com/questions/30233783/alignment-issue-with-multiline-text-form-fields-in-itextsharp
*/
package sandbox.acroforms;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.TextField;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
@WrapToTest
public class MultiLineField {
public static final String DEST = "results/acroforms/multinewline.pdf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new MultiLineField().createPdf(DEST);
}
public void createPdf(String filename) throws DocumentException, IOException {
PdfReader reader = new PdfReader(createForm());
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(filename));
AcroFields form = stamper.getAcroFields();
form.setField("text", "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");
stamper.setFormFlattening(true);
stamper.close();
}
public byte[] createForm() throws DocumentException, IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, baos);
document.open();
Rectangle rect = new Rectangle(36, 720, 144, 806);
TextField tf = new TextField(writer, rect, "text");
tf.setOptions(TextField.MULTILINE);
writer.addAnnotation(tf.getTextField());
document.close();
return baos.toByteArray();
}
}
formatfields
JAVA
JAVA
/**
* Example written by Bruno Lowagie in answer to the following question:
* http://stackoverflow.com/questions/27711035/itextsharp-acrofields-format-as-number
*/
package sandbox.acroforms;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
@WrapToTest
public class FormatFields {
public static final String SRC = "resources/pdfs/form.pdf";
public static final String DEST = "results/acroforms/field_format.pdf";
public static void main(String[] args) throws DocumentException, IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new FormatFields().manipulatePdf(SRC, DEST);
}
public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
AcroFields form = stamper.getAcroFields();
form.setField("Name", "1.0", "100%");
form.setField("Company", "1217000.000000", "$1,217,000");
stamper.close();
}
}
fillhybridform
JAVA
JAVA
/*
* 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 sandbox.acroforms;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
@WrapToTest
public class FillHybridForm {
public static final String SRC = "resources/pdfs/f8966.pdf";
public static final String DEST = "results/acroforms/f8966_filled.pdf";
public static void main(String[] args) throws DocumentException, IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new FillHybridForm().manipulatePdf(SRC, DEST);
}
public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
AcroFields form = stamper.getAcroFields();
form.removeXfa();
form.setField("topmostSubform[0].Page2[0].p2_cb01[0]", "1");
form.setField("topmostSubform[0].Page2[0].p2_cb01[1]", "2");
form.setField("topmostSubform[0].Page2[0].p2_cb01[2]", "3");
form.setField("topmostSubform[0].Page2[0].p2_cb01[3]", "4");
form.setField("topmostSubform[0].Page2[0].p2_cb01[4]", "5");
form.setField("topmostSubform[0].Page2[0].p2_cb01[5]", "6");
stamper.close();
reader.close();
}
}
fillformspecialchars
JAVA
JAVA
/**
* 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 sandbox.acroforms;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
@WrapToTest
public class FillFormSpecialChars {
public static final String SRC = "resources/pdfs/test.pdf";
public static final String DEST = "results/acroforms/test.pdf";
public static final String VALUE = "\u011b\u0161\u010d\u0159\u017e\u00fd\u00e1\u00ed\u00e9";
public static final String FONT = "resources/fonts/FreeSans.ttf";
public static void main(String[] args) throws DocumentException, IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new FillFormSpecialChars().manipulatePdf(SRC, DEST);
}
public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader,
new FileOutputStream(dest));
AcroFields fields = stamper.getAcroFields();
fields.setGenerateAppearances(true);
/**This method is used instead 'BaseFont createFont(String name, String encoding, boolean embedded)'
in order to avoid the font cashing. The cashed font could be mistakenly used in another tests.
This could cause a test failure on some platforms.
**/
BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, false, null, null, false);
fields.setFieldProperty("test", "textfont", bf, null);
fields.setField("test", VALUE);
fields.setFieldProperty("test2", "textfont", bf, null);
fields.setField("test2", VALUE);
stamper.close();
}
}
fillformspecialchars2
JAVA
JAVA
/**
* 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 sandbox.acroforms;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
@WrapToTest
public class FillFormSpecialChars2 {
public static final String SRC = "resources/pdfs/form.pdf";
public static final String DEST = "results/acroforms/form_special_chars.pdf";
public static final String FONT = "resources/fonts/FreeSans.ttf";
public static void main(String[] args) throws DocumentException, IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new FillFormSpecialChars2().manipulatePdf(SRC, DEST);
}
public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader,
new FileOutputStream(dest));
AcroFields fields = stamper.getAcroFields();
BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, false, null, null, false);
fields.setFieldProperty("Name", "textfont", bf, null);
fields.setField("Name", "\u04e711111");
stamper.setFormFlattening(true);
stamper.close();
}
}
fillform
JAVA
JAVA
/*
* Example written by Bruno Lowagie in answer to
* http://stackoverflow.com/questions/36523371
*/
package sandbox.acroforms;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author Bruno Lowagie (iText Software)
*/
public class FillForm {
public static final String SRC = "resources/pdfs/CertificateOfExcellence.pdf";
public static final String DEST = "results/acroforms/certificate.pdf";
public static void main(String[] args) throws DocumentException, IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new FillForm().manipulatePdf(SRC, DEST);
}
public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
AcroFields form = stamper.getAcroFields();
form.setField("course", "Copying and Pasting from StackOverflow");
form.setField("name", "Some dude on StackOverflow");
form.setField("date", "April 10, 2016");
form.setField("description",
"In this course, people consistently ignore the existing documentation completely. "
+ "They are encouraged to do no effort whatsoever, but instead post their questions "
+ "on StackOverflow. It would be a mistake to refer to people completing this course "
+ "as developers. A better designation for them would be copy/paste artist. "
+ "Only in very rare cases do these people know what they are actually doing. "
+ "Not a single student has ever learned anything substantial during this course.");
stamper.setFormFlattening(true);
stamper.close();
}
}