//读取pdf 全部内容
public static String topdffile(String pdffile){
StringBuffer result = new StringBuffer();
String str=null;
FileInputStream is = null;
PDDocument document = null;
try {
is = new FileInputStream(pdffile);
PDFParser parser = new PDFParser(is);
parser.parse();
document = parser.getPDDocument();
PDFTextStripper stripper = new PDFTextStripper();
str=stripper.getText(document);
// System.out.println("------"+str+"--------------");
} catch (FileNotFoundException e) { e.printStackTrace();
} catch (IOException e) { e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) { e.printStackTrace();
}
}
if (document != null) {
try {
document.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return str;
-----------------------------------------------------------
//分页查询保存到map里
// 是否排序
boolean sort = false;
// pdf文件名
String pdfFile = file;
// 输入文本文件名称
String textFile = null;
// 编码方式
String encoding = "UTF-8";
// 开始提取页数
int startPage = 1;
// 结束提取页数
int endPage =5;
// 文件输入流,生成文本文件
Writer output = null;
// 内存中存储的PDF Document
PDDocument document = null;
Map mpdf=new HashMap();
try {
try {
// 首先当作一个URL来装载文件,如果得到异常再从本地文件系统//去装载文件
URL url = new URL(pdfFile);
//注意参数已不是以前版本中的URL.而是File。
document = PDDocument.load(pdfFile);
// 获取PDF的文件名
String fileName = url.getFile();
// 以原来PDF的名称来命名新产生的txt文件
if (fileName.length() > 4) {
File outputFile = new File(fileName.substring(0, fileName
.length() - 4)
+ ".txt");
textFile = outputFile.getName();
}
} catch (MalformedURLException e) {
// 如果作为URL装载得到异常则从文件系统装载
//注意参数已不是以前版本中的URL.而是File。
document = PDDocument.load(pdfFile);
if (pdfFile.length() > 4) {
textFile = pdfFile.substring(0, pdfFile.length() - 4)
+ ".txt";
}
}
// 文件输入流,写入文件倒textFile
output = new OutputStreamWriter(new FileOutputStream(textFile),
encoding);
// PDFTextStripper来提取文本
PDFTextStripper stripper = null;
stripper = new PDFTextStripper();
// 设置是否排序
stripper.setSortByPosition(sort);
// // 设置起始页
// stripper.setStartPage(startPage);
// // 设置结束页
// stripper.setEndPage(endPage); for(int i=startPage;i<=document.getNumberOfPages();i++){
stripper = new PDFTextStripper();
stripper.setSortByPosition(sort);
// 设置起始页
stripper.setStartPage(i);
// 设置结束页
stripper.setEndPage(i);
String textT=stripper.getText(document);
// System.out.println("第" + i+"页");
// System.out.println( "开始--------------------------------------------------------------------");
// System.out.println( textT);
// System.out.println( "--------------------------------------------------------------------结束");
mpdf.put(i,textT);
}
// System.out.println(mpdf.size());
// 调用PDFTextStripper的writeText提取并输出文本
// stripper.writeText(document, output);
// System.out.println(stripper.getEndPage());
// System.out.println( "*****="+stripper.getText(document));
// System.out.println("*****22=" + stripper.getTextLineMatrix());
// System.out.println("*****33=" + stripper.getTextMatrix());
// System.out.println("*****44=" + stripper.getArticleStart());
// System.out.println("*****55=" + stripper.getArticleEnd()); } finally {
if (output != null) {
// 关闭输出流
output.close();
}
if (document != null) {
// 关闭PDF Document
document.close();
}
}
return mpdf; 返回map

读取pdf内容分页和全部的更多相关文章

  1. Python读取PDF内容

    1,引言 晚上翻看<Python网络数据采集>这本书,看到读取PDF内容的代码,想起来前几天集搜客刚刚发布了一个抓取网页pdf内容的抓取规则,这个规则能够把pdf内容当成html来做网页抓 ...

  2. 转:C# PDFbox读取PDF内容

    using System; using System.Windows.Forms; using org.apache.pdfbox.pdmodel; using org.apache.pdfbox.u ...

  3. 转:C#读取PDF、TXT内容

    //读取PDF内容 private void button2_Click(object sender, EventArgs e) { label3.Text = OnCreated("D:\ ...

  4. lucent检索技术之创建索引:使用POI读取txt/word/excel/ppt/pdf内容

    在使用lucent检索文档时,必须先为各文档创建索引.索引的创建即读出文档信息(如文档名称.上传时间.文档内容等),然后再经过分词建索引写入到索引文件里.这里主要是总结下读取各类文档内容这一步. 一. ...

  5. 深入学习python解析并读取PDF文件内容的方法

    这篇文章主要学习了python解析并读取PDF文件内容的方法,包括对学习库的应用,python2.7和python3.6中python解析PDF文件内容库的更新,包括对pdfminer库的详细解释和应 ...

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

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

  7. 【转】Python读取PDF文档,输出内容

    Python3读取pdf文档,输出内容(txt) from urllib.request import urlopen from pdfminer.pdfinterp import PDFResour ...

  8. SQL 横转竖 、竖专横 (转载) 使用Dapper.Contrib 开发.net core程序,兼容多种数据库 C# 读取PDF多级书签 Json.net日期格式化设置 ASPNET 下载共享文件 ASPNET 文件批量下载 递归,循环,尾递归 利用IDisposable接口构建包含非托管资源对象 《.NET 进阶指南》读书笔记2------定义不可改变类型

    SQL 横转竖 .竖专横 (转载)   普通行列转换 问题:假设有张学生成绩表(tb)如下: 姓名 课程 分数 张三 语文 74 张三 数学 83 张三 物理 93 李四 语文 74 李四 数学 84 ...

  9. Python | 实现pdf文件分页

    不知道大家有没有遇到过这么一种情况,就比如一个pdf格式的电子书,我们经常浏览的是其中的一部分,而这电子书的页数很大,每当需要浏览时,就需要翻到对应的页码,就有点儿繁琐. 还有一些情况,比如,我们想分 ...

随机推荐

  1. poj1011 && uva307 DFS + 剪枝

    将木棒从大到小排列,保证每次的选择都是最长可选的木棒. 剪枝: 1 . 如果第 i 根木棒被选择却无法成功拼接,那么后面与其长度相同的也不能选择. 2 . 如果第 cnt + 1 根木棒无法成功拼接, ...

  2. session不会过期

    $(function () { window.setInterval(function () { $.post('random.html'); }, 60000); }); 加在母版页里,使用与长时间 ...

  3. 笔记︱范数正则化L0、L1、L2-岭回归&Lasso回归(稀疏与特征工程)

    机器学习中的范数规则化之(一)L0.L1与L2范数 博客的学习笔记,对一些要点进行摘录.规则化也有其他名称,比如统计学术中比较多的叫做增加惩罚项:还有现在比较多的正则化. -------------- ...

  4. thinkphp中使用phpexcel读取所有的行记录

    ThinkPHP3.2使用phpexcle 将phpexcel放到Think\Org\PHPExcel中 public function index() { import('Org.PHPExcel. ...

  5. SVN同步出现问题

    1.错误描述    同步SVNStatusSubscribe时报告了错误,1中的0个资源已经同步    同步/frame时发生错误:Error getting status for resource ...

  6. 解析FAT16文件系统

    引导扇区的信息如下: 1.  偏移地址00H,长度3,内容:EB 3C 90 跳转指令. 2.  偏移地址03H,长度8,内容:4D 53 44 4F 53 35 2E 30 为厂商标志和os 版本号 ...

  7. ORA-00904:"T1"."AREA_ID" :标识符无效

    1.错误描述 ORA-00904:"T1"."AREA_ID" :标识符无效 00904 . 00000 - "%s:invalid identifi ...

  8. freemarker.core.InvalidReferenceException

    1.错误描述 freemarker.core.InvalidReferenceException:on line 68,column 18 in ftl/inc/incPro.ftl p.mainSe ...

  9. 【原】无脑操作:IDEA + maven + SpringBoot + JPA + Thymeleaf实现CRUD及分页

    一.开发环境: 1.windows 7 企业版 2.IDEA 14 3.JDK 1.8 4.Maven 3.5.2 5.MariaDB 6.SQLYog 二.Maven设置: Maven目录下的con ...

  10. 【转】Swagger2 添加HTTP head参数

    大家使用swagger往往会和JWT一起使用,而一般使用jwt会将token放在head里,这样我们在使用swagger测试的时候并不方便,因为跨域问题它默认不能自定义head参数.然后自己去网上找, ...