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

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

问题描述

我使用docx4j来处理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.

从上面我想采取低于某些标题的内容。我的关键字是Sample Text。因此,每当重复示例文本时,都需要将内容提取到新的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.

所以找到你的东西很容易。在一般情况下,复制它使用的格式以及其中的任何相关内容都很复杂。请参阅我的帖子 http://www.docx4java.org/blog/2010 / 11 / merge-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天全站免登陆