Summary: | XWPFDocument.insertTable doesn't work at all | ||
---|---|---|---|
Product: | POI | Reporter: | Nigel Gay <nigelgay> |
Component: | XWPF | Assignee: | POI Developers List <dev> |
Status: | NEW --- | ||
Severity: | normal | ||
Priority: | P2 | ||
Version: | 5.2.2-FINAL | ||
Target Milestone: | --- | ||
Hardware: | PC | ||
OS: | All |
Description
Nigel Gay
2022-11-24 13:47:57 UTC
The only workaround I can find is to create an XmlCursor for the insertion point, then call XWPFDocument.insertNewTbl to create a throwaway table, then use XWPFDocument.setTable to replace the throwaway table with the real one. public XmlCursor newCursorForBodyElement (IBodyElement elem) { XmlCursor cursor; if (elem instanceof XWPFParagraph) { XWPFParagraph para = (XWPFParagraph) elem; cursor = para.getCTP ().newCursor (); } else if (elem instanceof XWPFTable) { XWPFTable table = (XWPFTable) elem; cursor = table.getCTTbl ().newCursor (); } else throw new RuntimeException ("Don't know how to create a cursor from body element of type " + elem.getClass ()); return cursor; } .. and then can do ... index = 3; XWPFTable throwawayTable; if (index < doc.getBodyElements ().size ()) { // Insert before numbered element XmlCursor insertionPoint = newCursorForBodyElement (doc.getBodyElements ().get (index)); throwawayTable = doc.insertNewTbl (insertionPoint); } else { // Insert at end throwawayTable = doc.createTable (); } doc.setTable (doc.getTables ().indexOf (throwawayTable), dest); There are some unreleased changes in XWPF code, eg https://bz.apache.org/bugzilla/show_bug.cgi?id=66312 Would you be able to try the latest code? You can build jars from latest source code or you could get jars from https://ci-builds.apache.org/job/POI/job/POI-DSL-1.8/ Tried with latest JARs from Jenkins and find no difference. I think its just not intended to be used the way I am expecting to be able to. |