3

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) {
        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 );
Anup Ganatra
  • 366
  • 1
  • 6
  • 23

1 Answers1

3

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 http://www.docx4java.org/blog/2010/11/merging-word-documents/ for more on the issues involved.

JasonPlutext
  • 15,352
  • 4
  • 44
  • 84
  • I am using code mentioned above. My text are splitted across multiple runs.How can I implement using XPath? Will it cover all styles and formatting from word document? – Anup Ganatra Jul 15 '14 at 11:33
  • 1
    Google 'VariablePrepare'; it may help you to deal with your split runs. Re XPath, Google will answer your question. – JasonPlutext Jul 15 '14 at 12:35
  • I am getting following error while using VariablePrepare : Exception in thread "main" java.lang.IllegalAccessError: tried to access class org.apache.xml.serializer.ExtendedContentHandler from class org.apache.xalan.transformer.TransformerImpl . I am using xalan-2.7.1 and serializer-2.7.0 – Anup Ganatra Jul 16 '14 at 13:06
  • You can create a separate question to get help with that. – JasonPlutext Jul 16 '14 at 20:41