0

I want to insert a table at a specific position with poi, the table is generated, but I find this table is not visible.

The generated table in doc is visible when previewing or editing this doc with macOS and its text tool, POI can read the table and content, too. I plan to upload 4 pictures to display the process, but I can only post 2 images, sorry for that.

@Test
public void exportDoc() throws Exception {
    FileInputStream readFile = new FileInputStream(new File(readDoc));
    FileOutputStream replaceFile = new FileOutputStream(new File(replaceDoc));
    HWPFDocument document = new HWPFDocument(readFile);
    Table table = WordUtil.insertNewTable(document,"${table}");
    insertTableInDoc(table);
    document.write(replaceFile);
    readFile.close();
    replaceFile.close();
}

private Table insertNewTable(HWPFDocument doc, String sourceValue) {
    Range range = doc.getRange();
    Table table = null;
    for (int i = 0; i < range.numSections(); ++i) {
        Section s = range.getSection(i);
        for (int x = 0; x < s.numParagraphs(); x++) {
            Paragraph p = s.getParagraph(x);
            if (p.text().contains(sourceValue)) {
                //remove target text
                range.replaceText(sourceValue, "");
                table = p.insertTableBefore((short) 3, 3);
                return table;
            }
        }
    }
    return table;
}

private void insertTableInDoc(Table table) {
    int count = 1;
    for (int rowNum = 0; rowNum < table.numRows(); rowNum++) {
        TableRow tableRow = table.getRow(rowNum);
        for (int colNum = 0; colNum < tableRow.numCells(); colNum++) {
            TableCell cell = tableRow.getCell(colNum);
            Paragraph paragraph = cell.getParagraph(0);
            CharacterRun characterRun = paragraph.getCharacterRun(0);        
            characterRun.insertBefore("number: " + count++);
        }
    }
}
  1. the original doc

  2. the doc after table insert

PS:

I am sure this is not microsoft for mac 's problem, the generate table in doc at windows platform is not visible, too.

(First time to ask question, if anything wrong or my expression is not clear, please let me know and I will modify it without delay. Thanks)

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • For your information: I can reproduce your issue. Seems as if the table added with insertTableBefore has a fixed width of 0. Even the Word GUI cannot repair this. But I will not bother with HWPF (binary Word format) myself anymore. This format is outdated since 2007. XWPF (Office OpenXML) is the current format. Since nobody answered, I believe I am not the only one with this opinion. – Axel Richter Aug 11 '16 at 05:15
  • Thanks very much for your concern and replying. I explained to my product manager about differences between doc and docx before, but her insist on supporting doc type. It seems we must change our strategy that only allow user export word with table in docx type. – Blad Blue Shift Aug 11 '16 at 06:26

1 Answers1

2

With the current state of the HWPF project, you likely are out of luck when trying to insert content into a .doc file. Your best bet is to use a different format (docx).

I did not look at HWPF for the past year, so I may be wrong here regarding the current state of HWPF:

Some years ago I was developing a custom HWPF library for a client. The major goal for that custom library was the ability to modify .doc files and that Word can process the modified files correctly. Hence I know in how many levels modifying a .doc file can fail in the end. The public HWPF library is not able to handle many aspects of the .doc file format when it comes to modification (textboxes, two-byte character ranges, shape files, nested tables, ... to name a few).

To handle modification correctly, all the "features" of the specific .doc file must be supported by the library. So when there are shapes in the .doc file, HWPF must adjust the position tables of the shapes even when a simple text snippet is inserted and the shapes are not touched. If shapes are not handled, Word will crash when opening the output file.

So if you can, use docx or rtf. If it is an option, you might try one of the commercial libraries which are able to handle .doc files.

Rainer Schwarze
  • 4,725
  • 1
  • 27
  • 49
  • Thanks very much for your concern and replying. You do understand much internal details about POI and Your suggestion is reasonable. Really appreciate your answer :) – Blad Blue Shift Aug 14 '16 at 04:10