




全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
上海傲蓝科技用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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 灌南篝火晚会活动策划方案
- 美团团购的营销推广方案
- 福清建房基础施工方案
- 防洪设施建设土地利用影响分析
- 湖北省烟花爆竹应急预案
- 江门在线税务咨询方案
- 建筑教具制作比赛方案设计
- 分析鸿星尔克营销方案
- 推销食品活动策划方案模板
- 数据治理咨询项目方案
- 2024年公司内部审计制度例文(四篇)
- 江苏省江阴市河塘中学2024-2025学年七年级上学期10月阶级段性练习语文试题
- 小学英语词汇量小学英语词汇大全(约1000词)(按字母顺序)修改版
- 2024年道路交通(驾驶员交通法规及安全)知识考试题库与答案
- 2024年全国职业院校技能大赛中职(数字产品检测与维护赛项)考试题库(含答案)
- 【乡村旅游探究的文献综述4200字】
- 2025年上半年教师资格考试高中历史学科知识与教学能力试卷与参考答案
- 城市供热管网抢修与维护工程技术规程
- DB2104∕T 0011-2022 地理标志产品 清原龙胆
- 《电动汽车双向无线电能传输系统技术规范》
- 医院护理培训课件:《安全注射》
评论
0/150
提交评论