HWPFDocument读取doc,wps文档(含图片读取)
导包
代码:
1、图片工具类
1 package com.poi.test;2 3 import java.util.ArrayList;4 import java.util.HashMap;5 import java.util.HashSet;6 import java.util.List;7 import java.util.Map;8 import java.util.Set;9 10 import org.apache.poi.hwpf.HWPFDocument; 11 import org.apache.poi.hwpf.model.PicturesTable; 12 import org.apache.poi.hwpf.usermodel.CharacterRun; 13 import org.apache.poi.hwpf.usermodel.Picture; 14 import org.apache.poi.hwpf.usermodel.Range; 15 16 /** 17 * Provides access to the pictures both by offset, iteration over the 18 * un-claimed, and peeking forward 19 */ 20 public class PicturesSource {//这个类是poi官网找的 21 private PicturesTable picturesTable; 22 private Set<Picture> output = new HashSet<Picture>(); 23 private Map<Integer, Picture> lookup; 24 private List<Picture> nonU1based; 25 private List<Picture> all; 26 private int pn = 0; 27 28 public PicturesSource(HWPFDocument doc) { 29 picturesTable = doc.getPicturesTable(); 30 all = picturesTable.getAllPictures(); 31 32 // Build the Offset-Picture lookup map 33 lookup = new HashMap<Integer, Picture>(); 34 for (Picture p : all) { 35 lookup.put(p.getStartOffset(), p); 36 } 37 38 // Work out which Pictures aren't referenced by 39 // a \u0001 in the main text 40 // These are \u0008 escher floating ones, ones 41 // found outside the normal text, and who 42 // knows what else... 43 nonU1based = new ArrayList<Picture>(); 44 nonU1based.addAll(all); 45 Range r = doc.getRange(); 46 for (int i = 0; i < r.numCharacterRuns(); i++) { 47 CharacterRun cr = r.getCharacterRun(i); 48 if (picturesTable.hasPicture(cr)) { 49 Picture p = getFor(cr); 50 int at = nonU1based.indexOf(p); 51 nonU1based.set(at, null); 52 } 53 } 54 } 55 56 private boolean hasPicture(CharacterRun cr) { 57 return picturesTable.hasPicture(cr); 58 } 59 60 private void recordOutput(Picture picture) { 61 output.add(picture); 62 } 63 64 private boolean hasOutput(Picture picture) { 65 return output.contains(picture); 66 } 67 68 private int pictureNumber(Picture picture) { 69 return all.indexOf(picture) + 1; 70 } 71 72 public Picture getFor(CharacterRun cr) { 73 return lookup.get(cr.getPicOffset()); 74 } 75 76 /** 77 * Return the next unclaimed one, used towards the end 78 */ 79 private Picture nextUnclaimed() { 80 Picture p = null; 81 while (pn < nonU1based.size()) { 82 p = nonU1based.get(pn); 83 pn++; 84 if (p != null) 85 return p; 86 } 87 return null; 88 } 89 }
2、处理图片和段落文字
1 package com.poi.test;2 3 import java.io.ByteArrayOutputStream;4 import java.io.File;5 import java.io.FileInputStream;6 7 import org.apache.poi.hwpf.HWPFDocument;8 import org.apache.poi.hwpf.model.PicturesTable;9 import org.apache.poi.hwpf.usermodel.CharacterRun; 10 import org.apache.poi.hwpf.usermodel.Paragraph; 11 import org.apache.poi.hwpf.usermodel.Picture; 12 import org.apache.poi.hwpf.usermodel.Range; 13 14 public class PoiForWord { 15 /** 16 * 使用HWPFDocument解析word文档 17 * wps按doc处理即可 18 */ 19 public void parseDocByHWPFDocument(){ 20 try(FileInputStream is = new FileInputStream(new File("c:\\a.wps"));HWPFDocument document = new HWPFDocument(is);){ 21 ByteArrayOutputStream baos = new ByteArrayOutputStream();//字节流,用来存储图片 22 PicturesSource pictures = new PicturesSource(document); 23 PicturesTable pictureTable = document.getPicturesTable(); 24 25 Range r = document.getRange();//区间 26 for(int i=0;i<r.numParagraphs();i++){ 27 Paragraph p = r.getParagraph(i);//段落 28 int fontSize = p.getCharacterRun(0).getFontSize();//字号,字号和是否加粗可用来当做标题或者某一关键标识的判断boolean isBold = p.getCharacterRun(0).isBold();//是否加粗 29 String paragraphText = p.text();//段落文本 30 31 //以下代码解析图片,这样获取的图片是在文档流中的,是和文本按顺序解析的,可以很好的解决图片定位问题 32 for(int j=0;j<p.numCharacterRuns();j++){ 33 CharacterRun cr = p.getCharacterRun(j);//字符 34 if(pictureTable.hasPicture(cr)){ 35 Picture picture = pictures.getFor(cr); 36 //如果是在页面显示图片,可转换为base64编码的图片 37 picture.writeImageContent(baos);//将图片写入字节流 38 // String base64Image = "<img src='data:image/png;base64,"+new BASE64Encoder().encode(baos.toByteArray())+"'/>"; 39 } 40 } 41 } 42 }catch(Exception e){ 43 e.printStackTrace(); 44 } 45 } 46 47 }
3、处理表格
1 /**2 * 使用HWPFDocument解析word文档3 * wps按doc处理即可4 */5 @Test6 public void parseDocTableByHWPFDocument(){7 try(FileInputStream is = new FileInputStream(new File("d:\\b.doc"));HWPFDocument document = new HWPFDocument(is);){8 Range r = document.getRange();//区间9 for(int i=0;i<r.numParagraphs();i++){ 10 Paragraph p = r.getParagraph(i);//段落 11 String text = p.text(); 12 13 if(text.indexOf("序号")!=-1){//解析表格需要从表格第一个单元格获取表格,另一种表格的方式是直接获取所有表格,但是无法判断表格在文档中的位置 14 Table table = r.getTable(p); 15 16 int numRows = table.numRows();//获取行数 17 18 for(int j=0;j<numRows;j++){ 19 TableRow row = table.getRow(j); 20 int numCells = row.numCells();//当前行列数 21 for(int k=0;k<numCells;k++){ 22 TableCell cell = row.getCell(k); 23 System.out.print(cell.text()+" @ "); 24 } 25 System.out.println(); 26 } 27 } 28 } 29 }catch(Exception e){ 30 e.printStackTrace(); 31 } 32 }
字符"?"可通过字符串替换或截取来解决
另一种解析的方式,只支持解析文本内容,且无法获取字号和加粗等字体格式
1 WordExtractor extor = new WordExtractor(is); 2 String[] paragraphText = extor.getParagraphText();
HWPFDocument读取doc,wps文档(含图片读取)相关推荐
- Java web/springboot上传word/doc/docx文档(含图片)与HTML富文本导入/导出互相转换解析!附项目源码
测试效果 先看下效果 文档内容如下: 上传 上传docx文档 查看解析内容 <html><head><style>p{margin-top:0pt;margin-b ...
- wps文档插入图片图片不完全显示问题以及如何关闭自启动
wps文档插入图片图片不完全显示问题 解决图片不完全显示:单击图片旁边的布局选项,将布局选择为嵌入型,再ctrl+1即可 wps关闭自启动 有时wps会自启动占用系统内存造成电脑卡顿,像这种流氓软件就 ...
- wps图片与图片间距怎么调整_怎么样在WPS文档中将图片的中间间距拉?
2011-05-31 如何删除没有回车符的空行?如何删 选中WORD整个文档,进行"格式/段落/缩进和间距/间距/段前/0行/段后/0行/确定"操作,即可将段前和段后没有回车符的空 ...
- 使用python-docx读取doc,docx文档
API: http://python-docx.readthedocs.io/en/latest/#api-documentation 将doc转为docx: from win32com imp ...
- wps文档复制粘贴序号_WPS居然也搞人工智能?学会这几个AI功能之后我的工作效率提高了一半!...
悄咪咪地问一句,有没有同学在用WPS办公时做过这种事情: ●把图片里的英文手动输入到翻译软件,翻译完再将内容复制到WPS文档●把图片里的文字全部手动输入进WPS文档,累得半死●想抠图只能用P图软件抠好 ...
- java 使用Spire.Doc实现Word文档插入图片
目录 使用步骤 1.引入依赖 2.关键代码 Spire.Doc for Java 是一款专业的 Java Word 组件,开发人员使用它可以轻松地将 Word 文档创建.读取.编辑.转换和打印等功能集 ...
- WPS文档怎样转换为图片
文件转换操作不论是在日常生活中还是工作学习当中都会遇见.因此文件转换已经成为了基础操作,是大家都应该学习掌握的操作.为此今天小编将教给大家其中的一个转换技巧,那就是WPS转图片. 使用工具:迅捷PDF ...
- java doc转图片_使用Java实现word文档转图片 在线预览
[Java] 纯文本查看 复制代码/** * licence 验证 * @return * @throws Exception */ public static boolean getLicense( ...
- Java使用Spire.Doc实现Word文档添加图片水印
通过本文你将学到: Spire.Doc是什么? 如何在项目中引入Spire.Doc依赖? 项目中基于Spire.Doc封装工具类实现Word文档添加图片水印? 一.Spire.Doc是什么? 1.Sp ...
最新文章
- flutter报错Could not connect to lockdownd, error code -
- 2021-08-09 图像灰度二值化
- Android系统如何管理自己内存的?
- 【压缩率3000%】上交大ICCV:精度保证下的新型深度网络压缩框架
- Java 泛型总结(三):通配符的使用
- Sql:成功解决将sql输出的datetime时间格式转为常规格式
- Redis 管道(Pipelining)
- 《一》php多进程编程:第一次fork
- Orchard架构介绍
- [转] 比特币从“不了解”到“被误解”——详解区块链技术
- flask登录验证用ajax,基于 Ajax 请求的 Flask-Login 认证
- 【转】运用jieba库分词
- 在html用微信跳转,H5如何跳转微信小程序?
- CAPM模型的应用--回归模型中的Alpha, r_f
- C#进行CAD二次开发学习笔记-01
- 腾讯云Intel Xeon Cascade Lake 8255C(2.5 GHz)处理器性能评测
- [系统安全] 三十五.Procmon工具基本用法及文件进程、注册表查看
- 工业级静默活体检测开源算法技术解析记录
- Redis(2)数据结构
- 交易日九点到九点半的挂单撤单以及价格的一点心得
热门文章
- 计算机网络对我们思维的影响,计算机网络教学中学生计算思维的培养
- html5 websocket 手机,HTML5 WebSocket 示范
- 感觉自己做这个题的思路很不错 1225 Digit Counting
- x的奇幻之旅 (史蒂夫·斯托加茨 著)
- 无法访问_关于小米电视无法访问电脑创建共享文件夹问题
- multipart/form-data;boundary=----WebKitFormBoundaryRAYPKeHKTYSNdzc1;charset=UTF-8‘ not supporte
- PSP(Python Server Pages) 快速实例
- JSP 文件上传下载系列之一[基本方式上传文件]
- Java中的SOAP技术
- Spring Boot Mybatis入门示例