1. 说明

将pdf中的文字读取处理还有一些限制:1. 文档的安全属性不能过于严格 2. 不能存在图片。

2. 直接贴相关的源码

有两种读取方式,maven对应的pom文件

<dependencies>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>1.8.8</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.0.6</version>
</dependency>
</dependencies>

2.1 pdfbox

/**
* PdfboxUtil.java
*/
package com.hsm.pdfTest; import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStream; import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper; /**
* @author hsm
*/
public class PdfboxUtil {
private static String PDFPATH = "D:/Maven权威指南中文版.pdf";
private static String FILEPATH = "D:/Maven权威指南中文版.doc";
public static void main(String[] args) throws Exception {
String content=getPdfContent(PDFPATH);
toFile(content,FILEPATH);
}
/**
* 获取pdf的内容<br/>
* @param pdfPath
* @return
* @throws Exception
*/
private static String getPdfContent(String pdfPath) throws Exception {
boolean sort = false;// 是否排序
int startPage = 1;// 开始提取页数
int endPage = Integer.MAX_VALUE; // 结束提取页数
String content = null;//暂时存放pdf内容 InputStream input = null;
File pdfFile = new File(pdfPath);
PDDocument document = null;
try {
input = new FileInputStream(pdfFile);
// 加载 pdf 文档
PDFParser parser = new PDFParser(input);
parser.parse();
document = parser.getPDDocument();
// 获取内容信息
PDFTextStripper pts = new PDFTextStripper();
pts.setSortByPosition(sort);
endPage = document.getNumberOfPages();
System.out.println("Total Page: " + endPage);
pts.setStartPage(startPage);
pts.setEndPage(endPage);
try {
content = pts.getText(document);
}catch(Exception e) {
throw e;
}
System.out.println("Get PDF Content ...");
}catch(Exception e){
throw e;
} finally {
if (null != input)
input.close();
if (null != document)
document.close();
}
return content;
}
private static void toFile(String content,String filePath) {
try {
File f = new File(filePath);
if (!f.exists()) {
f.createNewFile();
}
System.out.println("Write PDF Content to txt file ...");
BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(content);
output.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

2.2 itext

package com.hsm.pdfTest;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy;
import com.itextpdf.text.pdf.parser.TextExtractionStrategy;
/**
* @author hsm
*/
public class ItextpdfUtil {
private static String PDFPATH = "D:/Maven权威指南中文版.pdf";
private static String FILEPATH = "D:/Maven权威指南中文版.doc";
public static void main(String[] args) {
String content=getPdfContent(PDFPATH);
System.out.println(content); toFile(PDFPATH,FILEPATH);
}
/**
* 获取pdf的内容
* @param pdfPath
* @return
*/
private static String getPdfContent(String pdfPath) {
PdfReader reader = null;
StringBuffer buff = new StringBuffer();
try {
reader = new PdfReader(pdfPath);
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
int num = reader.getNumberOfPages();// 获得页数
TextExtractionStrategy strategy;
for (int i = 1; i <= num; i++) {
strategy = parser.processContent(i,
new SimpleTextExtractionStrategy());
buff.append(strategy.getResultantText());
}
} catch (IOException e) {
e.printStackTrace();
}
return buff.toString();
}
/**
* 将对应的pdf文件读到指定的文件中
* @param pdfPath
* @param filePath
*/
private static void toFile(String pdfPath, String filePath) {
PrintWriter writer = null;
PdfReader reader = null;
try {
writer = new PrintWriter(new FileOutputStream(filePath));
reader = new PdfReader(pdfPath);
int num = reader.getNumberOfPages();// 获得页数
System.out.println("Total Page: " + num);
StringBuffer content = new StringBuffer(""); // 存放读取出的文档内容
for (int i = 1; i <= num; i++) {
// 读取第i页的文档内容
content.append(PdfTextExtractor.getTextFromPage(reader, i));
}
writer.write(content.toString());// 写入文件内容
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

java 获取pdf内容的更多相关文章

  1. Java从URL获取PDF内容

    Java直接URL获取PDF内容 题外话 网上很多Java通过pdf转 HTML,转文本的,可是通过URL直接获取PDF内容,缺没有,浪费时间,本人最近工作中刚好用到,花了时间整理下,分享出来,防止浪 ...

  2. Java 获取PDF数字签名证书信息

    PDF文档中可添加数字签名,在添加签名前,需要准备可信任签名证书.对文档中已有的签名,可验证书签是否有效.也可通过一定方法来获取数字签名或者签名证书信息.下面以Java代码示例展示如何读取签名的证书信 ...

  3. java根据URL获取HTML内容

    之前我写脚本,是想获取HTML内容的. 但是呢...一方面编码困扰着我,于是我写了这个: java根据URL获取网页编码 然后呢,每个网站是不是GZIP还得判断,贼麻烦... 但是没办法啊,麻烦也得写 ...

  4. Java:获取文件内容

    文章来源:https://www.cnblogs.com/hello-tl/p/9139353.html import java.io.*; public class FileBasicOperati ...

  5. 关于java 获取 html select标签 下拉框 option 文本内容 隐藏域

    在HTML中从多选下拉框中提取已选中选项的文本内容到后台,被这个问题难倒了. demo.jsp文件 <select id="selecttype" name"typ ...

  6. java通过URL获取文本内容

    原文地址https://www.cnblogs.com/myadmin/p/7634262.html public static String readFileByUrl(String urlStr) ...

  7. 通过http路径获取文本内容(Java)

    public static String readFileByUrl(String urlStr) { String res = null; try { URL url = new URL(urlSt ...

  8. 【apache tika】apache tika获取文件内容(与FileUtils的对比)

    Tika支持多种功能: 文档类型检测 内容提取 元数据提取 语言检测 重要特点: 统一解析器接口:Tika封装在一个单一的解析器接口的第三方解析器库.由于这个特征,用户逸出从选择合适的解析器库的负担, ...

  9. Java三方---->pdf框架之IText的使用

    在企业的信息系统中,报表处理一直占比较重要的作用t.通过在服务器端使用Jsp或JavaBean生成PDF报表,客户端采用超链接显示或下载得到生成的报表,这样就很好的解决了B/S系统的报表处理问题.今天 ...

随机推荐

  1. JavaScript的join()

    JavaScript join() 方法 JavaScript Array 对象 定义和用法 join() 方法用于把数组中的所有元素放入一个字符串. 元素是通过指定的分隔符进行分隔的. 语法 arr ...

  2. Log4Net.Config配置信息《转》

    看了log4net的简单使用之一_log4net介绍 大家对log4net组件应该有了大概的了解,下面再近一步介绍其在项目中如何应用. 1.Logger 所有的记录器都必须实现 ILog 接口,该接口 ...

  3. 在Mac上为自己手动编译安装一套PHP7的开发环境

    首先你得去官网下载php7 beta1的版本 这里由于我是在mac上安装,所以就去下载linux相关的版本,地址也直接附上了php7 beta1windows版的官方也有发布详情猛戳:这里 解压安装包 ...

  4. java,jquery对json的解析

    json常用于浏览器对服务器的数据传递,所以,我们会经常在浏览器和服务器段对json进行封装和拆装,下面对这些进行简单介绍吧 1,服务器端,也就是java方面,我们用的是 net.sf.json-li ...

  5. linux上安装Kafka

    写个一篇kafka文章了.但是那都是针对性能来说的,下面看一下完整的,kafka步骤: 安装单机三个 Broker 的 Kafka 集群,使用 Kafka 集群发布和接收消息.学完本课程,对 Kafk ...

  6. UVA 699 The Falling Leaves (二叉树水题)

    本文纯属原创.转载请注明出处,谢谢. http://blog.csdn.net/zip_fan. Description Each year, fall in the North Central re ...

  7. 探究css中各种情况下的元素的垂直和水平居中的问题(面试题)

    今天各种纠结,真的是不想写东西(ps 我比较懒)但是老是有人问这个问题,于是我就本着分享精神还是整理一下,好了废话不多说 开始上代码 问题:外边是一个容器,容器中还有一个容器,那么请问怎么让里边的容器 ...

  8. 自定义验证----required属性

    1,required属性 - 表示字段不能为空(注意:只有用户单击“提交”按钮提交表单的时候,浏览器才会执行验证.目前HTML5不支持指定验证的时间,而且验证消息的样式和内容各个浏览器不大一样,不能修 ...

  9. CentOS 7.x samba 服务器安装

    以下以root用户执行 1.安装: # yum install samba samba-client -y   2.设置开机启动: # systemctl enable smb.service ln ...

  10. Python PhatomJS 和Selenium动态加载页面 获取图片内容

    如果您觉得感兴趣的话,可以添加我的微信公众号:一步一步学Python![](http://images2017.cnblogs.com/blog/993869/201711/993869-201711 ...