Java实现word/pdf转html并在线预览
作者:知北游z 发布时间:2022-09-09 09:16:49
标签:Java,文档,在线预览
实现文档在线预览的方式除了上篇文章《文档在线预览(一)通过将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


猜你喜欢
- 本文实例展示了WinForm实现为TextBox设置水印文字功能,非常实用的技巧,分享给大家供大家参考。关键代码如下:using Syste
- 前言项目里有个功能,在应用内切换语言,之前上线了大半年,一直都是正常运行,但是最近这次发版以后,在国外的同事反馈(这里面还包括CEO...
- 本篇分析ArrayList的源码,在分析之前先跟大家谈一谈数组。数组可能是我们最早接触到的数据结构之一,它是在内存中划分出一块连续的地址空间
- 一、输入映射parameterType指定输入参数的Java类型,可以使用别名或者类的全限定名。它可以接收简单类型、POJO、H
- 实践过程效果代码public partial class Form1 : Form{ public Form1()
- 一、概述Overview - LINQ to XML | Microsoft 官方文档LINQ to XMLLINQ to XML 是一种启
- 整理文档,搜刮出一个android 通过MediaRecorder实现简单的录音示例,稍微整理精简一下做下分享。MainActivitypa
- 本文根据java开发人员在编码过程中容易忽视或经常出错的地方进行了整理,总结了十个比较常见的低级错误点,方便大家学习。1、不能用“==”比较
- 目录三大只读类型介绍使用 IReadOnlyList 替换 List使用 IEnumberable 接口集合 表示一组可用于获取和存储的对象
- BottomNavigationView 很早之前就在 Material Design 中出现了,但是直到 Android Support
- 在Servlet 3.0之前都是使用web.xml文件进行配置,需要增加Servlet、Filter或者Listener都需要在web.xm
- 本文实例为大家分享了WPF实现3D翻牌式倒计时的具体代码,供大家参考,具体内容如下实现效果如下:思路:使用自定义控件,设置一个背板 MyCa
- 编写一个 Java 应用程序,实现图形界面多人聊天室(多线程实现),要求聊天室窗口标题是 “欢迎使用 XXX 聊天室应用
- 1.springboot * 处理过滤token,并且返回结果import org.apache.commons.lang3.String
- JetBrains正在开发一种被称为Qodana的代码质量检测工具。它将JetBrains IDE具有的智能代码检查带入了项目CI/CD管道
- 前几天在“Android绘图之渐隐动画”一文中通过画线实现了渐隐动画,但里面有个问题,画笔较粗(大于1)时线段之间会有裂隙,我又改进了一下。
- struct InputStream 是单个输入流的管理器。是由 add_input_stream() 函数申
- 本文实例为大家分享了C#访问共享文件夹或者磁盘的具体代码,供大家参考,具体内容如下SharedTool:using System; &nbs
- 方法一:使用多个controller的共同拥有的父类,即精确到两个controller的上一级@Beanpublic Docket crea
- 一、简介CyclicBarrier 字面意思回环栅栏(循环屏障),它可以实现让一组线程等待至某个状态(屏障点)之后再全部同时执行。叫做回环是