java读取pdf文本转换html
补充:一下代码基于maven,现将依赖的jar包单独导出
地址:pdf jar
完整代码地址 也就两个文件

java读取pdf中的纯文字,这里使用的是pdfbox工具包
maven引入如下配置
<dependency>
<groupId>net.sf.cssbox</groupId>
<artifactId>pdf2dom</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.12</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox-tools</artifactId>
<version>2.0.12</version>
</dependency>
工具类直接读取
代码示例
/*
读取pdf文字
*/
@Test
public void readPdfTextTest() throws IOException {
byte[] bytes = getBytes("D:\\code\\pdf\\HashMap.pdf");
//加载PDF文档
PDDocument document = PDDocument.load(bytes);
readText(document);
} public void readText(PDDocument document) throws IOException {
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(document);
System.out.println(text);
}
将pdf转换为html
效果图

代码示例
/*
pdf转换html
*/
@Test
public void pdfToHtmlTest() {
String outputPath = "D:\\code\\pdf\\HashMap.html";
byte[] bytes = getBytes("D:\\code\\pdf\\HashMap.pdf");
// try() 写在()里面会自动关闭流
try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(outputPath)),"UTF-8"));){
//加载PDF文档
PDDocument document = PDDocument.load(bytes);
PDFDomTree pdfDomTree = new PDFDomTree();
pdfDomTree.writeText(document,out);
} catch (Exception e) {
e.printStackTrace();
}
}
/*
将文件转换为byte数组
*/
private byte[] getBytes(String filePath){
byte[] buffer = null;
try {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer;
}
完整的一个上传pdf转换为HTML功能(今后转换pdf也不需要找什么第三方了,哈哈)
@RequestMapping("ud")
@Controller
public class UpAndDownController {
@RequestMapping("upload.do")
@ResponseBody
public Map<String,Object> upload(@RequestParam("file") MultipartFile file, HttpServletRequest request){
Map<String, Object> map = new HashMap<>();
map.put("code","200");
try {
PdfConvertUtil pdfConvertUtil = new PdfConvertUtil();
String pdfName = file.getOriginalFilename();
int lastIndex = pdfName.lastIndexOf(".pdf");
String fileName = pdfName.substring(0, lastIndex);
String htmlName = fileName + ".html";
String realPath = ResourceUtils.getURL("classpath:").getPath() + "/templates/file";
File f = new File(realPath);
if(!f.exists()){
f.mkdirs();
}
String htmlPath = realPath + "\\" + htmlName;
pdfConvertUtil.pdftohtml(file.getBytes(), htmlPath);
} catch (Exception e) {
map.put("code","500");
e.printStackTrace();
}
return map;
}
}
可以使用postman调试
需要设置请求头 Content-Type 指定为 application/x-www-form-urlencoded

之后选择body选择form-data,OK

如果涉及到HTML页面直接加载PDF,无需插件
可以参考下
https://www.cnblogs.com/jacksoft/p/5302587.html
https://github.com/mozilla/pdf.js
java读取pdf文本转换html的更多相关文章
- [转].NET下读取PDF文本
本文转自:http://blog.csdn.net/wangqiuyun/article/details/8548779 在.NET下读取PDF文本用到的类库主要有两个:PDFBox和iTextSha ...
- .Net ->> iTextSharp工具读取PDF文本内容
分享一个开源的C#DLL,可以读取PDF文本内容. 地址:http://sourceforge.net/projects/itextsharp/ 这里还有相关的链接:http://www.codepr ...
- Java 读取PDF中的文本和图片
本文将介绍通过Java程序来读取PDF文档中的文本和图片的方法.分别调用方法extractText()和extractImages()来读取. 使用工具:Free Spire.PDF for Ja ...
- java读取pdf总结
第三方软件 1.pdfbox PDFBox 0.7.3.PDFBox是一个开源的对pdf文件进行操作的库. PDFBox-0.7.3.jar加入classpath.同时FontBox1.0.jar加入 ...
- Java 读取PDF中的表格
一.概述 本文以Java示例展示读取PDF中的表格的方法.这里导入Spire.PDF for Javah中的jar包,并使用其提供的相关及方法来实现获取表格中的文本内容.下表中整理了本次代码使用到的主 ...
- Java 读取Word文本/段落格式属性
本文介绍通过Java后端程序代码来读取Word文本和段落格式的方法. 本次测试环境如下: Word版本:2013 编译环境:IntelliJ IDEA2018 Work库:free spire.doc ...
- java读取pdf和MS Office文档
有时候PDF中的文字无法复制,这可能是因为PDF文件加密了,不过使用PDFBox开源软件就可以把它读出来. 还有一个用于创建PDF文件的项目----iText. PDFBox下面有两个子项目:Font ...
- java读取pdf文档
import java.io.*;import org.pdfbox.pdmodel.PDDocument;import org.pdfbox.pdfparser.PDFParser;import o ...
- Java 读取Word文本框中的文本/图片/表格
Word可插入文本框,文本框中可嵌入文本.图片.表格等内容.对文档中的已有文本框,也可以读取其中的内容.本文以Java程序代码来展示如何读取文本框,包括读取文本框中的文本.图片以及表格等. [程序环境 ...
随机推荐
- 查看mysql慢日志,进行优化
MySQL 慢查询的相关参数解释:slow_query_log :是否开启慢查询日志,1表示开启,0表示关闭. slow_query_log :是否开启慢查询日志,1表示开启,0表示关闭. lo ...
- 2018 ICPC Asia Singapore Regional A. Largest Triangle (计算几何)
题目链接:Kattis - largesttriangle Description Given \(N\) points on a \(2\)-dimensional space, determine ...
- vbox出现Failed to opencreate the internal network错误,无法启动虚拟机
vbox出现Failed to opencreate the internal network错误,无法启动虚拟机 标签(空格分隔): 未分类 问题 Failed to open/create the ...
- JPA 继承关系实现的三种方式
single table 一张表保存所有类型 join 扩展属性保存在子表中 TABLE_PER_CLASS 每个类型一张表
- (1.1)学习笔记之mysql体系结构(内存、进程、线程)
关键词:mysql体系结构 参考:https://www.cnblogs.com/zhoubaojian/articles/7866292.html 一.mysql体系架构概述 1.mysql体系结构 ...
- Tomcat启动脚本(2)catalina.bat
@echo off rem Licensed to the Apache Software Foundation (ASF) under one or more rem contributor lic ...
- 利用os和pandas来合并当前目录下所有excel文件
#1.引入模块 import os import pandas as pd #2.取出指定目录下的全部excel文件路径 path="C:\\TEST" dirlist=[] fo ...
- Spring入门之生命周期
好几日没有读东西了,今天本来要读mybatis原理部分,但是看到作者讲,只是学会用不用学那么深,遂直接开干spring,工作中一直用springboot,框架都是领导搭好的,每天的任务就是增删改查,挺 ...
- python 数据压缩
zlib 压缩 import zlib import this s = this.s.encode('utf8')*10 for i in range(10): data = zlib.compres ...
- sync - 清空文件系统缓冲区
总览 (SYNOPSIS) sync [OPTION] 描述 (DESCRIPTION) 强迫把更改的块写入磁盘, 并更新超级块. --help 显示帮助然后终止. --version 显示版本信息然 ...