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. redhat安装中文man手册

    1.下载中文man手册 http://download.chinaunix.net/download.php?id=13232&ResourceID=6537 2.上传至服务器并解压 tar ...

  2. nginx(Window下安装 & 配置文件参数说明 & 实例)

    一.为什么需要对Tomcat服务器做负载均衡:  Tomcat服务器作为一个Web服务器,其并发数在300-500之间,如果有超过500的并发数便会出现Tomcat不能响应新的请求的情况,严重影响网站 ...

  3. Laravel开发:多用户登录验证(2)

    上一篇讲了最基本的User验证,现在来讲一下Admin的验证. 先贴代码, 路由:routes/web.php加上以下代码, //... Route::get('admin/login', 'Admi ...

  4. eclipse 创建maven web 项目

    虽然网上教程一大把,但也重新整理下. 一.创建项目 1.Eclipse中用Maven创建项目 上图中Next 2.继续Next 3.选maven-archetype-webapp后,next 4.填写 ...

  5. python 写一个类似于top的监控脚本

    最近老板给提出一个需要,项目需求大致如下:      1.用树莓派作为网关,底层接多个ZigBee传感节点,网关把ZigBee传感节点采集到的信息通过串口接收汇总,并且发送给上层的HTTP Serve ...

  6. 问题:今天测试模块一直出现一个问题?module 'subprocess' has no attribute 'Popen'

    原因:我起的名字用了模块本身的名字,这个地方一定要切记

  7. zoj 3721 Final Exam Arrangement【贪心】

    题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3721 来源:http://acm.hust.edu.cn/vjudg ...

  8. ubuntu下操作端口的方法

    最简单的一个操作:sudo ufw status可检查防火墙的状态,我的返回的是:不活动 sudo ufw version防火墙版本: ufw 0.29-4ubuntu1 Copyright 2008 ...

  9. iOS OC和JS的交互 javaScriptCore方法封装

    一.javaScriptCore javaScriptCore是一种JavaScript引擎,主要为webKit提供脚本处理能力,javaScriptCore是开源webkit的一部分,他提供了强大的 ...

  10. js打开新窗口: window.open

    var iWidth = 800; var iHeight = 600; var iLeft = (window.screen.width - 10 - iWidth) / 2; //获得窗口的水平位 ...