使用Java和iText 7,我试图从XFA PDF表单中提取XML数据以解析(并可能修改)数据,但是我能做的就是获取一些与任何XFA相同的基本通用数据我使用的文件。
我知道这是必须的,因为它是在iText RUPS工具中完成的,但是我已经圈了好几天了。
public class Parse {
private PdfDocument pdf;
private PdfAcroForm form;
private XfaForm xfa;
private Document domDocument;
private Map<Integer, String> data;
private int numberOfPages;
private String pdfText;
public void openPdf(String src, String dest) throws IOException, TransformerException {
PdfReader reader = new PdfReader(src);
reader.setUnethicalReading(true);
pdf = new PdfDocument(reader, new PdfWriter(dest));
form = PdfAcroForm.getAcroForm(pdf, true);
data = new HashMap<Integer, String>();
numberOfPages = getNumberOfPdfPages();
PdfPage currentPage;
String textFromPage;
for (int page = 1; page <= numberOfPages; page++) {
System.out.println("Reading page: " + page + " -----------------");
currentPage = pdf.getPage(page);
textFromPage = PdfTextExtractor.getTextFromPage(currentPage);
data.put(page, textFromPage);
pdfText += currentPage + ":" + "\n" + textFromPage + "\n";
}
xfa = form.getXfaForm();
domDocument = xfa.getDomDocument();
Map<String, Node> map = xfa.extractXFANodes(domDocument);
System.out.println("The template node = " + map.get("template").toString() + "\n");
System.out.println("Dom document = " + domDocument.toString() + "\n");
System.out.println("In map form = " + map.toString() + "\n");
System.out.println("pdfText = " + pdfText + "\n");
Node node = xfa.getDatasetsNode();
NodeList list = node.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
System.out.println("Get Child Nodes Output = " + list.item(i) + "\n");
}
}
}
这是我收到的通用输出。
Reading page: 1 -----------------
The template node = [template: null]
Dom document = [#document: null]
In map form = {template=[template: null], form=[form: null], xfdf=[xfdf: null], xmpmeta=[x:xmpmeta: null], datasets=[xfa:datasets: null], config=[config: null], PDFSecurity=[PDFSecurity: null]}
pdfText = nullcom.itextpdf.kernel.pdf.PdfPage@6fa38a:
> Please wait...
>
> If this message is not eventually replaced by the proper contents of
> the document, your PDF viewer may not be able to display this type of
> document. You can upgrade to the latest version of Adobe Reader
> for Windows®, Mac, or Linux® by visiting
> http://www.adobe.com/go/reader_download. For more assistance with
> Adobe Reader visit http://www.adobe.com/go/acrreader. Windows is
> either a registered trademark or a trademark of Microsoft Corporation
> in the United States and/or other countries. Mac is a trademark of
> Apple Inc., registered in the United States and other countries. Linux
> is the registered trademark of Linus Torvalds in the U.S. and other
> countries.
Get Child Nodes Output = [xfa:data: null]
最佳答案
您有一个纯XFA文件。这意味着此文件中存储的唯一PDF内容由“请稍候...”消息组成。该页面显示在不知道如何呈现XFA的PDF查看器中。
当您使用以下方法从页面中提取内容时,它也是获得的内容:
currentPage = pdf.getPage(page);
textFromPage = PdfTextExtractor.getTextFromPage(currentPage);
当面对纯XFA文件时,这是您不应该做的事情,因为所有相关内容都存储在PDF文件中存储的XML流中。
您已经拥有第一部分的权利:
xfa = form.getXfaForm();
domDocument = xfa.getDomDocument();
XFA流将在
/AcroForm
条目中找到。我知道这很尴尬,但这就是PDF的设计方式。这不是我们的选择,而且XFA在PDF 2.0中已弃用,因此XFA还是要死了。当XFA最终死亡并被掩埋时,该问题将消失。就是说,您有一个
org.w3c.dom.Document
的实例,并且想要获取存储在此对象中的XML文件。您不需要iText即可执行此操作。例如,在Converting a org.w3c.dom.Document in Java to String using Transformer中对此进行了解释我使用以下代码段在XFA文件上测试了该代码:
public static void main(String[] args) throws IOException, TransformerException {
PdfDocument pdf = new PdfDocument(new PdfReader(SRC));
PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
XfaForm xfa = form.getXfaForm();
Document doc = xfa.getDomDocument();
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(domSource, result);
writer.flush();
System.out.println(writer.toString());
}
屏幕上的输出是带有我期望的所有XFA信息的XDP XML文件。
请注意,替换XFA XML文件时要小心。最好不要混入XFA结构,而是创建一个XML文件,除了使用适当的模式创建的数据外,不包含任何内容,并按照FAQ中的形式填写表格:How to fill out a pdf file programmatically? (Dynamic XFA)