解析docx以及doc格式的word文档中的图片

  • 格式:doc
  • 大小:49.50 KB
  • 文档页数:5

用Java从Doc和Docx格式的Word文档中提取图片Doc和Docx的文档格式因为存储格式不一样,所以需要用不通的方法提取图片。

Docx格式的文档实际上是压缩文件,用程序提取图片相对实际上只需要对文章进行解压缩,然后到指定文件夹找到图片即可。

Doc文档则需要把图片数据读出来,再生成文件。

下面代码均可直接运行。

从Docx文件中提取图片的代码:package com.onlan.docx;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import java.util.Enumeration;import java.util.zip.ZipEntry;import java.util.zip.ZipFile;public class ImgExt4Docx {/*** @param args*/static final int BUFFER = 2048;public static void main(String[] args) {String inputFilename = "D:/work/test.docx";String unZipPathname = "D:/work/unZipDir/";ImgExt4Docx ied=new ImgExt4Docx();System.out.println(ied.unZipDocx(inputFilename,unZipPathname));}/** return the fold of the images*/public String unZipDocx(String docxfile,String destDir)try{String inputFilename = docxfile;String unZipPathname = destDir;ZipFile zipFile = new ZipFile(inputFilename);Enumeration enu = zipFile.entries();int i = 0;while(enu.hasMoreElements()){ZipEntry zipEntry = (ZipEntry)enu.nextElement();if(zipEntry.isDirectory()){newFile(unZipPathname+zipEntry.getName()).mkdirs();continue;}BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(zipEntry));File file = new File(unZipPathname+zipEntry.getName());File parent = file.getParentFile();if(parent != null && !parent.exists()){parent.mkdirs();}FileOutputStream fos = new FileOutputStream(file); BufferedOutputStream bos = new BufferedOutputStream(fos,BUFFER);int count;byte[] array = new byte[BUFFER];while((count = bis.read(array, 0, BUFFER))!=-1){ bos.write(array, 0, BUFFER);}bos.flush();bos.close();bis.close();}return destDir+"word/media";}catch(Exception e){e.printStackTrace();return null;}}从Doc文件中提取图片的代码,该代码不仅可以从文档中提取图片,还可以把Doc文档中图片所在位置加上标签以及图片文件名,与提取的图片对应起来:package com.onlan;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import org.apache.poi.hwpf.HWPFDocument;import org.apache.poi.hwpf.model.PicturesTable;import ermodel.CharacterRun;import ermodel.Picture;import ermodel.Range;public class ImgExt4Doc {public void readWord(String docFile,String imgStoreDir) throws Exception{File f = new File(docFile); / // File f = new File("D:\\work\\test.doc"); //InputStream in =new FileInputStream(f);HWPFDocument msWord=new HWPFDocument(in);String text="";PicturesTable pTable = msWord.getPicturesTable();int numCharacterRuns = msWord.getRange().numCharacterRuns();// ArrayList <PAPX> pList=msWord.getParagraphTable().getParagraphs();// PAPX p=pList.get(0);int underlinecode = 0;int imgcount = 0;OutputStream out = null;int startOffset=0,endOffset;int enterCount=0;for (int i = 0; i < numCharacterRuns; i++) {CharacterRun characterRun =msWord.getRange().getCharacterRun(i);startOffset= characterRun.getStartOffset();characterRun.getUnderlineCode();endOffset= characterRun.getEndOffset();///////////////////for(int m =startOffset;m<endOffset;m++){/////////////Range range = new Range(m,m+1,msWord);int hashCode=range.text().hashCode();// System.out.println(range.text()+"--"+hashCode);CharacterRun cr=range.getCharacterRun(0);underlinecode = cr.getUnderlineCode();if (underlinecode != 0 && range.text() != null) {text = text +"_";}if(m<endOffset&&(hashCode == 13||hashCode==7))//get line{enterCount++;// System.out.println("ENTER FIND........."+enterCount);}text=text + range.text();}///////////////////if(pTable.hasPicture(characterRun)) {//是图片则把图片位置替换为image标签,再把图片输出到指定位置Picture pic = pTable.extractPicture(characterRun, true);String fileName = pic.suggestFullFileName();byte[] content=pic.getContent();out=new FileOutputStream(new File(imgStoreDir + File.separator + fileName));//save pictureout.write(content);out.flush();out.close();text = text + "<image src='" + fileName + "'>";imgcount++;}}in.close();System.out.println(text);}/*** @param args*/public static void main(String[] args) throws Exception{ // TODO Auto-generated method stubImgExt4Doc pt1= new ImgExt4Doc();pt1.readWord("D:\\work\\test.doc","D:\\work\\imgOutput\\");}}。