【问题标题】:How to extract XML from XFA PDF document in Java using iText 7 (or other)?如何使用 iText 7(或其他)从 Java 中的 XFA PDF 文档中提取 XML?
【发布时间】: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]

【问题讨论】:

    标签: java xml pdf itext


    【解决方案1】:

    您有一个纯 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

    对此进行了解释

    我使用这个 sn-p 在 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());
    }
    

    屏幕上的输出是 XDP XML 文件,其中包含我预期的所有 XFA 信息。

    请注意,替换 XFA XML 文件时要小心。最好不要干预 XFA 结构,而是创建一个 XML 文件,其中只包含使用适当模式创建的数据,并按照常见问题解答中所述填写表单:How to fill out a pdf file programmatically? (Dynamic XFA)

    【讨论】:

    • 正是我所追求的!完美运行!谢谢!
    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 2016-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多