java - Apache Poi: Text is added to .doc file before it's content but not after it -
i need add simple text (from .txt or .doc file) .doc file code simple :
public static void main(string[] args) throws ioexception { poifsfilesystem fs = new poifsfilesystem(new fileinputstream("/home/amira/work/apps-579/word/test1.doc")); hwpfdocument doc = new hwpfdocument(fs); range range = doc.getrange(); characterrun run = range.insertafter("hello world!!! works well!!!"); run.setbold(true); run.setitalic(true); run.setcapitalized(true); outputstream out = new fileoutputstream("/home/amira/work/apps-579/word/sampleafter.doc"); doc.write(out); out.flush(); out.close(); }
the new sampleafter.doc created contains content of test1.doc : "hello world!!! works well!!!" string has not been added .
i tried use
range.insertbefore(string text)
method works , string added before content of test1.doc.
i don't it. there explanation issue.
here's content of test1.doc :
voilà mon premier test le 24/03/24
here's result of : system.out.println(range.text());
with insertbefore :
- hello world!!! works well!!!voilà mon premier test le 24/03/24
with insertafter :
voilà mon premier test le 24/03/24 hello world!!! works well!!!
well, above code seems work me. guess better follow following approach
range r1 = doc.getrange(); section sec = r1.getsection(r1.numsections()-1); paragraph para = sec.getparagraph(sec.numparagraphs()-1); characterrun run = para.getcharacterrun(para.numcharacterruns()-1); run.insertafter("hello world!!! works well!!!"); run.setbold(true); run.setitalic(true); run.setcapitalized(true);
Comments
Post a Comment