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. 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.

enter image description here

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.

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) {
        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;
            // Get all the Cells in the Row
            List<Object> allCells = DocxUtility.getDocxUtility()
                    .getAllElementFromObject(tr, Tc.class);
            for (Object cell : allCells) {
                Tc tc = (Tc) cell;
                // 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;
                    // 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) {
                            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. Or you can manually traverse using TraversalUtil. See docx4j's Getting Started for more info.

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 for more on the issues involved.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.