XFA examples

This is a code example of iText PDF, discover more.

31st May 2016
admin-marketing

Switch code for this example

RemoveXFA.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/27057187/setting-acrofield-text-color-in-itextsharp
  11.  */
  12. package com.itextpdf.samples.sandbox.acroforms;
  13.  
  14. import com.itextpdf.forms.PdfAcroForm;
  15. import com.itextpdf.forms.fields.PdfFormField;
  16. import com.itextpdf.kernel.color.Color;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfReader;
  19. import com.itextpdf.kernel.pdf.PdfWriter;
  20. import com.itextpdf.samples.GenericTest;
  21. import com.itextpdf.test.annotations.type.SampleTest;
  22.  
  23. import java.io.File;
  24. import java.util.Map;
  25.  
  26. import org.junit.experimental.categories.Category;
  27.  
  28.  
  29. @Category(SampleTest.class)
  30. public class RemoveXFA extends GenericTest {
  31.     public static final String DEST = "./target/test/resources/sandbox/acroforms/remove_xfa.pdf";
  32.     public static final String SRC = "./src/test/resources/pdfs/reportcardinitial.pdf";
  33.  
  34.     public static void main(String[] args) throws Exception {
  35.         File file = new File(DEST);
  36.         file.getParentFile().mkdirs();
  37.         new RemoveXFA().manipulatePdf(DEST);
  38.     }
  39.  
  40.     @Override
  41.     protected void manipulatePdf(String dest) throws Exception {
  42.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  43.  
  44.         PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
  45.         form.removeXfaForm();
  46.  
  47.         Map fields = form.getFormFields();
  48.         for (Map.Entry name : fields.entrySet()) {
  49.             if (name.getKey().indexOf("Total") > 0) {
  50.                 name.getValue().getWidgets().get(0).setColor(Color.RED);
  51.             }
  52.             name.getValue().setValue("X");
  53.         }
  54.  
  55.         pdfDoc.close();
  56.     }
  57. }
FillXFA.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. package com.itextpdf.samples.sandbox.acroforms;
  9.  
  10. import com.itextpdf.forms.PdfAcroForm;
  11. import com.itextpdf.forms.xfa.XfaForm;
  12. import com.itextpdf.kernel.pdf.PdfDocument;
  13. import com.itextpdf.kernel.pdf.PdfReader;
  14. import com.itextpdf.kernel.pdf.PdfWriter;
  15. import com.itextpdf.samples.GenericTest;
  16. import com.itextpdf.test.annotations.type.SampleTest;
  17.  
  18. import org.junit.experimental.categories.Category;
  19.  
  20. import java.io.File;
  21. import java.io.FileInputStream;
  22.  
  23. @Category(SampleTest.class)
  24. public class FillXFA extends GenericTest {
  25.     public static final String DEST = "./target/test/resources/sandbox/acroforms/purchase_order_filled.pdf";
  26.     public static final String SRC = "./src/test/resources/pdfs/purchase_order.pdf";
  27.     public static final String XML = "./src/test/resources/xml/data.xml";
  28.  
  29.     public static void main(String[] args) throws Exception {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new FillXFA().manipulatePdf(DEST);
  33.     }
  34.  
  35.     @Override
  36.     protected void manipulatePdf(String dest) throws Exception {
  37.         PdfDocument pdfdoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  38.  
  39.         PdfAcroForm form = PdfAcroForm.getAcroForm(pdfdoc, true);
  40.         XfaForm xfa = form.getXfaForm();
  41.         xfa.fillXfaForm(new FileInputStream(XML));
  42.         xfa.write(pdfdoc);
  43.  
  44.         pdfdoc.close();
  45.     }
  46. }
FillXFA2.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7. /**
  8.  * Example written by Bruno Lowagie in answer to:
  9.  * http://stackoverflow.com/questions/28601068/get-names-field-from-interactive-form-pdf
  10.  */
  11. package com.itextpdf.samples.sandbox.acroforms;
  12.  
  13. import com.itextpdf.forms.PdfAcroForm;
  14. import com.itextpdf.forms.xfa.XfaForm;
  15. import com.itextpdf.kernel.pdf.PdfDocument;
  16. import com.itextpdf.kernel.pdf.PdfReader;
  17. import com.itextpdf.kernel.pdf.PdfWriter;
  18. import com.itextpdf.samples.GenericTest;
  19. import com.itextpdf.test.annotations.type.SampleTest;
  20.  
  21. import org.junit.experimental.categories.Category;
  22.  
  23. import java.io.File;
  24. import java.io.FileInputStream;
  25.  
  26. @Category(SampleTest.class)
  27. public class FillXFA2 extends GenericTest {
  28.     public static final String DEST = "./target/test/resources/sandbox/acroforms/xfa_form_poland_filled.pdf";
  29.     public static final String SRC = "./src/test/resources/pdfs/xfa_form_poland.pdf";
  30.     public static final String XML = "./src/test/resources/xml/xfa_form_poland.xml";
  31.  
  32.     public static void main(String[] args) throws Exception {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new FillXFA2().manipulatePdf(DEST);
  36.     }
  37.  
  38.     @Override
  39.     protected void manipulatePdf(String dest) throws Exception {
  40.         PdfReader reader = new PdfReader(SRC);
  41.         reader.setUnethicalReading(true);
  42.         PdfDocument pdfDoc = new PdfDocument(reader, new PdfWriter(DEST));
  43.  
  44.         PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
  45.         XfaForm xfa = form.getXfaForm();
  46.         xfa.fillXfaForm(new FileInputStream(XML));
  47.         xfa.write(pdfDoc);
  48.  
  49.         pdfDoc.close();
  50.     }
  51. }
ReadXFA.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7. /**
  8.  * Example written by Bruno Lowagie in answer to:
  9.  * http://stackoverflow.com/questions/28601068/get-names-field-from-interactive-form-pdf
  10.  */
  11. package com.itextpdf.samples.sandbox.acroforms;
  12.  
  13. import com.itextpdf.forms.PdfAcroForm;
  14. import com.itextpdf.forms.xfa.XfaForm;
  15. import com.itextpdf.kernel.pdf.PdfDocument;
  16. import com.itextpdf.kernel.pdf.PdfReader;
  17. import com.itextpdf.samples.GenericTest;
  18. import com.itextpdf.test.annotations.type.SampleTest;
  19.  
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import javax.xml.transform.OutputKeys;
  23. import javax.xml.transform.Transformer;
  24. import javax.xml.transform.TransformerFactory;
  25. import javax.xml.transform.dom.DOMSource;
  26. import javax.xml.transform.stream.StreamResult;
  27.  
  28. import org.junit.experimental.categories.Category;
  29. import org.w3c.dom.Node;
  30. import org.w3c.dom.NodeList;
  31.  
  32. @Category(SampleTest.class)
  33. public class ReadXFA extends GenericTest {
  34.     public static final String DEST = "./target/test/resources/xml/xfa_form_poland.xml";
  35.     public static final String SRC = "./src/test/resources/pdfs/xfa_form_poland.pdf";
  36.  
  37.     public static void main(String[] args) throws Exception {
  38.         File file = new File(DEST);
  39.         file.getParentFile().mkdirs();
  40.         new ReadXFA().manipulatePdf(DEST);
  41.     }
  42.  
  43.     @Override
  44.     protected void manipulatePdf(String dest) throws Exception {
  45.         compareXml = true;
  46.  
  47.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC));
  48.  
  49.         PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
  50.         XfaForm xfa = form.getXfaForm();
  51.  
  52.         Node node = xfa.getDatasetsNode();
  53.         NodeList list = node.getChildNodes();
  54.         for (int i = 0; i < list.getLength(); i++) {
  55.             if ("data".equals(list.item(i).getLocalName())) {
  56.                 node = list.item(i);
  57.                 break;
  58.             }
  59.         }
  60.         list = node.getChildNodes();
  61.         for (int i = 0; i < list.getLength(); i++) {
  62.             if ("movies".equals(list.item(i).getLocalName())) {
  63.                 node = list.item(i);
  64.                 break;
  65.             }
  66.         }
  67.  
  68.         Transformer tf = TransformerFactory.newInstance().newTransformer();
  69.         tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
  70.         tf.setOutputProperty(OutputKeys.INDENT, "yes");
  71.         FileOutputStream os = new FileOutputStream(DEST);
  72.         tf.transform(new DOMSource(node), new StreamResult(os));
  73.         os.close();
  74.  
  75.         pdfDoc.close();
  76.     }
  77. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now