匯出資料
從PDF檔案填入調適型表單的第一步,是匯出指定PDF檔案的資料,並將其儲存在AEM存放庫中。
下列程式碼是用來從上傳的pdf中擷取資料,並經過調整以取得填入最適化表單時所用的正確格式
public String getFormData(Document pdfForm) {
DocumentBuilderFactory factory = null;
DocumentBuilder builder = null;
org.w3c.dom.Document xmlDocument = null;
try {
Document xmlData = formsService.exportData(pdfForm, DataFormat.Auto);
xmlData.copyToFile(new File("xmlData.xml"));
factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
builder = factory.newDocumentBuilder();
xmlDocument = builder.parse(xmlData.getInputStream());
org.w3c.dom.Node xdpNode = xmlDocument.getDocumentElement();
log.debug("Got xdp " + xdpNode.getNodeName());
org.w3c.dom.Node datasets = getChildByTagName(xdpNode, "xfa:datasets");
log.debug("Got datasets " + datasets.getNodeName());
org.w3c.dom.Node data = getChildByTagName(datasets, "xfa:data");
log.debug("Got data " + data.getNodeName());
org.w3c.dom.Node topmostSubform = getChildByTagName(data, "topmostSubform");
if (topmostSubform != null) {
org.w3c.dom.Document newXmlDocument = builder.newDocument();
org.w3c.dom.Node importedNode = newXmlDocument.importNode(topmostSubform, true);
newXmlDocument.appendChild(importedNode);
Document aemFDXmlDocument = documentServices.orgw3cDocumentToAEMFDDocument(newXmlDocument);
aemFDXmlDocument.copyToFile(new File("aemFDXmlDocument.xml"));
// saveDocumentInCrx is an utility method of the custom DocumentServices service.
return documentServices.saveDocumentInCrx("/content/exporteddata", ".xml", aemFDXmlDocument);
}
} catch (Exception e) {
log.debug("Error: " + e.getMessage());
}
return null;
}
以下是撰寫的公用程式函式,用於擷取 topmostSubForm 與適當的名稱空間
private static org.w3c.dom.Node getChildByTagName(org.w3c.dom.Node parent, String tagName) {
NodeList nodeList = parent.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
org.w3c.dom.Node node = nodeList.item(i);
if (node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE && node.getNodeName().equals(tagName)) {
return node;
}
}
return null;
}
擷取的資料會儲存在crx存放庫的/content/exporteddata節點下。 然後,匯出資料的檔案路徑會傳回至呼叫的應用程式,以填入調適型表單。
8de24117-1378-413c-a581-01e660b7163e