Package com.itextpdf.forms.xfa
Class XfaForm
- java.lang.Object
-
- com.itextpdf.forms.xfa.XfaForm
-
public class XfaForm extends java.lang.ObjectProcesses XFA forms.
-
-
Field Summary
Fields Modifier and Type Field Description private AcroFieldsSearchacroFieldsSomprivate org.w3c.dom.NodedatasetsNodeprivate Xml2SomDatasetsdatasetsSomprivate org.w3c.dom.DocumentdomDocumentprivate static intINIT_SERIALIZER_BUFFER_SIZEprivate org.w3c.dom.NodetemplateNodestatic java.lang.StringXFA_DATA_SCHEMAThe URI for the XFA Data schema.private booleanxfaPresent
-
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 theDocumentcontaining all xml information
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidcreateDatasetsNode(org.w3c.dom.Node n)Some XFA forms don't have a datasets node.private voidextractNodes()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.voidfillXfaForm(java.io.File file)Replaces the XFA data under datasets/data.voidfillXfaForm(java.io.File file, boolean readOnly)Replaces the XFA data under datasets/data.voidfillXfaForm(java.io.InputStream is)Replaces the XFA data under datasets/data.voidfillXfaForm(java.io.InputStream is, boolean readOnly)Replaces the XFA data under datasets/data.voidfillXfaForm(org.w3c.dom.Node node)Replaces the XFA data under datasets/data.voidfillXfaForm(org.w3c.dom.Node node, boolean readOnly)Replaces the XFA data under datasets/data.voidfillXfaForm(org.xml.sax.InputSource is)Replaces the XFA data under datasets/data.voidfillXfaForm(org.xml.sax.InputSource is, boolean readOnly)Replaces the XFA data under datasets/data.private org.w3c.dom.NodefindDataNode(org.w3c.dom.Node datasetsNode)java.lang.StringfindDatasetsName(java.lang.String name)Finds the complete SOM name contained in the datasets section from a possibly partial name.org.w3c.dom.NodefindDatasetsNode(java.lang.String name)Finds theNodecontained in the datasets section from a possibly partial name.java.lang.StringfindFieldName(java.lang.String name)Finds the complete field name from a partial name.org.w3c.dom.NodegetDatasetsNode()Gets theNodethat corresponds to the datasets part.org.w3c.dom.DocumentgetDomDocument()Gets the top level DOM document.private org.w3c.dom.NodegetFirstElementNode(org.w3c.dom.Node src)static java.lang.StringgetNodeText(org.w3c.dom.Node n)Gets all the text contained in the child nodes of this node.private static java.lang.StringgetNodeText(org.w3c.dom.Node n, java.lang.String name)java.lang.StringgetNodeTextByPath(java.lang.String path)Gets all the text contained in the child nodes of the node under the provided path.java.lang.StringgetXfaFieldValue(java.lang.String name)Gets the xfa field value.private static PdfObjectgetXfaObject(PdfAcroForm acroForm)Return the XFA Object, could be an array, could be a Stream.private static PdfObjectgetXfaObject(PdfDocument pdfDocument)Return the XFA Object, could be an array, could be a Stream.private voidinitXfaForm(PdfObject xfa)private voidinitXfaForm(java.io.InputStream inputStream)booleanisXfaPresent()Returnstrueif it is a XFA form.private static byte[]serializeDocument(org.w3c.dom.Node n)Serializes a XML document to a byte array.voidsetDomDocument(org.w3c.dom.Document domDocument)Sets the top DOM document.voidsetNodeText(org.w3c.dom.Node n, java.lang.String text)Sets the text of this node.voidsetXfaFieldValue(java.lang.String name, java.lang.String value)Changes a field value in the XFA form.static voidsetXfaForm(XfaForm form, PdfAcroForm acroForm)Sets the XFA key from a byte array.static voidsetXfaForm(XfaForm form, PdfDocument pdfDocument)Sets the XFA key from a byte array.voidwrite(PdfAcroForm acroForm)Write the XfaForm to the providedPdfAcroForm.voidwrite(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 theDocumentcontaining 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 theXFAkey must contain correct XFA syntax. If theXFAkey 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- anPdfAcroForminstance- 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
Mapof 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()
Returnstrueif it is a XFA form.- Returns:
trueif 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
nullif 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
nullif not found
-
findDatasetsNode
public org.w3c.dom.Node findDatasetsNode(java.lang.String name)
Finds theNodecontained in the datasets section from a possibly partial name.- Parameters:
name- the complete or partial name- Returns:
- the
Nodeornullif 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
nullif 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- theNodeto 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 theNodethat corresponds to the datasets part.- Returns:
- the
Nodethat corresponds to the datasets part
-
fillXfaForm
public void fillXfaForm(java.io.File file) throws java.io.IOExceptionReplaces the XFA data under datasets/data. Accepts afile objectto 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.IOExceptionReplaces the XFA data under datasets/data. Accepts afile objectto fill this object with XFA data.- Parameters:
file- theFilereadOnly- 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.IOExceptionReplaces the XFA data under datasets/data. Accepts anInputStreamto 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.IOExceptionReplaces the XFA data under datasets/data. Accepts anInputStreamto fill this object with XFA data.- Parameters:
is- theInputStreamreadOnly- 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.IOExceptionReplaces the XFA data under datasets/data. Accepts aSAX input sourceto 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.IOExceptionReplaces the XFA data under datasets/data. Accepts aSAX input sourceto fill this object with XFA data.- Parameters:
is- theSAX input sourcereadOnly- 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 inputNodereadOnly- 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.IOExceptionSerializes 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.IOExceptionjavax.xml.parsers.ParserConfigurationExceptionorg.xml.sax.SAXException
-
initXfaForm
private void initXfaForm(java.io.InputStream inputStream) throws java.io.IOException, org.xml.sax.SAXException- Throws:
java.io.IOExceptionorg.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)
-
-