Search code examples
javadocx4j

Create table in word doc using docx4j in specific bookmark:


I’m creating a table in specific bookmark location (means it's adisplay table in word doc), but after converting word to PDF it can't show table in PDF because of bookmark is inside a w:p!!

<w:p w:rsidR="00800BD9" w:rsidRDefault="00800BD9">  
  <w:bookmarkStart w:id="0" w:name="abc"/>
  <w:bookmarkEnd w:id="0"/>
</w:p>

Now I want to find the paragraph (using the bookmark), then replace the paragraph with the table.

Does anybody have any suggestion?

Here is my code:

    private void replaceBookmarkContents(List<Object> paragraphs,  Map<DataFieldName, String> data) throws Exception {

    Tbl table  = TblFactory.createTable(3,3,9600);
    RangeFinder rt = new RangeFinder("CTBookmark", "CTMarkupRange");
    new TraversalUtil(paragraphs, rt);

    for (CTBookmark bm : rt.getStarts()) {

        // do we have data for this one?
        if (bm.getName()==null) continue;
        String value = data.get(new DataFieldName(bm.getName()));
        if (value==null) continue;

        try {
            // Can't just remove the object from the parent,
            // since in the parent, it may be wrapped in a JAXBElement
            List<Object> theList = null;
            if (bm.getParent() instanceof P) {
                System.out.println("OK!");
                theList = ((ContentAccessor)(bm.getParent())).getContent();
            } else {
                continue;
            }

            int rangeStart = -1;
            int rangeEnd=-1;
            int i = 0;
            for (Object ox : theList) {
                Object listEntry = XmlUtils.unwrap(ox); 
                if (listEntry.equals(bm)) {
                    if (DELETE_BOOKMARK) {
                        rangeStart=i;
                    } else {
                        rangeStart=i+1;                         
                    }
                } else if (listEntry instanceof  CTMarkupRange) {
                    if ( ((CTMarkupRange)listEntry).getId().equals(bm.getId())) {
                        if (DELETE_BOOKMARK) {
                            rangeEnd=i;
                        } else {
                            rangeEnd=i-1;                           
                        }
                        break;
                    }
                }
                i++;
            }

            if (rangeStart>0) {

                // Delete the bookmark range
                for (int j=rangeStart; j>0; j--) {
                    theList.remove(j);
                }

                // now add a run
                org.docx4j.wml.R  run = factory.createR();
                run.getContent().add(table);
                theList.add(rangeStart, run);
            }

        } catch (ClassCastException cce) {
            log.error(cce.getMessage(), cce);
        }
    }
}

Solution

  • The problem with blindly replacing a bookmark inside a paragraph with a table is that you'll end up with w:p/w:tbl (or with your code, w:p/w:r/w:tbl!) which is not allowed by the file format.

    To avoid this issue, you could make the bookmark a sibling of the paragraph, or you could change your code so that once you have found a bookmark inside a paragraph, if you are replacing it with a table, you replace the parent p instead.

    Note that bookmark find/replace is a brittle basis for document generation. If your requirements are other than modest, you'd be better off using content controls instead.