Package com.itextpdf.forms.xfa
Class XfaForm
- java.lang.Object
-
- com.itextpdf.forms.xfa.XfaForm
-
public class XfaForm extends java.lang.Object
Processes XFA forms.
-
-
Field Summary
Fields Modifier and Type Field Description private AcroFieldsSearch
acroFieldsSom
private org.w3c.dom.Node
datasetsNode
private Xml2SomDatasets
datasetsSom
private org.w3c.dom.Document
domDocument
private static int
INIT_SERIALIZER_BUFFER_SIZE
private org.w3c.dom.Node
templateNode
static java.lang.String
XFA_DATA_SCHEMA
The URI for the XFA Data schema.private boolean
xfaPresent
-
Constructor Summary
Constructors Constructor Description XfaForm()
An empty constructor to build on.XfaForm(PdfDictionary acroFormDictionary)
A constructor from aPdfDictionary
.XfaForm(PdfDocument pdfDocument)
A constructor from aPdfDocument
.XfaForm(java.io.InputStream inputStream)
Creates an XFA form by the stream containing all xml informationXfaForm(org.w3c.dom.Document domDocument)
Creates an XFA form by theDocument
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 theNode
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 theNode
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()
Returnstrue
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 providedPdfAcroForm
.void
write(PdfDocument document)
Write the XfaForm to the providedPdfDocument
.
-
-
-
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
-
datasetsSom
private Xml2SomDatasets datasetsSom
-
datasetsNode
private org.w3c.dom.Node datasetsNode
-
acroFieldsSom
private AcroFieldsSearch acroFieldsSom
-
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 theDocument
containing all xml information- Parameters:
domDocument
- The document
-
XfaForm
public XfaForm(PdfDictionary acroFormDictionary)
A constructor from aPdfDictionary
. It is assumed, but not necessary for correct initialization, that the dictionary is actually aPdfAcroForm
. An entry in the dictionary with theXFA
key must contain correct XFA syntax. If theXFA
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 aPdfDocument
. 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 datapdfDocument
- 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 dataacroForm
- anPdfAcroForm
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 aDOM document
- Returns:
- a
Map
of XFA packet names and their associatedDOM nodes
-
write
public void write(PdfDocument document) throws java.io.IOException
Write the XfaForm to the providedPdfDocument
.- 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 providedPdfAcroForm
.- 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 changedvalue
- 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()
Returnstrue
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 theNode
contained in the datasets section from a possibly partial name.- Parameters:
name
- the complete or partial name- Returns:
- the
Node
ornull
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
- theNode
- 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
- theNode
to add the text totext
- 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 theNode
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 afile object
to fill this object with XFA data. The resulting DOM document may be modified.- Parameters:
file
- theFile
- Throws:
java.io.IOException
- if any I/O issue occurs on theInputSource
-
fillXfaForm
public void fillXfaForm(java.io.File file, boolean readOnly) throws java.io.IOException
Replaces the XFA data under datasets/data. Accepts afile object
to fill this object with XFA data.- Parameters:
file
- theFile
readOnly
- whether or not the resulting DOM document may be modified- Throws:
java.io.IOException
- if any I/O issue occurs on theInputSource
-
fillXfaForm
public void fillXfaForm(java.io.InputStream is) throws java.io.IOException
Replaces the XFA data under datasets/data. Accepts anInputStream
to fill this object with XFA data. The resulting DOM document may be modified.- Parameters:
is
- theInputStream
- Throws:
java.io.IOException
- if any I/O issue occurs on theInputSource
-
fillXfaForm
public void fillXfaForm(java.io.InputStream is, boolean readOnly) throws java.io.IOException
Replaces the XFA data under datasets/data. Accepts anInputStream
to fill this object with XFA data.- Parameters:
is
- theInputStream
readOnly
- whether or not the resulting DOM document may be modified- Throws:
java.io.IOException
- if any I/O issue occurs on theInputSource
-
fillXfaForm
public void fillXfaForm(org.xml.sax.InputSource is) throws java.io.IOException
Replaces the XFA data under datasets/data. Accepts aSAX input source
to fill this object with XFA data. The resulting DOM document may be modified.- Parameters:
is
- theSAX input source
- Throws:
java.io.IOException
- if any I/O issue occurs on theInputSource
-
fillXfaForm
public void fillXfaForm(org.xml.sax.InputSource is, boolean readOnly) throws java.io.IOException
Replaces the XFA data under datasets/data. Accepts aSAX input source
to fill this object with XFA data.- Parameters:
is
- theSAX input source
readOnly
- whether or not the resulting DOM document may be modified- Throws:
java.io.IOException
- if any I/O issue occurs on theInputSource
-
fillXfaForm
public void fillXfaForm(org.w3c.dom.Node node)
Replaces the XFA data under datasets/data.- Parameters:
node
- the inputNode
-
fillXfaForm
public void fillXfaForm(org.w3c.dom.Node node, boolean readOnly)
Replaces the XFA data under datasets/data.- Parameters:
node
- the inputNode
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)
-
-