简体   繁体   English

如何使用docx4j阅读word文档并使用所有样式获取部分文档

[英]How to read word document and get parts of it with all styles using docx4j

I am using docx4j to deal with word document formatting. 我使用docx4j来处理word文档格式。 I have one word document which is divided in number of tables. 我有一个word文档,它按表的数量划分。 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. 我想阅读所有表格,如果我找到一些关键字,那么我想将这些内容带到另一个包含所有格式的word文档。 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. 我的关键字是Sample Text。 So whenever Sample Text gets repeated, content needs to be fetched to new word document. 因此,每当重复示例文本时,都需要将内容提取到新的word文档。

I am using following code. 我正在使用以下代码。

    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 );

Assuming your text is in a single run (ie not split across runs), then you can search for it via XPath. 假设您的文本是单次运行(即不跨运行分割),那么您可以通过XPath搜索它。 Or you can manually traverse using TraversalUtil. 或者您可以使用TraversalUtil手动遍历。 See docx4j's Getting Started for more info. 有关详细信息,请参阅docx4j的入门。

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. 有关所涉及的问题,请参阅我的文章http://www.docx4java.org/blog/2010/11/merging-word-documents/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM