如何使用 docx4j 阅读 Word 文档并获取所有样式的部分内容 [英] How to read word document and get parts of it with all styles using docx4j

查看:47
本文介绍了如何使用 docx4j 阅读 Word 文档并获取所有样式的部分内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 docx4j 来处理 Word 文档格式.我有一个 word 文档,它分为多个表格.我想阅读所有表格,如果我找到一些关键字,那么我想将这些内容带到另一个带有所有格式的 Word 文档中.我的word文档如下.

I am using docx4j to deal with word document formatting. I have one word document which is divided in number of tables. I want to read all the tables and if I find some keywords then I want to take those contents to another word document with all the formatting. My word document is as follow.

就像从上面一样,我想取一些标题下面的内容.这里我的关键字是示例文本.因此,每当示例文本重复时,都需要将内容提取到新的 Word 文档中.

Like from above I want to take content which is below Some Title. Here my keyword is Sample Text. So whenever Sample Text gets repeated, content needs to be fetched to new word document.

我正在使用以下代码.

    MainDocumentPart mainDocumentPart = null;
    WordprocessingMLPackage docxFile = WordprocessingMLPackage.load(new File(fileName));
    mainDocumentPart = docxFile.getMainDocumentPart();

    WordprocessingMLPackage  wordMLPackage = WordprocessingMLPackage.createPackage();

    ClassFinder finder = new ClassFinder(Tbl.class);
    new TraversalUtil(mainDocumentPart.getContent(), finder);
    Tbl tbl = null;

    int noTbls = 0;
    int noRows = 0;
    int noCells = 0;
    int noParas = 0;
    int noTexts = 0;

    for (Object table : finder.results) {
        noTbls++;
        tbl = (Tbl) table;
        // Get all the Rows in the table
        List<Object> allRows = DocxUtility.getDocxUtility()
                .getAllElementFromObject(tbl, Tr.class);
        for (Object row : allRows) {
            Tr tr = (Tr) row;
            noRows++;
            // Get all the Cells in the Row
            List<Object> allCells = DocxUtility.getDocxUtility()
                    .getAllElementFromObject(tr, Tc.class);
            toCell:
            for (Object cell : allCells) {
                Tc tc = (Tc) cell;
                noCells++;
                // Get all the Paragraph's in the Cell
                List<Object> allParas = DocxUtility.getDocxUtility()
                        .getAllElementFromObject(tc, P.class);
                for (Object para : allParas) {
                    P p = (P) para;
                    noParas++;
                    // Get all the Run's in the Paragraph
                    List<Object> allRuns = DocxUtility.getDocxUtility()
                            .getAllElementFromObject(p, R.class);


                    for (Object run : allRuns) {
                        R r = (R) run;

                        // Get the Text in the Run
                        List<Object> allText = DocxUtility.getDocxUtility()
                                .getAllElementFromObject(r, Text.class);
                        for (Object text : allText) {
                            noTexts++;
                            Text txt = (Text) text;                         
                        }
                        System.out.println("No of Text in Para No: " + noParas + "are: " + noTexts);
                    }

                }
                System.out.println("No of Paras in Cell No: " + noCells + "are: " + noParas);
            }
            System.out.println("No of Cells in Row No: " + noRows + "are: " + noCells);
        }
        System.out.println("No of Rows in Table No: " + noTbls + "are: " + noRows);

    }
    System.out.println("Total no of Tables: " + noTbls );

推荐答案

假设您的文本在一个单独的运行中(即不跨运行拆分),那么您可以通过 XPath 搜索它.或者您可以使用 TraversalUtil 手动遍历.有关更多信息,请参阅 docx4j 的入门.

Assuming your text is in a single run (ie not split across runs), then you can search for it via XPath. Or you can manually traverse using TraversalUtil. See docx4j's Getting Started for more info.

所以找到你的东西很容易.复制它使用的格式以及其中的任何 rels,在一般情况下都很复杂.请参阅我的帖子 http://www.docx4java.org/blog/2010/11/merging-word-documents/ 有关所涉及问题的更多信息.

So finding your stuff is pretty easy. Copying the formatting it uses, and any rels in it, is in the general case, complicated. See my post http://www.docx4java.org/blog/2010/11/merging-word-documents/ for more on the issues involved.

这篇关于如何使用 docx4j 阅读 Word 文档并获取所有样式的部分内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆