XFA examples

Tags: XFAformscolored textfontsfont colorfield propertiesfill formiText 7pdfXfapdfXFA examples
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/27057187/setting-acrofield-text-color-in-itextsharp
 */
package com.itextpdf.samples.sandbox.acroforms;
 
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 com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Map;
 
import org.junit.experimental.categories.Category;
 
 
@Category(SampleTest.class)
public class RemoveXFA extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/acroforms/remove_xfa.pdf";
    public static final String SRC = "./src/test/resources/pdfs/reportcardinitial.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new RemoveXFA().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();
 
        Map<String, PdfFormField> fields = form.getFormFields();
        for (String name : fields.keySet()) {
            if (name.indexOf("Total") > 0) {
                fields.get(name).getWidgets().get(0).setColor(Color.RED);
            }
            form.getField(name).setValue("X");
        }
 
        pdfDoc.close();
    }
}
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
package com.itextpdf.samples.sandbox.acroforms;
 
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.xfa.XfaForm;
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;
 
@Category(SampleTest.class)
public class FillXFA extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/acroforms/purchase_order_filled.pdf";
    public static final String SRC = "./src/test/resources/pdfs/purchase_order.pdf";
    public static final String XML = "./src/test/resources/xml/data.xml";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new FillXFA().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);
        XfaForm xfa = form.getXfaForm();
        xfa.fillXfaForm(new FileInputStream(XML));
        xfa.write(pdfdoc);
 
        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/28601068/get-names-field-from-interactive-form-pdf
 */
package com.itextpdf.samples.sandbox.acroforms;
 
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.xfa.XfaForm;
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;
 
@Category(SampleTest.class)
public class FillXFA2 extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/acroforms/xfa_form_poland_filled.pdf";
    public static final String SRC = "./src/test/resources/pdfs/xfa_form_poland.pdf";
    public static final String XML = "./src/test/resources/xml/xfa_form_poland.xml";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new FillXFA2().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfReader reader = new PdfReader(SRC);
        reader.setUnethicalReading(true);
        PdfDocument pdfDoc = new PdfDocument(reader, new PdfWriter(DEST));
 
        PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
        XfaForm xfa = form.getXfaForm();
        xfa.fillXfaForm(new FileInputStream(XML));
        xfa.write(pdfDoc);
 
        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/28601068/get-names-field-from-interactive-form-pdf
 */
package com.itextpdf.samples.sandbox.acroforms;
 
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.xfa.XfaForm;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
 
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.FileOutputStream;
 
import org.junit.experimental.categories.Category;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
 
@Category(SampleTest.class)
public class ReadXFA extends GenericTest {
    public static final String DEST = "./target/test/resources/xml/xfa_form_poland.xml";
    public static final String SRC = "./src/test/resources/pdfs/xfa_form_poland.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ReadXFA().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        compareXml = true;
 
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC));
 
        PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
        XfaForm xfa = form.getXfaForm();
 
        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));
 
        pdfDoc.close();
    }
}
File nameRaw URLUpdated
RemoveXFA.javaRemoveXFA.java2016-08-15 1:59 pm
FillXFA.javaFillXFA.java2016-08-15 2:01 pm
FillXFA2.javaFillXFA2.java2016-08-15 2:01 pm
ReadXFA.javaReadXFA.java2016-08-15 2:01 pm
Resources: 
File nameRaw URLUpdated
purchase_order.pdfpurchase_order.pdf2016-08-15 2:06 pm
reportcardinitial.pdfreportcardinitial.pdf2016-08-15 2:06 pm
xfa_form_poland.pdfxfa_form_poland.pdf2016-08-15 2:06 pm
data.xmldata.xml2016-08-09 3:09 pm
xfa_form_poland.xmlxfa_form_poland.xml2016-08-15 2:06 pm