《用Java从Doc和Docx格式的Word文档中提取图片.docx》由会员分享,可在线阅读,更多相关《用Java从Doc和Docx格式的Word文档中提取图片.docx(9页珍藏版)》请在第一文库网上搜索。
1、用JaVa从DOC和DOCX格式的Word文档中提取图片DOC和DOCX的文档格式因为存储格式不一样,所以需要用不通的方法提取图片。DOCX格式的文档实际上是压缩文件,用程序提取图片相对实际上只需要对文章进行解压缩,然后到指定文件夹找到图片即可。DOe文档则需要把图片数据读出来,再生成文件。下面代码均可直接运行。从DoCX文件中提取图片的代码:packagecom.on1an.docx;importjavaio,BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.Fi1e;importjava.io.Fi1e
2、OutputStream;importjava.uti1.Enumeration;importjava.uti1.zip.ZipEntry;importjava.uti1.zip.ZipFi1e;pub1icc1assImgExt4Docx* paramargs*/staticfina1intBUFFER=2048;pub1icstaticvoidmain(Stringargs)StringinputFi1ename=”D:/WOrk/test.docx”;StringUnZipPathname=,DworkunZipDir;ImgExt4Docxied=newImgExt4Docx();Sy
3、stem.out.printIn(ied.unZipDocx(inputFi1enamejUnZipPathname);)* returnthefo1doftheimages* /pub1icStringunZipDocx(Stringdocxfi1e,StringdestDir)tryStringinputFi1ename=docxfi1e;StringUnZipPathname=destDir;ZipFi1eZiPFi1e=newZipFi1e(inputFi1ename);EnUrneratiOnenu=zipFi1e.entries();inti=0;whi1e(enu.hasMore
4、E1ements()ZipEntryZipEntry=(ZipEntry)enu.nextE1ement();if(zipEntry.isDirectory()newFi1e(unZipPathname+zipEntry.getName().mkdirs();continue;)BufferedInputStreambis=newBufferedInputStream(zipFi1e.getInputStream(zipEntry);Fi1efi1e=newFi1e(unZipPathname+zipEntry.getName();Fi1eparent=fi1e.getParentFi1e()
5、;if(parent!=nu11&!parent.exists()parent.mkdirs();Fi1eOutputStreamfos=newFi1eOutputStream(fi1e);BufferedOutputStreambos=newBufferedOutputStream(fosjtFFE?);intcount;bytearray=newbyteBUFFER;whi1e(count=bis.read(arrayj0jBUFFER)!=-1)bos.write(array,0,BUFFER);bos.f1ush();bos.c1ose();bis.c1ose();)returndes
6、tDir+wordmedia;catch(Exceptione)eprintStackTrace();returnnu11;)从Doc文件中提取图片的代码,该代码不仅可以从文档中提取图片,还可以把DOC文档中图片所在位置加上标签以及图片文件名,与提取的图片对应起来:packagecom.on1an;importjava.io.Fi1e;importjava.io.Fi1eInputStream;importjava.io.Fi1eOutputStream;importjava.io.Inputstream;importjava.io.OutputStream;importorg.apache.
7、poi.hwpf.HWPFDocument;importorg.apache.poi.hwpf.mode1.PicturesTab1e;importorgapache.poi.hwpf.usermode1.CharacterRun;importorg.apache.poi.hwpf.usermode1.Picture;importorg.apache.poi.hwpf.usermode1.Range;pub1icc1assImgExt4Docpub1icvoidreadWord(StringdocFi1ejStringimgStoreDir)throwsException(Fi1ef=newF
8、i1e(docFi1e);/Fi1ef=newFi1e(D:worktest.doc);/InputStreamin=newFi1eInputStream(f);HWPFDocumentmsWord=newHWPFDocument(in);Stringtext=”;PicturesTab1epTab1e=msord.getPicturesTab1e();intnumCharacterRuns=msord.getRange().numCharacterRuns();/Array1istp1ist=msord.getParagraphTab1e().getParagraphs();/PAPXp=p
9、1ist.get(0);intunder1inecode=0;intim-count=0;OutputStreamout=nu11;intStartOffset=0jendoffset;intenterCount=0;for(inti=0;inumCharacterRuns;i+)(CharacterRunCharacterRun=msWord.getRange().getCharacterRun(i);startoffset=CharacterRun.getStartOffset();characterRun.get11nder1ineCode();endffset=CharaCterRUn
10、.getEndOffset();Iiiiiiiiiiiiiiiiiiifor(intm=staPtOffset;mendOffset;m+)IiiiiiiiiiiiiRangerange=newRange(m,m+1,msWord);inthashCode=range.text().hashCode();/System.out.printIn(range,text()+,-+hashCode);CharacterRuncr=range.getCharacterRun(0);under1inecode=cr.getUnder1ineCode();if(under1inecode!=0&range
11、.text()!=nu11)text=text)if(mendOffset&(hashCode=131IhashCode=7)get1ine(enterCount+;System.out.printIn(ENTERFIND,1+enterCount);)text=text+range.text();)Iiiiiiiiiiiiiiiiiiiif(pTab1e.hasPicture(CharacterRun)是图片则把图片位置替换为image标签,再把图片输出到指定位置Picturepic=pTab1e.extractPicture(characterRuntrue);Stringfi1eName
12、=picSuggestFu11Fi1eNameO;bytecontent=pic.getContent();out=newFi1eOutputStream(newFi1e(imgStoreDir+Fi1e.separator+fi1eName);/savepictureout.write(content);out.f1ush();out.c1ose();text=text+,imagesrc=,十fi1eName+imgcount+;)in.c1ose();System.out.printIn(text);)*paramargs*/pub1icstaticvoidmain(Stringargs)throwsException/TODOAuto-generatedmethodstubImgExt4Docpt1=newImgExt4Doc();pt1.readWord(,D:worktest.doc,jD:workimgutput);)