Java实现word/pdf转html并在线预览
作者:知北游z 时间:2022-09-09 09:16:49
实现文档在线预览的方式除了上篇文章《文档在线预览(一)通过将txt、word、pdf转成图片实现在线预览功能》说的将文档转成图片的实现方式外,还有转成pdf,前端通过pdf.js、pdfobject.js等插件来实现在线预览,以及本文将要说到的将文档转成html的方式来实现在线预览。代码基于 aspose-words(用于word转html),pdfbox(用于pdf转html),所以事先需要在项目里下面两个依赖:
<dependency>
<groupId>com.luhuiguo</groupId>
<artifactId>aspose-words</artifactId>
<version>23.1</version></dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.4</version>
</dependency>
一、将文件转换成html字符串
1、将word文件转成html字符串
public static String wordToHtmlStr(String wordPath) {
try {
Document doc = new Document(wordPath); // Address是将要被转化的word文档
String htmlStr = doc.toString();
return htmlStr;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
验证结果:
2、将pdf文件转成html字符串
public static String pdfToHtmlStr(String pdfPath) throws IOException, ParserConfigurationException {
PDDocument document = PDDocument.load(new File(pdfPath));
Writer writer = new StringWriter();
new PDFDomTree().writeText(document, writer);
writer.close();
document.close();
return writer.toString();
}
验证结果:
二、将文件转换成html,并生成html文件
有时我们是需要的不仅仅返回html字符串,而是需要生成一个html文件这时应该怎么做呢?一个改动量小的做法就是使用org.apache.commons.io包下的FileUtils工具类写入目标地址:
1、FileUtils类将html字符串生成html文件示例
首先需要引入pom:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
相关代码:
String htmlStr = FileConvertUtil.pdfToHtmlStr("D:\\书籍\\电子书\\小说\\历史小说\\最后的可汗.doc");
FileUtils.write(new File("D:\\test\\doc.html"), htmlStr, "utf-8");
除此之外,还可以对上面的代码进行一些调整,已实现生成html文件,代码调整如下:
2、将word文件转换成html文件
public static void wordToHtml(String wordPath, String htmlPath) {
try {
File sourceFile = new File(wordPath);
String path = htmlPath + File.separator + sourceFile.getName().substring(0, sourceFile.getName().lastIndexOf(".")) + ".html";
File file = new File(path); // 新建一个空白pdf文档
FileOutputStream os = new FileOutputStream(file);
Document doc = new Document(wordPath); // Address是将要被转化的word文档
HtmlSaveOptions options = new HtmlSaveOptions();
options.setExportImagesAsBase64(true);
options.setExportRelativeFontSize(true);
doc.save(os, options);
} catch (Exception e) {
e.printStackTrace();
}
}
验证结果:
3、将pdf文件转换成html文件
public static void pdfToHtml(String pdfPath, String htmlPath) throws IOException, ParserConfigurationException {
File file = new File(pdfPath);
String path = htmlPath + File.separator + file.getName().substring(0, file.getName().lastIndexOf(".")) + ".html";
PDDocument document = PDDocument.load(new File(pdfPath));
Writer writer = new PrintWriter(path, "UTF-8");
new PDFDomTree().writeText(document, writer);
writer.close();
document.close();
}
图片版PDF文件验证结果:
文字版PDF文件验证结果:
来源:https://www.cnblogs.com/fhey/p/17442536.html
标签:Java,文档,在线预览
0
投稿
猜你喜欢
JPA Specification常用查询+排序实例
2023-11-23 04:56:32
Flutter路由传递参数及解析实现
2023-06-22 11:48:45
Maven学习----Maven安装与环境变量配置教程
2021-12-04 08:20:25
使用IDEA开发配置Java Web的初始化过程
2022-09-25 16:33:38
Spring Cache简单介绍和使用大全
2023-11-25 04:46:46
解析Spring Mvc Long类型精度丢失问题
2021-11-06 12:27:37
基于Mybatis plus 自动代码生成器的实现代码
2023-11-24 10:40:51
java客户端登陆服务器用户名验证
2023-11-09 07:03:09
Java8 Comparator源码演示及解析
2023-09-18 10:51:12
java设计模式之外观模式学习笔记
2022-07-02 18:48:06
IntelliJ IDEA本地代码覆盖后恢复原来的代码图解
2022-03-09 22:42:19
Flutter本地存储之基本的键值对存储详解
2023-08-18 03:52:35
Java 数组高频考点分析讲解
2021-09-01 13:14:36
关于统计数字问题的算法
2023-11-03 00:33:32
关于java 图形验证码的解决方法
2023-08-09 15:21:19
浅析java修饰符访问权限(动力节点Java学院整理)
2023-07-13 18:07:34
解决SpringBoot中MultipartResolver和ServletFileUpload的冲突问题
2023-10-22 15:28:19
java中关于深拷贝的几种方式总结
2023-12-13 17:39:41
SpringBoot核心@SpringBootApplication使用介绍
2023-11-25 08:33:59
Flutter 队列任务的实现
2023-07-07 17:25:14