简体   繁体   中英

Docx4j - indent table

I have a nice little problem with docx4j. I have a document which I build from a template (replace MailMerge fields and adding text to it) In this document I want to insert two tables. So far, everything works fine, no problem here. I just want to indent my tables, to put a tab in front of it.

I work with a class which represents the docx4j document and work with helper methods like these:

public void buildTable(Map<String, String> data, boolean indent) {      
    Tbl table = factory.createTbl();
    Tr tableRow = null;

    for (String key : data.keySet()) {
        tableRow = factory.createTr();

        addTableCell(tableRow, key);
        addTableCell(tableRow, data.get(key));
        table.getContent().add(tableRow);           
    }

    if (indent) {
        R run = factory.createR();
        P para = factory.createP();
        R.Tab tab = factory.createRTab();

        run.getContent().add(tab);
        para.getContent().add(run);

        table.setParent(para);

        documentPart.addObject(para);
        documentPart.addObject(table);

    } else {
        documentPart.addObject(table);
    }       
}

I have tried different things, the tab is inserted. But either my table isn't rendered or my it is appended below the tabs.
I tried to add the table to the run object -> No table
to the paragraph object -> No table
setting the paragraph as parent for the table and then add the table -> table below the tabbed paragraph

So... obviously I'm doing something wrong. Do you have a solution for this problem? I saw that the text object has a property like text.setSpace("preserve"); to preserve the space for the text, is there something similar for the table object?

Thanks in advance.

Matthias

A table is a block level element; its a sibling of paragraph, and can't be a descendant of it.

What you are after is tblInd

For help in adding it, make a table in Word, then drag its left border.

Upload the resulting docx to the docx4j webapp, or use the helper AddIn, to see the resulting XML, and to generate corresponding docx4j Java code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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