用java从和x格式的word文档中提取图片.doc_第1页
用java从和x格式的word文档中提取图片.doc_第2页
用java从和x格式的word文档中提取图片.doc_第3页
用java从和x格式的word文档中提取图片.doc_第4页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

上海傲蓝科技用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() new File(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 byteBUFFER; 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 org.apache.poi.hwpf.usermodel.CharacterRun;import org.apache.poi.hwpf.usermodel.Picture;import org.apache.poi.hwpf.usermodel.Range;public class ImgExt4Doc public void readWord(String docFile,String imgStoreDir) throws Exception File f = new File(docFile); / / File f = new File(D:worktest.doc); / InputStream in =new FileInputStream(f); HWPFDocument msWord=new HWPFDocument(in); String text=; PicturesTable pTable = msWord.getPicturesTable(); int numCharacterRuns = msWord.getRange().numCharacterRuns(); / ArrayList 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;mendOffset;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(mendOffset&(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 picture out.write(content); out.flush(); out.close(); text = text + ; imgcount+; in.close(); System.out.prin

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论