Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
*
* This file is part of the iText (R) project.
* Copyright (c) 1998-2016 iText Group NV
* Authors: Bruno Lowagie, Paulo Soares, et al.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License version 3
* as published by the Free Software Foundation with the addition of the
* following permission added to Section 15 as permitted in Section 7(a):
* FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
* ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
* OF THIRD PARTY RIGHTS
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program; if not, see http://www.gnu.org/licenses or write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA, 02110-1301 USA, or download the license from the following URL:
* http://itextpdf.com/terms-of-use/
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License.
*
* In accordance with Section 7(b) of the GNU Affero General Public License,
* a covered work must retain the producer line in every PDF that is created
* or manipulated using iText.
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial activities involving the iText software without
* disclosing the source code of your own applications.
* These activities include: offering paid services to customers as an ASP,
* serving PDFs on the fly in a web application, shipping iText with a closed
* source product.
*
* For more information, please contact iText Software Corp. at this
* address: [email protected]
*/
package com.itextpdf.text.pdf;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.xml.XmlDomWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EmptyStackException;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/**
* Processes XFA forms.
* @author Paulo Soares
*/
public class XfaForm {
private Xml2SomTemplate templateSom;
private Node templateNode;
private Xml2SomDatasets datasetsSom;
private Node datasetsNode;
private AcroFieldsSearch acroFieldsSom;
private PdfReader reader;
private boolean xfaPresent;
private org.w3c.dom.Document domDocument;
private boolean changed;
public static final String XFA_DATA_SCHEMA = "http://www.xfa.org/schema/xfa-data/1.0/";
/**
* An empty constructor to build on.
*/
public XfaForm() {
}
/**
* Return the XFA Object, could be an array, could be a Stream.
* Returns null f no XFA Object is present.
* @param reader a PdfReader instance
* @return the XFA object
* @since 2.1.3
*/
public static PdfObject getXfaObject(PdfReader reader) {
PdfDictionary af = (PdfDictionary)PdfReader.getPdfObjectRelease(reader.getCatalog().get(PdfName.ACROFORM));
if (af == null) {
return null;
}
return PdfReader.getPdfObjectRelease(af.get(PdfName.XFA));
}
/**
* A constructor from a PdfReader. It basically does everything
* from finding the XFA stream to the XML parsing.
* @param reader the reader
* @throws java.io.IOException on error
* @throws javax.xml.parsers.ParserConfigurationException on error
* @throws org.xml.sax.SAXException on error
*/
public XfaForm(PdfReader reader) throws IOException, ParserConfigurationException, SAXException {
this.reader = reader;
PdfObject xfa = getXfaObject(reader);
if (xfa == null) {
xfaPresent = false;
return;
}
xfaPresent = true;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
if (xfa.isArray()) {
PdfArray ar = (PdfArray)xfa;
for (int k = 1; k < ar.size(); k += 2) {
PdfObject ob = ar.getDirectObject(k);
if (ob instanceof PRStream) {
byte[] b = PdfReader.getStreamBytes((PRStream)ob);
bout.write(b);
}
}
}
else if (xfa instanceof PRStream) {
byte[] b = PdfReader.getStreamBytes((PRStream)xfa);
bout.write(b);
}
bout.close();
DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
fact.setNamespaceAware(true);
DocumentBuilder db = fact.newDocumentBuilder();
domDocument = db.parse(new ByteArrayInputStream(bout.toByteArray()));
extractNodes();
}
/**
* Extracts the nodes from the domDocument.
* @since 2.1.5
*/
private void extractNodes() {
Map xfaNodes = extractXFANodes(domDocument);
if (xfaNodes.containsKey("template")) {
templateNode = xfaNodes.get("template");
templateSom = new Xml2SomTemplate(templateNode);
}
if (xfaNodes.containsKey("datasets")) {
datasetsNode = xfaNodes.get("datasets");
Node dataNode = findDataNode(datasetsNode);
datasetsSom = new Xml2SomDatasets(dataNode != null ? dataNode : datasetsNode.getFirstChild());
}
if (datasetsNode == null)
createDatasetsNode(domDocument.getFirstChild());
}
private Node findDataNode(Node datasetsNode) {
NodeList childNodes = datasetsNode.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
if (childNodes.item(i).getNodeName().equals("xfa:data")) {
return childNodes.item(i);
}
}
return null;
}
public static Map extractXFANodes(Document domDocument) {
Map xfaNodes = new HashMap();
Node n = domDocument.getFirstChild();
while (n.getChildNodes().getLength() == 0) {
n = n.getNextSibling();
}
n = n.getFirstChild();
while (n != null) {
if (n.getNodeType() == Node.ELEMENT_NODE) {
String s = n.getLocalName();
xfaNodes.put(s, n);
}
n = n.getNextSibling();
}
return xfaNodes;
}
/**
* Some XFA forms don't have a datasets node.
* If this is the case, we have to add one.
*/
private void createDatasetsNode(Node n) {
while (n.getChildNodes().getLength() == 0) {
n = n.getNextSibling();
}
if (n != null) {
Element e = n.getOwnerDocument().createElement("xfa:datasets");
e.setAttribute("xmlns:xfa", XFA_DATA_SCHEMA);
datasetsNode = e;
n.appendChild(datasetsNode);
}
}
/**
* Sets the XFA key from a byte array. The old XFA is erased.
* @param form the data
* @param reader the reader
* @param writer the writer
* @throws java.io.IOException on error
*/
public static void setXfa(XfaForm form, PdfReader reader, PdfWriter writer) throws IOException {
PdfDictionary af = (PdfDictionary)PdfReader.getPdfObjectRelease(reader.getCatalog().get(PdfName.ACROFORM));
if (af == null) {
return;
}
PdfObject xfa = getXfaObject(reader);
if (xfa.isArray()) {
PdfArray ar = (PdfArray)xfa;
int t = -1;
int d = -1;
for (int k = 0; k < ar.size(); k += 2) {
PdfString s = ar.getAsString(k);
if ("template".equals(s.toString())) {
t = k + 1;
}
if ("datasets".equals(s.toString())) {
d = k + 1;
}
}
if (t > -1 && d > -1) {
reader.killXref(ar.getAsIndirectObject(t));
reader.killXref(ar.getAsIndirectObject(d));
PdfStream tStream = new PdfStream(serializeDoc(form.templateNode));
tStream.flateCompress(writer.getCompressionLevel());
ar.set(t, writer.addToBody(tStream).getIndirectReference());
PdfStream dStream = new PdfStream(serializeDoc(form.datasetsNode));
dStream.flateCompress(writer.getCompressionLevel());
ar.set(d, writer.addToBody(dStream).getIndirectReference());
af.put(PdfName.XFA, new PdfArray(ar));
return;
}
}
reader.killXref(af.get(PdfName.XFA));
PdfStream str = new PdfStream(serializeDoc(form.domDocument));
str.flateCompress(writer.getCompressionLevel());
PdfIndirectReference ref = writer.addToBody(str).getIndirectReference();
af.put(PdfName.XFA, ref);
}
/**
* Sets the XFA key from the instance data. The old XFA is erased.
* @param writer the writer
* @throws java.io.IOException on error
*/
public void setXfa(PdfWriter writer) throws IOException {
setXfa(this, reader, writer);
}
/**
* Serializes a XML document to a byte array.
* @param n the XML document
* @throws java.io.IOException on error
* @return the serialized XML document
*/
public static byte[] serializeDoc(Node n) throws IOException {
XmlDomWriter xw = new XmlDomWriter();
ByteArrayOutputStream fout = new ByteArrayOutputStream();
xw.setOutput(fout, null);
xw.setCanonical(false);
xw.write(n);
fout.close();
return fout.toByteArray();
}
/**
* Returns true if it is a XFA form.
* @return true if it is a XFA form
*/
public boolean isXfaPresent() {
return xfaPresent;
}
/**
* Gets the top level DOM document.
* @return the top level DOM document
*/
public org.w3c.dom.Document getDomDocument() {
return domDocument;
}
/**
* Finds the complete field name contained in the "classic" forms from a partial
* name.
* @param name the complete or partial name
* @param af the fields
* @return the complete name or null if not found
*/
public String findFieldName(String name, AcroFields af) {
Map items = af.getFields();
if (items.containsKey(name))
return name;
if (acroFieldsSom == null) {
if (items.isEmpty() && xfaPresent)
acroFieldsSom = new AcroFieldsSearch(datasetsSom.getName2Node().keySet());
else
acroFieldsSom = new AcroFieldsSearch(items.keySet());
}
if (acroFieldsSom.getAcroShort2LongName().containsKey(name))
return acroFieldsSom.getAcroShort2LongName().get(name);
return acroFieldsSom.inverseSearchGlobal(Xml2Som.splitParts(name));
}
/**
* Finds the complete SOM name contained in the datasets section from a
* possibly partial name.
* @param name the complete or partial name
* @return the complete name or null if not found
*/
public String findDatasetsName(String name) {
if (datasetsSom.getName2Node().containsKey(name))
return name;
return datasetsSom.inverseSearchGlobal(Xml2Som.splitParts(name));
}
/**
* Finds the Node contained in the datasets section from a
* possibly partial name.
* @param name the complete or partial name
* @return the Node or null if not found
*/
public Node findDatasetsNode(String name) {
if (name == null)
return null;
name = findDatasetsName(name);
if (name == null)
return null;
return datasetsSom.getName2Node().get(name);
}
/**
* Gets all the text contained in the child nodes of this node.
* @param n the Node
* @return the text found or "" if no text was found
*/
public static String getNodeText(Node n) {
if (n == null)
return "";
return getNodeText(n, "");
}
private static String getNodeText(Node n, String name) {
Node n2 = n.getFirstChild();
while (n2 != null) {
if (n2.getNodeType() == Node.ELEMENT_NODE) {
name = getNodeText(n2, name);
}
else if (n2.getNodeType() == Node.TEXT_NODE) {
name += n2.getNodeValue();
}
n2 = n2.getNextSibling();
}
return name;
}
/**
* Sets the text of this node. All the child's node are deleted and a new
* child text node is created.
* @param n the Node to add the text to
* @param text the text to add
*/
public void setNodeText(Node n, String text) {
if (n == null)
return;
Node nc = null;
while ((nc = n.getFirstChild()) != null) {
n.removeChild(nc);
}
if (n.getAttributes().getNamedItemNS(XFA_DATA_SCHEMA, "dataNode") != null)
n.getAttributes().removeNamedItemNS(XFA_DATA_SCHEMA, "dataNode");
n.appendChild(domDocument.createTextNode(text));
changed = true;
}
/**
* Sets the XFA form flag signaling that this is a valid XFA form.
* @param xfaPresent the XFA form flag signaling that this is a valid XFA form
*/
public void setXfaPresent(boolean xfaPresent) {
this.xfaPresent = xfaPresent;
}
/**
* Sets the top DOM document.
* @param domDocument the top DOM document
*/
public void setDomDocument(org.w3c.dom.Document domDocument) {
this.domDocument = domDocument;
extractNodes();
}
/**
* Gets the PdfReader used by this instance.
* @return the PdfReader used by this instance
*/
public PdfReader getReader() {
return reader;
}
/**
* Sets the PdfReader to be used by this instance.
* @param reader the PdfReader to be used by this instance
*/
public void setReader(PdfReader reader) {
this.reader = reader;
}
/**
* Checks if this XFA form was changed.
* @return true if this XFA form was changed
*/
public boolean isChanged() {
return changed;
}
/**
* Sets the changed status of this XFA instance.
* @param changed the changed status of this XFA instance
*/
public void setChanged(boolean changed) {
this.changed = changed;
}
/**
* A structure to store each part of a SOM name and link it to the next part
* beginning from the lower hierarchy.
*/
public static class InverseStore {
protected ArrayList part = new ArrayList();
protected ArrayList