【发布时间】:2018-05-26 11:36:40
【问题描述】:
使用 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]
【问题讨论】: