java读取pdf文本转换html
java读取pdf文本转换html
完整代码地址 也就两个文件
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文字*/@Testpublic 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*/@Testpublic 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")@ResponseBodypublic 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,无需插件
可以参考下
pdf.js实现在HTML下直接浏览pdf文档,无需插件即可实现 - 河畔一角 - 博客园
GitHub - mozilla/pdf.js: PDF Reader in JavaScript
java读取pdf文本转换html相关推荐
- python怎么读取pdf为文本_python怎么读取pdf文本内容
python读取pdf文本内容的方法:首先打开相应的python脚本文件:然后使用PDFMiner工具来读取pdf文本内容:最后通过print输出读取后的内容即可. python读取pdf文本内容 p ...
- [转].NET下读取PDF文本
本文转自:http://blog.csdn.net/wangqiuyun/article/details/8548779 在.NET下读取PDF文本用到的类库主要有两个:PDFBox和iTextSha ...
- Java读取pdf模板,并动态生成pdf文件,如动态生成准考证
Java读取pdf模板,并动态生成pdf文件,如动态生成准考证 前几天遇到了一个生成准考证的需求,并提供用户下载,然后百度了一圈还是觉得使用itextpdf这个框架好用点.但是还需要找到一个能创建 ...
- java 读取文件文本内容_Java读取文本文件
java 读取文件文本内容 There are many ways to read a text file in java. Let's look at java read text file dif ...
- Java 读取PDF中的文本和图片
本文将介绍通过Java程序来读取PDF文档中的文本和图片的方法.分别调用方法extractText()和extractImages()来读取. 使用工具:Free Spire.PDF for Java ...
- 用Java读取pdf中的数据
用Java简单的读取pdf文件中的数据: 第一步:下载PDFBox-0.7.2.jar.提供一个下载地址:[url]http://pdfhome.hope.com.cn/Resource.aspx?C ...
- java读取pdf总结
第三方软件 1.pdfbox PDFBox 0.7.3.PDFBox是一个开源的对pdf文件进行操作的库. PDFBox-0.7.3.jar加入classpath.同时FontBox1.0.jar加入 ...
- Java 读取PDF中表格的工具
目录 1.方法1:Spire.PDF 1.1 Maven仓库下载导入 1.2 读取PDF中的表格 1.2.1 代码 1.2.2 表格内容 1.2.3 读取结果 2.方法2:Tabula 2.1 Mav ...
- java 读取pdf各级标题_Java那些事之pdf读取
今天说说java中的pdf读取,实话说,这是一个很恼人的任务.现在网络上的pdf各种各样,分析出来的文本也是千差万别,目前最流行的jar包pdfbox也只能做到把文字分析出来,并无法很好的控制分析的顺 ...
最新文章
- 华为201万年薪招毕业生!来自这些高校
- IOS笔记-C语言中的指针与数组
- 【 FPGA 】FIR滤波器开篇之传统抽头延迟线FIR滤波器实现介绍
- 无法下载linux系统的驱动精灵,有没有LINUX版的 驱动精灵 破系统搞驱动太费劲,尤其笔记本电脑...
- python运行方式特点_编程必修课:一文弄懂python的运行机制
- Effective Java之多个构造参数考虑用构建器(二)
- 33Exchange Server 2010跨站点部署-分支机构邮件从分支机构出
- python教程博客园_python学习(一)—简明python教程
- php isnumber 小数点,JavaScript常用正则验证函数实例小结【年龄,数字,Email,手机,URL,日期等】...
- SQL将A表的现有数据添加到B表,通过A表现有数据更新B表
- 【DirectX3D - 5】渲染多路视频(单一窗口)
- 系统更新win10服务器出错,Win10系统Windows Update更新出现0x80080005错误代码怎么办...
- 计算机恢复原始桌面图标,Win10桌面图标如何恢复原来排列?
- 打造千万级流量秒杀 过载保护:如何通过熔断和限流解决流量过载问题?
- 怎样把计算机里的W0rd放到电脑桌面,当电脑桌面没有WORD文档时怎么打开WORD文档...
- 7-22 循环日程表
- Linux aarch64 编译 qemu 搭建实验平台
- 计算机怎么演示音乐,做ppt的时候怎样添加音乐 想要在PPT里面增添视频以及音乐怎么操作...
- C++简单程序编写和操作步骤
- excel2016中绘制多条折线的散点图