补充:一下代码基于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的更多相关文章

  1. [转].NET下读取PDF文本

    本文转自:http://blog.csdn.net/wangqiuyun/article/details/8548779 在.NET下读取PDF文本用到的类库主要有两个:PDFBox和iTextSha ...

  2. .Net ->> iTextSharp工具读取PDF文本内容

    分享一个开源的C#DLL,可以读取PDF文本内容. 地址:http://sourceforge.net/projects/itextsharp/ 这里还有相关的链接:http://www.codepr ...

  3. Java 读取PDF中的文本和图片

    本文将介绍通过Java程序来读取PDF文档中的文本和图片的方法.分别调用方法extractText()和extractImages()来读取.   使用工具:Free Spire.PDF for Ja ...

  4. java读取pdf总结

    第三方软件 1.pdfbox PDFBox 0.7.3.PDFBox是一个开源的对pdf文件进行操作的库. PDFBox-0.7.3.jar加入classpath.同时FontBox1.0.jar加入 ...

  5. Java 读取PDF中的表格

    一.概述 本文以Java示例展示读取PDF中的表格的方法.这里导入Spire.PDF for Javah中的jar包,并使用其提供的相关及方法来实现获取表格中的文本内容.下表中整理了本次代码使用到的主 ...

  6. Java 读取Word文本/段落格式属性

    本文介绍通过Java后端程序代码来读取Word文本和段落格式的方法. 本次测试环境如下: Word版本:2013 编译环境:IntelliJ IDEA2018 Work库:free spire.doc ...

  7. java读取pdf和MS Office文档

    有时候PDF中的文字无法复制,这可能是因为PDF文件加密了,不过使用PDFBox开源软件就可以把它读出来. 还有一个用于创建PDF文件的项目----iText. PDFBox下面有两个子项目:Font ...

  8. java读取pdf文档

    import java.io.*;import org.pdfbox.pdmodel.PDDocument;import org.pdfbox.pdfparser.PDFParser;import o ...

  9. Java 读取Word文本框中的文本/图片/表格

    Word可插入文本框,文本框中可嵌入文本.图片.表格等内容.对文档中的已有文本框,也可以读取其中的内容.本文以Java程序代码来展示如何读取文本框,包括读取文本框中的文本.图片以及表格等. [程序环境 ...

随机推荐

  1. Struts1.3——使用MyEclipse集成工具开发Struts

    前面两篇通过手工写代码,手工配置struts-config.xml文件的方法来开发了一个最简单的struts项目,通过手工的方式旨在学习底层原理细节.当熟悉了以后,我们就不需要再通过纯手工的方式来开发 ...

  2. CodeForces - 841B-Godsend-思维

    Leha somehow found an array consisting of n integers. Looking at it, he came up with a task. Two pla ...

  3. Redis Sentinel用法

    1 Redis Sentinel 1.1 哨兵的作用 1. 监控:监控主从是否正常 2. 通知:出现问题时,可以通知相关人员 3. 故障迁移:自动主从切换 4. 统一的配置管理:连接者询问sentin ...

  4. JOGL教程

    本章介绍了OpenGL,Java OpenGL绑定(GL4java,LWJGL,JOGL)和JOGL比其他的OpenGL的优点. Java支持OpenGL(JOGL)是近期在Java OpenGL图形 ...

  5. Error (10170): Verilog HDL syntax error at passwd.v(21) near text "if"; expecting an identifier ("if" is a reserved keyword ), or "endmodule", or a parallel statement

    你得加上时序啊笨蛋

  6. React-Native 指定模拟器RUN-IOS

    react-native run-ios --simulator "iPhone 7”

  7. Web开发进阶

    1.可靠性 可扩展性,服务降级,负载均衡   应用扩展 1.垂直扩展,方式:提升机器硬件,缺点,成本昂贵,扩展能力有限 2.水平扩展,方式:增加节点,优点:升级过程平花,硬件成本低,理论上无线扩展,确 ...

  8. egrep 正则

    首先要记住的是: 正则表达式与通配符不一样,它们表示的含义并不相同!正则表达式只是一种表示法,只要工具支持这种表示法, 那么该工具就可以处理正则表达式的字符串.vim.grep.awk .sed 都支 ...

  9. Nodejs常用代码段

    1、利用createReadStream方式计算大文件的md5签名 const fs = require('fs'); const crypto = require('crypto'); let pa ...

  10. mysql的事务四个特性以及 事务的四个隔离级别

    一.事务四大属性 分别是原子性.一致性.隔离性.持久性. 1,原子性(Atomicity) 原子性是指事务包含的所有操作要么全部成功,要么全部失败回滚,因此事务的操作如果成功就必须要完全应用到数据库, ...