HWPF-POI:The table insert into doc with poi hwpf is not visible

1.1k Views Asked by At

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)

1

There are 1 best solutions below

1
On BEST ANSWER

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.