Summary: | createRow() leads to corrupted word file | ||
---|---|---|---|
Product: | POI | Reporter: | Thomas Hoffmann <Thomas.Hoffmann> |
Component: | XWPF | Assignee: | POI Developers List <dev> |
Status: | RESOLVED FIXED | ||
Severity: | normal | CC: | Thomas.Hoffmann |
Priority: | P2 | ||
Version: | 5.0.0-FINAL | ||
Target Milestone: | --- | ||
Hardware: | PC | ||
OS: | All | ||
Attachments: | Test word file |
Digging into the sourcecode of POI, I found that the empty cells are causing the problems. class: XWPFTable private void addColumn(XWPFTableRow tabRow, int sizeCol) { if (sizeCol > 0) { for (int i = 0; i < sizeCol; i++) { tabRow.createCell(); } } } If i change the loop and additionally fill the cell with a paragraph, MS word won't complain about a corrupted file any more. So, instead of "tabRow.createCell()" I do: XWPFTableCell c = tabRow.createCell(); c.addParagraph(); Error is gone after this change in the POI sources. Maybe you can check, whether empty cells violate the specification. It does report an error when opened in ms word, but it seems to be correct when opened in libreoffice. Please check bug #64838(https://bz.apache.org/bugzilla/show_bug.cgi?id=64838). The solution may be: 1. not add a paragraph when loading an existing empty table cell, and add a paragraph by default when creating a cell??? 2. not add a paragraph: in order to open without error, users add paragraph by themselves 3. add a paragraph by default: this will change the structure of the empty cell in the existing document Hello, thanks for the quick response. I think option no 1) sounds reasonable to me. POI shouldn't change a file or the content / structure when opening. But when I add a new row, it should be initialized "correctly". MS word shows the file correctly but the error message will worry the user and needs an extra approval when opening. It didn't happen with POI 4.x so probably something changed in POI 5.0.0. Another option would be to pass a parameter whether to initialize the cells when creating a new row --> tabRow.createCell(true) (option no 2) would cause many errors if the programmer needs to take care of "post-processing" the newly added row which is not obvious at first) Regards, Thomas |
Created attachment 37849 [details] Test word file Hello, we have a docx file with a table in it. If we add a new row via createRow() and save the document, it gets corrupted. MS Word says "Word found unreadable cotent in ... Do you want to recover the contents of this document? If you trust the source of this document, click yes" The code to reproduce this is quite small: public static void main(String[] args) throws Exception { File templateFile = new File("c\\test.docx"); XWPFDocument xwpfDocument = null; try (InputStream input = new FileInputStream(templateFile)) { xwpfDocument = new XWPFDocument(input); } XWPFTable table = xwpfDocument.getTableArray(0); table.createRow(); try (FileOutputStream output = new FileOutputStream("c:\\test_out.docx")) { xwpfDocument.write(output); } xwpfDocument.close(); } Without the line table.createRow() everything works well. Adding a row to the table renders the file corrupt when saving. Could you check and verify the problem with the current version 5.0.0 ? Thanks in advance!