Forms

Files: 
/**
 * Example written for JavaOne 2016.
 * Differences between iText 5 and iText 7 are discussed in the JavaOne talk
 * "Oops, I broke my API" by Raf Hens and Bruno Lowagie.
 * This is the iText 5 version of one of the examples.
 */
package javaone.edition16;
 
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfFormField;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.TextField;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class FormCreation {
 
    public static final String SRC = "resources/pdfs/hello.pdf";
    public static final String DEST = "results/javaone/edition16/form_creation.pdf";
 
 
    public static void main(String[] args) throws DocumentException, IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new FormCreation().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));
        TextField tf = new TextField(stamper.getWriter(), new Rectangle(110, 780, 180, 806), "text");
        tf.setBorderColor(BaseColor.BLUE);
        tf.setBorderWidth(2);
        tf.setTextColor(BaseColor.RED);
        tf.setFontSize(12);
        tf.setText("Text field");
        PdfFormField field = tf.getTextField();
        stamper.addAnnotation(field, 1);
        stamper.close();
        reader.close();
    }
}
/**
 * Example written for JavaOne 2016.
 * Differences between iText 5 and iText 7 are discussed in the JavaOne talk
 * "Oops, I broke my API" by Raf Hens and Bruno Lowagie.
 * This is the iText 7 version of one of the examples.
 */
package com.itextpdf.sandbox.javaone16;
 
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.forms.fields.PdfTextFormField;
import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import java.io.File;
import java.io.IOException;
 
public class FormCreation {
    public static final String SRC = "src/main/resources/pdfs/hello.pdf";
    public static final String DEST = "results/javaone16/form_creation.pdf";
 
    public static void main(String[] args) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new FormCreation().manipulatePdf(SRC, DEST);
    }
 
    public void manipulatePdf(String src, String dest) throws IOException {
        PdfReader reader = new PdfReader(src);
        PdfDocument pdf = new PdfDocument(reader, new PdfWriter(dest));
        PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
        PdfFormField tf = PdfTextFormField.createText(
            pdf, new Rectangle(110, 780, 70, 26), "text", "Text Field")
            .setBorderColor(Color.BLUE)
            .setBorderWidth(2)
            .setColor(Color.RED)
            .setFontSize(12);
        form.addField(tf);
        pdf.close();
    }
}
/**
 * Example written for JavaOne 2016.
 * Differences between iText 5 and iText 7 are discussed in the JavaOne talk
 * "Oops, I broke my API" by Raf Hens and Bruno Lowagie.
 * This is the iText 5 version of one of the examples.
 */
package javaone.edition16;
 
import com.itextpdf.text.BaseColor;
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 FormFilling {
 
    public static final String SRC = "resources/pdfs/hello_form.pdf";
    public static final String DEST = "results/javaone/edition16/form_filling.pdf";
 
 
    public static void main(String[] args) throws DocumentException, IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new FormFilling().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.setFieldProperty("text", "textcolor", BaseColor.BLUE, null);
        fields.setFieldProperty("text", "bordercolor", BaseColor.RED, null);
        fields.setFieldProperty("text", "fontsize", 14, null);
        fields.setField("text", "Field Text");
        stamper.close();
        reader.close();
    }
}
/**
 * Example written for JavaOne 2016.
 * Differences between iText 5 and iText 7 are discussed in the JavaOne talk
 * "Oops, I broke my API" by Raf Hens and Bruno Lowagie.
 * This is the iText 7 version of one of the examples.
 */
package com.itextpdf.sandbox.javaone16;
 
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import java.io.File;
import java.io.IOException;
 
public class FormFilling {
    public static final String SRC = "src/main/resources/pdfs/hello_form.pdf";
    public static final String DEST = "results/javaone16/form_filling.pdf";
 
 
    public static void main(String[] args) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new FormFilling().manipulatePdf(SRC, DEST);
    }
 
    public void manipulatePdf(String src, String dest) throws IOException {
        PdfReader reader = new PdfReader(src);
        PdfDocument pdf = new PdfDocument(reader, new PdfWriter(dest));
        PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
        PdfFormField tf = form.getFormFields().get("text");
        tf.setBorderColor(Color.RED)
            .setColor(Color.BLUE)
            .setFontSize(14)
            .setValue("Field Text");
        pdf.close();
    }
}
File nameRaw URLUpdated
FormCreation.javaFormCreation.java2016-09-02 11:58 am
FormCreation.javaFormCreation.java2016-09-02 11:59 am
FormFilling.javaFormFilling.java2016-09-02 11:58 am
FormFilling.javaFormFilling.java2016-09-02 11:59 am
Resources: 
File nameRaw URLUpdated
hello.pdfhello.pdf2015-10-11 3:52 pm
hello_form.pdfhello_form.pdf2016-09-02 12:01 pm
hello.pdfhello.pdf2016-09-02 12:02 pm
hello_form.pdfhello_form.pdf2016-09-02 12:02 pm
Results: