XFA examples

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

25th October 2015
admin-marketing

Switch code for this example

FillXFA.java
  1. package sandbox.xfa;
  2.  
  3. import com.itextpdf.text.DocumentException;
  4. import com.itextpdf.text.pdf.AcroFields;
  5. import com.itextpdf.text.pdf.PdfReader;
  6. import com.itextpdf.text.pdf.PdfStamper;
  7. import com.itextpdf.text.pdf.XfaForm;
  8.  
  9. import java.io.File;
  10. import java.io.FileInputStream;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13. import sandbox.WrapToTest;
  14.  
  15. @WrapToTest
  16. public class FillXFA {
  17.  
  18.     public static final String SRC = "resources/pdfs/purchase_order.pdf";
  19.     public static final String XML = "resources/xml/data.xml";
  20.     public static final String DEST = "results/xfa/purchase_order_filled.pdf";
  21.  
  22.     public static void main(String[] args) throws IOException, DocumentException {
  23.         File file = new File(DEST);
  24.         file.getParentFile().mkdirs();
  25.         new FillXFA().manipulatePdf(SRC, DEST);
  26.     }
  27.  
  28.     public void manipulatePdf(String src, String dest)
  29.         throws IOException, DocumentException {
  30.         PdfReader reader = new PdfReader(src);
  31.         PdfStamper stamper = new PdfStamper(reader,
  32.                 new FileOutputStream(dest));
  33.         AcroFields form = stamper.getAcroFields();
  34.         XfaForm xfa = form.getXfa();
  35.         xfa.fillXfaForm(new FileInputStream(XML));
  36.         stamper.close();
  37.         reader.close();
  38.     }
  39. }
FillXFA2.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/28601068/get-names-field-from-interactive-form-pdf
  4.  */
  5. package sandbox.xfa;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.pdf.AcroFields;
  9. import com.itextpdf.text.pdf.PdfReader;
  10. import com.itextpdf.text.pdf.PdfStamper;
  11. import com.itextpdf.text.pdf.XfaForm;
  12.  
  13. import java.io.File;
  14. import java.io.FileInputStream;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;
  18.  
  19. @WrapToTest
  20. public class FillXFA2 {
  21.  
  22.     public static final String SRC = "resources/pdfs/xfa_form_poland.pdf";
  23.     public static final String XML = "resources/xml/xfa_form_poland.xml";
  24.     public static final String DEST = "results/xfa/xfa_form_poland_filled.pdf";
  25.  
  26.     public static void main(String[] args) throws IOException, DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new FillXFA2().manipulatePdf(SRC, DEST);
  30.     }
  31.  
  32.     public void manipulatePdf(String src, String dest)
  33.         throws IOException, DocumentException {
  34.         PdfReader.unethicalreading = true;
  35.         PdfReader reader = new PdfReader(src);
  36.         PdfStamper stamper = new PdfStamper(reader,
  37.                 new FileOutputStream(dest));
  38.         AcroFields form = stamper.getAcroFields();
  39.         XfaForm xfa = form.getXfa();
  40.         xfa.fillXfaForm(new FileInputStream(XML));
  41.         stamper.close();
  42.         reader.close();
  43.     }
  44. }
ReadXFA.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/28601068/get-names-field-from-interactive-form-pdf
  4.  */
  5. package sandbox.xfa;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.pdf.AcroFields;
  9. import com.itextpdf.text.pdf.PdfReader;
  10. import com.itextpdf.text.pdf.XfaForm;
  11.  
  12. import java.io.File;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15.  
  16. import javax.xml.transform.OutputKeys;
  17. import javax.xml.transform.Transformer;
  18. import javax.xml.transform.TransformerException;
  19. import javax.xml.transform.TransformerFactory;
  20. import javax.xml.transform.dom.DOMSource;
  21. import javax.xml.transform.stream.StreamResult;
  22.  
  23. import org.w3c.dom.Node;
  24. import org.w3c.dom.NodeList;
  25.  
  26. public class ReadXFA {
  27.  
  28.     public static final String SRC = "resources/pdfs/xfa_form_poland.pdf";
  29.     public static final String DEST = "results/xfa/data.xml";
  30.  
  31.     public static void main(String[] args) throws IOException, DocumentException, TransformerException {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new ReadXFA().readXml(SRC, DEST);
  35.     }
  36.  
  37.     public void readXml(String src, String dest)
  38.         throws IOException, DocumentException, TransformerException {
  39.         PdfReader reader = new PdfReader(src);
  40.         AcroFields form = reader.getAcroFields();
  41.         XfaForm xfa = form.getXfa();
  42.         Node node = xfa.getDatasetsNode();
  43.         NodeList list = node.getChildNodes();
  44.         for (int i = 0; i < list.getLength(); i++) {
  45.             if("data".equals(list.item(i).getLocalName())) {
  46.                 node = list.item(i);
  47.                 break;
  48.             }
  49.         }
  50.         list = node.getChildNodes();
  51.         for (int i = 0; i < list.getLength(); i++) {
  52.             if("movies".equals(list.item(i).getLocalName())) {
  53.                 node = list.item(i);
  54.                 break;
  55.             }
  56.         }
  57.         Transformer tf = TransformerFactory.newInstance().newTransformer();
  58.         tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
  59.         tf.setOutputProperty(OutputKeys.INDENT, "yes");
  60.         FileOutputStream os = new FileOutputStream(dest);
  61.         tf.transform(new DOMSource(node), new StreamResult(os));
  62.         reader.close();
  63.     }
  64. }
RemoveXFA.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/27057187/setting-acrofield-text-color-in-itextsharp
  4.  */
  5. package sandbox.acroforms;
  6.  
  7. import com.itextpdf.text.BaseColor;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.pdf.AcroFields;
  10. import com.itextpdf.text.pdf.PdfReader;
  11. import com.itextpdf.text.pdf.PdfStamper;
  12.  
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16. import java.util.Map;
  17. import sandbox.WrapToTest;
  18.  
  19. @WrapToTest
  20. public class RemoveXFA {
  21.  
  22.     public static final String SRC = "resources/pdfs/reportcardinitial.pdf";
  23.     public static final String DEST = "results/acroforms/reportcard.pdf";
  24.    
  25.     public static void main(String[] args) throws DocumentException, IOException {
  26.         File file = new File(DEST);
  27.         file.getParentFile().mkdirs();
  28.         new RemoveXFA().manipulatePdf(SRC, DEST);
  29.     }
  30.  
  31.     public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
  32.         PdfReader reader = new PdfReader(src);
  33.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  34.         AcroFields form = stamper.getAcroFields();
  35.         form.removeXfa();
  36.         Map fields = form.getFields();
  37.         for (String name : fields.keySet()) {
  38.             if (name.indexOf("Total") > 0)
  39.                 form.setFieldProperty(name, "textcolor", BaseColor.RED, null);
  40.             form.setField(name, "X");
  41.         }
  42.         stamper.close();
  43.         reader.close();
  44.     }
  45. }
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