读取docx(带图片)
来源:互联网 发布:培训seo 编辑:程序博客网 时间:2024/04/27 13:25
import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.List;import java.util.Map;import java.util.UUID;import org.apache.poi.xwpf.usermodel.XWPFDocument;import org.apache.poi.xwpf.usermodel.XWPFParagraph;import org.apache.poi.xwpf.usermodel.XWPFPictureData;import org.apache.poi.xwpf.usermodel.XWPFRun;import com.google.common.collect.Maps;public class Test {public static void main(String[] args){String importPath = "D:/zyk_import/习题模板V1.4(2).docx";String absolutePath = "";try {FileInputStream inputStream = new FileInputStream(importPath);XWPFDocument xDocument = new XWPFDocument(inputStream);List<XWPFParagraph> paragraphs = xDocument.getParagraphs();List<XWPFPictureData> pictures = xDocument.getAllPictures();Map<String, String> map = Maps.newHashMap();for(XWPFPictureData picture : pictures){String id = picture.getPackageRelationship().getId();File folder = new File(absolutePath);if (!folder.exists()) {folder.mkdirs();}String rawName = picture.getFileName();String fileExt = rawName.substring(rawName.lastIndexOf("."));String newName = System.currentTimeMillis() + UUID.randomUUID().toString() + fileExt;File saveFile = new File(absolutePath + File.separator + newName);@SuppressWarnings("resource")FileOutputStream fos = new FileOutputStream(saveFile); fos.write(picture.getData());System.out.println(saveFile.getAbsolutePath()); map.put(id, saveFile.getAbsolutePath());}String text = "";for(XWPFParagraph paragraph : paragraphs){//System.out.println(paragraph.getParagraphText());List<XWPFRun> runs = paragraph.getRuns();for(XWPFRun run : runs){if(run.getCTR().xmlText().indexOf("<w:pict>")!=-1){String runXmlText = run.getCTR().xmlText();int rIdIndex = runXmlText.indexOf("r:id");int rIdEndIndex = runXmlText.indexOf("/>", rIdIndex);String rIdText = runXmlText.substring(rIdIndex, rIdEndIndex);System.out.println(rIdText.split("\"")[1].substring("rId".length()));String id = rIdText.split("\"")[1];System.out.println(map.get(id));text = text +"<img src = '"+map.get(id)+"'/>";}else{text = text + run;}}}System.out.println(text);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
0 2
- 读取docx(带图片)
- 关于poi读取word docx格式文本和图片功能
- 用JavaScript(JSON)生成pdf、docx、png、svg、flash等,带中文、图形、图片、超链接...
- paip.android 读取docx总结
- PHP读取docx文档内容
- c# 读取docx文件示例代码
- Java用POI读取docx文档
- POI读取.doc 和.docx的区别
- android 读取doc docx xsl xslx
- Python读取Word(.docx)正文信息
- java读取txt、doc、docx文件
- JAVA读取Doc、Docx及注意点
- Word-docx文件图片信息格式分析
- 提取docx中图片,文字,表格元素
- 带公式的markdown转成docx和pdf
- docx格式之指定位置插入图片(图片复制)
- 读取excel文件,支持doc ,docx格式 附带上传方法
- 编程读取文档Doc,Docx,Pdf的内容
- ubnutu 一般安装的路径
- 蓝桥杯 结果填空 猜生日
- POJ 3126 Prime Path (BFS)
- HTML基本框架
- JVM
- 读取docx(带图片)
- mysql乱码问题解决过程
- UED工作流程
- 测试大牛们的博客
- Java_语法基础_优先使用整型池
- Android学习之路之浅谈Activity(窗口)
- HDU 3974 Assign the task (线段树+dfs序)
- p51,16
- Underscore.string.js Version (2.0.0) 中文文档