Class XfaForm


  • public class XfaForm
    extends java.lang.Object
    Processes XFA forms.
    • Constructor Summary

      Constructors 
      Constructor Description
      XfaForm()
      An empty constructor to build on.
      XfaForm​(PdfDictionary acroFormDictionary)
      A constructor from a PdfDictionary.
      XfaForm​(PdfDocument pdfDocument)
      A constructor from a PdfDocument.
      XfaForm​(java.io.InputStream inputStream)
      Creates an XFA form by the stream containing all xml information
      XfaForm​(org.w3c.dom.Document domDocument)
      Creates an XFA form by the Document containing all xml information
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void createDatasetsNode​(org.w3c.dom.Node n)
      Some XFA forms don't have a datasets node.
      private void extractNodes()
      Extracts the nodes from the domDocument.
      static java.util.Map<java.lang.String,​org.w3c.dom.Node> extractXFANodes​(org.w3c.dom.Document domDocument)
      Extracts DOM nodes from an XFA document.
      void fillXfaForm​(java.io.File file)
      Replaces the XFA data under datasets/data.
      void fillXfaForm​(java.io.File file, boolean readOnly)
      Replaces the XFA data under datasets/data.
      void fillXfaForm​(java.io.InputStream is)
      Replaces the XFA data under datasets/data.
      void fillXfaForm​(java.io.InputStream is, boolean readOnly)
      Replaces the XFA data under datasets/data.
      void fillXfaForm​(org.w3c.dom.Node node)
      Replaces the XFA data under datasets/data.
      void fillXfaForm​(org.w3c.dom.Node node, boolean readOnly)
      Replaces the XFA data under datasets/data.
      void fillXfaForm​(org.xml.sax.InputSource is)
      Replaces the XFA data under datasets/data.
      void fillXfaForm​(org.xml.sax.InputSource is, boolean readOnly)
      Replaces the XFA data under datasets/data.
      private org.w3c.dom.Node findDataNode​(org.w3c.dom.Node datasetsNode)  
      java.lang.String findDatasetsName​(java.lang.String name)
      Finds the complete SOM name contained in the datasets section from a possibly partial name.
      org.w3c.dom.Node findDatasetsNode​(java.lang.String name)
      Finds the Node contained in the datasets section from a possibly partial name.
      java.lang.String findFieldName​(java.lang.String name)
      Finds the complete field name from a partial name.
      org.w3c.dom.Node getDatasetsNode()
      Gets the Node that corresponds to the datasets part.
      org.w3c.dom.Document getDomDocument()
      Gets the top level DOM document.
      private org.w3c.dom.Node getFirstElementNode​(org.w3c.dom.Node src)  
      static java.lang.String getNodeText​(org.w3c.dom.Node n)
      Gets all the text contained in the child nodes of this node.
      private static java.lang.String getNodeText​(org.w3c.dom.Node n, java.lang.String name)  
      java.lang.String getNodeTextByPath​(java.lang.String path)
      Gets all the text contained in the child nodes of the node under the provided path.
      java.lang.String getXfaFieldValue​(java.lang.String name)
      Gets the xfa field value.
      private static PdfObject getXfaObject​(PdfAcroForm acroForm)
      Return the XFA Object, could be an array, could be a Stream.
      private static PdfObject getXfaObject​(PdfDocument pdfDocument)
      Return the XFA Object, could be an array, could be a Stream.
      private void initXfaForm​(PdfObject xfa)  
      private void initXfaForm​(java.io.InputStream inputStream)  
      boolean isXfaPresent()
      Returns true if it is a XFA form.
      private static byte[] serializeDocument​(org.w3c.dom.Node n)
      Serializes a XML document to a byte array.
      void setDomDocument​(org.w3c.dom.Document domDocument)
      Sets the top DOM document.
      void setNodeText​(org.w3c.dom.Node n, java.lang.String text)
      Sets the text of this node.
      void setXfaFieldValue​(java.lang.String name, java.lang.String value)
      Changes a field value in the XFA form.
      static void setXfaForm​(XfaForm form, PdfAcroForm acroForm)
      Sets the XFA key from a byte array.
      static void setXfaForm​(XfaForm form, PdfDocument pdfDocument)
      Sets the XFA key from a byte array.
      void write​(PdfAcroForm acroForm)
      Write the XfaForm to the provided PdfAcroForm.
      void write​(PdfDocument document)
      Write the XfaForm to the provided PdfDocument.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • INIT_SERIALIZER_BUFFER_SIZE

        private static final int INIT_SERIALIZER_BUFFER_SIZE
        See Also:
        Constant Field Values
      • templateNode

        private org.w3c.dom.Node templateNode
      • datasetsNode

        private org.w3c.dom.Node datasetsNode
      • xfaPresent

        private boolean xfaPresent
      • domDocument

        private org.w3c.dom.Document domDocument
      • XFA_DATA_SCHEMA

        public static final java.lang.String XFA_DATA_SCHEMA
        The URI for the XFA Data schema.
        See Also:
        Constant Field Values
    • Constructor Detail

      • XfaForm

        public XfaForm()
        An empty constructor to build on.
      • XfaForm

        public XfaForm​(java.io.InputStream inputStream)
        Creates an XFA form by the stream containing all xml information
        Parameters:
        inputStream - the InputStream
      • XfaForm

        public XfaForm​(org.w3c.dom.Document domDocument)
        Creates an XFA form by the Document containing all xml information
        Parameters:
        domDocument - The document
      • XfaForm

        public XfaForm​(PdfDictionary acroFormDictionary)
        A constructor from a PdfDictionary. It is assumed, but not necessary for correct initialization, that the dictionary is actually a PdfAcroForm. An entry in the dictionary with the XFA key must contain correct XFA syntax. If the XFA key is absent, then the constructor essentially does nothing.
        Parameters:
        acroFormDictionary - the dictionary object to initialize from
      • XfaForm

        public XfaForm​(PdfDocument pdfDocument)
        A constructor from a PdfDocument. It basically does everything from finding the XFA stream to the XML parsing.
        Parameters:
        pdfDocument - the PdfDocument instance
    • Method Detail

      • setXfaForm

        public static void setXfaForm​(XfaForm form,
                                      PdfDocument pdfDocument)
                               throws java.io.IOException
        Sets the XFA key from a byte array. The old XFA is erased.
        Parameters:
        form - the data
        pdfDocument - pdfDocument
        Throws:
        java.io.IOException - if any I/O issue occurs
      • setXfaForm

        public static void setXfaForm​(XfaForm form,
                                      PdfAcroForm acroForm)
                               throws java.io.IOException
        Sets the XFA key from a byte array. The old XFA is erased.
        Parameters:
        form - the data
        acroForm - an PdfAcroForm instance
        Throws:
        java.io.IOException - if any I/O issue occurs
      • extractXFANodes

        public static java.util.Map<java.lang.String,​org.w3c.dom.Node> extractXFANodes​(org.w3c.dom.Document domDocument)
        Extracts DOM nodes from an XFA document.
        Parameters:
        domDocument - an XFA file as a DOM document
        Returns:
        a Map of XFA packet names and their associated DOM nodes
      • write

        public void write​(PdfDocument document)
                   throws java.io.IOException
        Write the XfaForm to the provided PdfDocument.
        Parameters:
        document - the PdfDocument to write the XFA Form to
        Throws:
        java.io.IOException - if any I/O issue occurs
      • write

        public void write​(PdfAcroForm acroForm)
                   throws java.io.IOException
        Write the XfaForm to the provided PdfAcroForm.
        Parameters:
        acroForm - the PdfDocument to write the XFA Form to
        Throws:
        java.io.IOException - if any I/O issue occurs
      • setXfaFieldValue

        public void setXfaFieldValue​(java.lang.String name,
                                     java.lang.String value)
        Changes a field value in the XFA form.
        Parameters:
        name - the name of the field to be changed
        value - the new value
      • getXfaFieldValue

        public java.lang.String getXfaFieldValue​(java.lang.String name)
        Gets the xfa field value.
        Parameters:
        name - the fully qualified field name
        Returns:
        the field value
      • isXfaPresent

        public boolean isXfaPresent()
        Returns true if it is a XFA form.
        Returns:
        true if it is a XFA form
      • findFieldName

        public java.lang.String findFieldName​(java.lang.String name)
        Finds the complete field name from a partial name.
        Parameters:
        name - the complete or partial name
        Returns:
        the complete name or null if not found
      • findDatasetsName

        public java.lang.String findDatasetsName​(java.lang.String name)
        Finds the complete SOM name contained in the datasets section from a possibly partial name.
        Parameters:
        name - the complete or partial name
        Returns:
        the complete name or null if not found
      • findDatasetsNode

        public org.w3c.dom.Node findDatasetsNode​(java.lang.String name)
        Finds the Node contained in the datasets section from a possibly partial name.
        Parameters:
        name - the complete or partial name
        Returns:
        the Node or null if not found
      • getNodeText

        public static java.lang.String getNodeText​(org.w3c.dom.Node n)
        Gets all the text contained in the child nodes of this node.
        Parameters:
        n - the Node
        Returns:
        the text found or "" if no text was found
      • getNodeTextByPath

        public java.lang.String getNodeTextByPath​(java.lang.String path)
        Gets all the text contained in the child nodes of the node under the provided path.
        Parameters:
        path - path to the node to extract text in the format "some.path.to.node"
        Returns:
        text found under the provided path or null if node or text wasn't found
      • setNodeText

        public void setNodeText​(org.w3c.dom.Node n,
                                java.lang.String text)
        Sets the text of this node. All the child's node are deleted and a new child text node is created.
        Parameters:
        n - the Node to add the text to
        text - the text to add
      • getDomDocument

        public org.w3c.dom.Document getDomDocument()
        Gets the top level DOM document.
        Returns:
        the top level DOM document
      • setDomDocument

        public void setDomDocument​(org.w3c.dom.Document domDocument)
        Sets the top DOM document.
        Parameters:
        domDocument - the top DOM document
      • getDatasetsNode

        public org.w3c.dom.Node getDatasetsNode()
        Gets the Node that corresponds to the datasets part.
        Returns:
        the Node that corresponds to the datasets part
      • fillXfaForm

        public void fillXfaForm​(java.io.File file)
                         throws java.io.IOException
        Replaces the XFA data under datasets/data. Accepts a file object to fill this object with XFA data. The resulting DOM document may be modified.
        Parameters:
        file - the File
        Throws:
        java.io.IOException - if any I/O issue occurs on the InputSource
      • fillXfaForm

        public void fillXfaForm​(java.io.File file,
                                boolean readOnly)
                         throws java.io.IOException
        Replaces the XFA data under datasets/data. Accepts a file object to fill this object with XFA data.
        Parameters:
        file - the File
        readOnly - whether or not the resulting DOM document may be modified
        Throws:
        java.io.IOException - if any I/O issue occurs on the InputSource
      • fillXfaForm

        public void fillXfaForm​(java.io.InputStream is)
                         throws java.io.IOException
        Replaces the XFA data under datasets/data. Accepts an InputStream to fill this object with XFA data. The resulting DOM document may be modified.
        Parameters:
        is - the InputStream
        Throws:
        java.io.IOException - if any I/O issue occurs on the InputSource
      • fillXfaForm

        public void fillXfaForm​(java.io.InputStream is,
                                boolean readOnly)
                         throws java.io.IOException
        Replaces the XFA data under datasets/data. Accepts an InputStream to fill this object with XFA data.
        Parameters:
        is - the InputStream
        readOnly - whether or not the resulting DOM document may be modified
        Throws:
        java.io.IOException - if any I/O issue occurs on the InputSource
      • fillXfaForm

        public void fillXfaForm​(org.xml.sax.InputSource is)
                         throws java.io.IOException
        Replaces the XFA data under datasets/data. Accepts a SAX input source to fill this object with XFA data. The resulting DOM document may be modified.
        Parameters:
        is - the SAX input source
        Throws:
        java.io.IOException - if any I/O issue occurs on the InputSource
      • fillXfaForm

        public void fillXfaForm​(org.xml.sax.InputSource is,
                                boolean readOnly)
                         throws java.io.IOException
        Replaces the XFA data under datasets/data. Accepts a SAX input source to fill this object with XFA data.
        Parameters:
        is - the SAX input source
        readOnly - whether or not the resulting DOM document may be modified
        Throws:
        java.io.IOException - if any I/O issue occurs on the InputSource
      • fillXfaForm

        public void fillXfaForm​(org.w3c.dom.Node node)
        Replaces the XFA data under datasets/data.
        Parameters:
        node - the input Node
      • fillXfaForm

        public void fillXfaForm​(org.w3c.dom.Node node,
                                boolean readOnly)
        Replaces the XFA data under datasets/data.
        Parameters:
        node - the input Node
        readOnly - whether or not the resulting DOM document may be modified
      • getNodeText

        private static java.lang.String getNodeText​(org.w3c.dom.Node n,
                                                    java.lang.String name)
      • getXfaObject

        private static PdfObject getXfaObject​(PdfDocument pdfDocument)
        Return the XFA Object, could be an array, could be a Stream. Returns null if no XFA Object is present.
        Parameters:
        pdfDocument - a PdfDocument instance
        Returns:
        the XFA object
      • getXfaObject

        private static PdfObject getXfaObject​(PdfAcroForm acroForm)
        Return the XFA Object, could be an array, could be a Stream. Returns null if no XFA Object is present.
        Parameters:
        acroForm - a PdfDocument instance
        Returns:
        the XFA object
      • serializeDocument

        private static byte[] serializeDocument​(org.w3c.dom.Node n)
                                         throws java.io.IOException
        Serializes a XML document to a byte array.
        Parameters:
        n - the XML document
        Returns:
        the serialized XML document
        Throws:
        java.io.IOException - if any I/O issue occurs
      • initXfaForm

        private void initXfaForm​(PdfObject xfa)
                          throws java.io.IOException,
                                 javax.xml.parsers.ParserConfigurationException,
                                 org.xml.sax.SAXException
        Throws:
        java.io.IOException
        javax.xml.parsers.ParserConfigurationException
        org.xml.sax.SAXException
      • initXfaForm

        private void initXfaForm​(java.io.InputStream inputStream)
                          throws java.io.IOException,
                                 org.xml.sax.SAXException
        Throws:
        java.io.IOException
        org.xml.sax.SAXException
      • extractNodes

        private void extractNodes()
        Extracts the nodes from the domDocument.
      • createDatasetsNode

        private void createDatasetsNode​(org.w3c.dom.Node n)
        Some XFA forms don't have a datasets node. If this is the case, we have to add one.
      • getFirstElementNode

        private org.w3c.dom.Node getFirstElementNode​(org.w3c.dom.Node src)
      • findDataNode

        private org.w3c.dom.Node findDataNode​(org.w3c.dom.Node datasetsNode)