How to merge .doc files using Apache POI API

為{幸葍}努か 提交于 2019-12-25 11:45:44

问题


i'm trying to merge .doc files , i used this code found in previous stackOverflow post :

        public static void main (String[] args) throws Exception {  
        // POI apparently can't create a document from scratch,  
        // so we need an existing empty dummy document  
        HWPFDocument doc = new HWPFDocument(new FileInputStream("fich1.doc"));  
        Range range = doc.getRange();  


        //I can get the entire Document and insert it in the tmp.doc  
        //However any formatting in my word document is lost.  
        HWPFDocument doc2 = new HWPFDocument(new FileInputStream("fich2.doc"));  
        Range range2 = doc2.getRange();  
        range.insertAfter(range2.text());  

        //I can get the information (text only) for each character run/paragraph or section.  
        //Again any formatting in my word document is lost.  
        HWPFDocument doc3 = new HWPFDocument(new FileInputStream("fich3.doc"));  
        Range range3 = doc3.getRange();  
        for(int i=0;i<range3.numCharacterRuns();i++){  
            CharacterRun run3 = range3.getCharacterRun(i);  
            range.insertAfter(run3.text());  
        }  

        OutputStream out = new FileOutputStream("result.doc");  
        doc.write(out);  
        out.flush();  
        out.close();  
}  

The problem that i'm getting this error :

Your document seemed to be mostly unicode, but the section definition was in bytes! Trying anyway, but things may well go wrong! Your document seemed to be mostly unicode, but the section definition was in bytes! Trying anyway, but things may well go wrong! Your document seemed to be mostly unicode, but the section definition was in bytes! Trying anyway, but things may well go wrong!

Here's the content of the different files :

fich1.doc : Hi all

fich2.doc : Bonjour à tous

fich3.doc : Hallo alles

resullt.doc : Hi allB


回答1:


This bug is fixed in poi 3.9 jars : used jars

poi-3.9.jar / poi-ooxml-3.9.jar / poi-ooxml-schemas-3.9.jar / poi-scratchpad-3.9.jar



来源:https://stackoverflow.com/questions/22564664/how-to-merge-doc-files-using-apache-poi-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!