//读取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. linux中的三个文件时间

    Linux系统文件有三个主要的时间属性,分别是ctime(change time), atime(access time), mtime(modify time). 后来为了解决atime的性能问题, ...

  2. mac的一些小技巧

    切换到超级管理员: sudo -s: 让你很快的全屏之间进行切换!很方便!很实用! command+tab 今天的感觉到公司的每一个人员,对于mac的系统的使用都是非常的熟悉的,我还什么都不会. 我得 ...

  3. node实现jsonp跨域

    1. 搭建node server //引入模块 var http=require("http"); var fs=require("fs");var url = ...

  4. <CEPH中国-深圳站-技术交流会演讲PPT> YY云平台Ceph Block应用实践 & 我写的书 《CEPH实战》

    YY云平台Ceph Block应用实践 http://s3.yyclouds.com/public/YY%E4%BA%91%E5%B9%B3%E5%8F%B0Ceph%E5%AE%9E%E8%B7%B ...

  5. 4.2 PCIe体系结构的组成部件

    PCIe总线作为处理器系统的局部总线,其作用与PCI总线类似,主要目的是为了连接处理器系统中的外部设备,当然PCIe总线也可以连接其他处理器系统.在不同的处理器系统中,PCIe体系结构的实现方法略有不 ...

  6. Unable to find the ncurses libraries的解决办法

    我们在更新CentOS或者Ubuntu的内核时,执行make menuconfig可能看如这样的错误: *** Unable to find the ncurses libraries or the* ...

  7. mongodb: Remote server has closed the connection

    <?php function getMongoClient($seeds = "", $options = array(), $retry = 3) { try { retu ...

  8. SPI FLASH与NOR FLASH的区别?

    1.SPI Flash (即SPI Nor Flash)是Nor Flash的一种: 2.NOR Flash根据数据传输的位数可以分为并行(Parallel)NOR Flash和串行(SPI)NOR ...

  9. linux命令--ldconfig和ldd用法

    一.ldconfig ldconfig是一个动态链接库管理命令,为了让动态链接库为系统所共享,还需运行动态链接库的管理命令--ldconfig. ldconfig 命令的用途,主要是在默认搜寻目录(/ ...

  10. Java报SQLException

    Java报SQLException 具体错误如下: java.sql.SQLException:Column count doesn't match value count at row 1