java - HWPF-POI:The table insert into doc with poi hwpf is not visible -
i want insert table @ specific position poi, table generated, find table not visible.
the generated table in doc visible when previewing or editing doc macos , text tool, poi can read table , content, too. plan upload 4 pictures display process, can post 2 images, sorry 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; (int = 0; < range.numsections(); ++i) { section s = range.getsection(i); (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; (int rownum = 0; rownum < table.numrows(); rownum++) { tablerow tablerow = table.getrow(rownum); (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++); } } }
ps:
i sure not microsoft mac 's problem, generate table in doc @ windows platform not visible, too.
(first time ask question, if wrong or expression not clear, please let me know , modify without delay. thanks)
with current state of hwpf project, out of luck when trying insert content .doc file. best bet use different format (docx).
i did not @ hwpf past year, may wrong here regarding current state of hwpf:
some years ago developing custom hwpf library client. major goal custom library ability modify .doc files , word can process modified files correctly. hence know in how many levels modifying .doc file can fail in end. public hwpf library not able handle many aspects of .doc file format when comes modification (textboxes, two-byte character ranges, shape files, nested tables, ... name few).
to handle modification correctly, "features" of specific .doc file must supported library. when there shapes in .doc file, hwpf must adjust position tables of shapes when simple text snippet inserted , shapes not touched. if shapes not handled, word crash when opening output file.
so if can, use docx or rtf. if option, might try 1 of commercial libraries able handle .doc files.
Comments
Post a Comment