XFA examples

Tags: XFAformscolored textfontsiText 5font colorfield propertiesfill form
Files: 
package sandbox.xfa;
 
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.XfaForm;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class FillXFA {
 
    public static final String SRC = "resources/pdfs/purchase_order.pdf";
    public static final String XML = "resources/xml/data.xml";
    public static final String DEST = "results/xfa/purchase_order_filled.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new FillXFA().manipulatePdf(SRC, DEST);
    }
 
    public void manipulatePdf(String src, String dest)
        throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader,
                new FileOutputStream(dest));
        AcroFields form = stamper.getAcroFields();
        XfaForm xfa = form.getXfa();
        xfa.fillXfaForm(new FileInputStream(XML));
        stamper.close();
        reader.close();
    }
}
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/28601068/get-names-field-from-interactive-form-pdf
 */
package sandbox.xfa;
 
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.XfaForm;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class FillXFA2 {
 
    public static final String SRC = "resources/pdfs/xfa_form_poland.pdf";
    public static final String XML = "resources/xml/xfa_form_poland.xml";
    public static final String DEST = "results/xfa/xfa_form_poland_filled.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new FillXFA2().manipulatePdf(SRC, DEST);
    }
 
    public void manipulatePdf(String src, String dest)
        throws IOException, DocumentException {
        PdfReader.unethicalreading = true;
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader,
                new FileOutputStream(dest));
        AcroFields form = stamper.getAcroFields();
        XfaForm xfa = form.getXfa();
        xfa.fillXfaForm(new FileInputStream(XML));
        stamper.close();
        reader.close();
    }
}
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/28601068/get-names-field-from-interactive-form-pdf
 */
package sandbox.xfa;
 
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.XfaForm;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
 
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
 
public class ReadXFA {
 
    public static final String SRC = "resources/pdfs/xfa_form_poland.pdf";
    public static final String DEST = "results/xfa/data.xml";
 
    public static void main(String[] args) throws IOException, DocumentException, TransformerException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ReadXFA().readXml(SRC, DEST);
    }
 
    public void readXml(String src, String dest)
        throws IOException, DocumentException, TransformerException {
        PdfReader reader = new PdfReader(src);
        AcroFields form = reader.getAcroFields();
        XfaForm xfa = form.getXfa();
        Node node = xfa.getDatasetsNode();
        NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            if("data".equals(list.item(i).getLocalName())) {
                node = list.item(i);
                break;
            }
        }
        list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            if("movies".equals(list.item(i).getLocalName())) {
                node = list.item(i);
                break;
            }
        }
        Transformer tf = TransformerFactory.newInstance().newTransformer();
        tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        tf.setOutputProperty(OutputKeys.INDENT, "yes");
        FileOutputStream os = new FileOutputStream(dest);
        tf.transform(new DOMSource(node), new StreamResult(os));
        reader.close();
    }
}
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/27057187/setting-acrofield-text-color-in-itextsharp
 */
package sandbox.acroforms;
 
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 java.util.Map;
import sandbox.WrapToTest;
 
@WrapToTest
public class RemoveXFA {
 
    public static final String SRC = "resources/pdfs/reportcardinitial.pdf";
    public static final String DEST = "results/acroforms/reportcard.pdf";
 
    public static void main(String[] args) throws DocumentException, IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new RemoveXFA().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();
        Map<String, AcroFields.Item> fields = form.getFields();
        for (String name : fields.keySet()) {
            if (name.indexOf("Total") > 0)
                form.setFieldProperty(name, "textcolor", BaseColor.RED, null);
            form.setField(name, "X");
        }
        stamper.close();
        reader.close();
    }
}
File nameRaw URLUpdated
FillXFA.javaFillXFA.java2015-11-08 6:06 pm
FillXFA2.javaFillXFA2.java2015-11-08 6:06 pm
ReadXFA.javaReadXFA.java2015-11-08 6:06 pm
RemoveXFA.javaRemoveXFA.java2015-10-25 3:37 am
Resources: 
File nameRaw URLUpdated
purchase_order.pdfpurchase_order.pdf2015-11-08 6:09 pm
data.xmldata.xml2015-11-07 4:42 pm
xfa_form_poland.xmlxfa_form_poland.xml2015-11-08 6:08 pm
xfa_form_poland.pdfxfa_form_poland.pdf2015-11-08 6:09 pm
reportcardinitial.pdfreportcardinitial.pdf2015-10-25 3:40 am