社区
Web 开发
帖子详情
使用pdfbox怎么获得pdf的总页数?
xchh1220
2009-04-22 06:09:23
使用pdfbox怎么获得pdf的总页数?
...全文
568
3
打赏
收藏
使用pdfbox怎么获得pdf的总页数?
使用pdfbox怎么获得pdf的总页数?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
3 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
lc黑
2011-04-08
打赏
举报
回复
获得总页数:
PDDocument pdd = PDDocument.load(“c:/test.pdf”);
List allPages = pdd.getDocumentCatalog().getAllPages();
PDPage page = (PDPage)allPages.get(0);//获得第一页
xchh1220
2009-04-23
打赏
举报
回复
还是没取得总页数啊
wuyu637
2009-04-22
打赏
举报
回复
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import org.pdfbox.pdfparser.PDFParser;
import org.pdfbox.util.PDFTextStripper;
public class PdfParser {
/**
* @param args
*/
// TODO 自动生成方法存根
public static void main(String[] args) throws Exception{
FileInputStream fis = new FileInputStream("F:\\task\\lerman-atem2001.pdf");
BufferedWriter writer = new BufferedWriter(new FileWriter("F:\\task\\pdf_change.txt"));
PDFParser p = new PDFParser(fis);
p.parse();
PDFTextStripper ts = new PDFTextStripper();
String s = ts.getText(p.getPDDocument());
writer.write(s);
System.out.println(s);
fis.close();
writer.close();
}
}
下面是自己按照书上的例子写的代码。
1package TestPDF.pdfbox;
2
3import java.io.File;
4import java.io.FileOutputStream;
5import java.io.IOException;
6import java.io.OutputStreamWriter;
7import java.io.Writer;
8import java.net.URL;
9
10import org.apache.lucene.analysis.standard.StandardAnalyzer;
11import org.apache.lucene.document.Document;
12import org.apache.lucene.index.IndexWriter;
13import org.apache.lucene.index.Term;
14import org.apache.lucene.search.IndexSearcher;
15import org.apache.lucene.search.PhraseQuery;
16import org.apache.lucene.search.Query;
17import org.apache.lucene.search.ScoreDoc;
18import org.apache.lucene.search.TermQuery;
19import org.apache.lucene.search.TopDocCollector;
20import org.apache.lucene.search.TopDocs;
21import org.pdfbox.pdmodel.PDDocument;
22import org.pdfbox.searchengine.lucene.LucenePDFDocument;
23import org.pdfbox.util.PDFTextStripper;
24
25public class Test {
26
27 public void getText(String file) throws Exception{
28 //是否排序
29 boolean sort = false;
30 //pdf文件名
31 String pdfFile = file;
32 //输入文本文件名称
33 String textFile = null;
34 //编码方式
35 String encoding = "UTF-8";
36 //开始提取页数
37 int startPage = 1;
38 //结束提取页数
39 int endPage = Integer.MAX_VALUE;
40 //文件输入流,输入文本文件
41 Writer output = null;
42 //内存中存储的PDF Document
43 PDDocument document = null;
44
45 try{
46 try{
47 //首先当作一个URL来加载文件,如果得到异常再从本地系统装载文件
48 URL url = new URL(pdfFile);
49 document = PDDocument.load(url);
50 String fileName = url.getFile();
51
52 if(fileName.length() > 4){
53 //以原来pdf名称来命名新产生的txt文件
54 File outputFile = new File(fileName.substring(0, fileName.length()-4) + ".txt");
55 textFile = outputFile.getName();
56 }
57 }catch(Exception e){
58 //如果作为URL装载得到异常则从文件系统装载
59 document = PDDocument.load(pdfFile);
60 if(pdfFile.length() > 4){
61 textFile = pdfFile.substring(0, pdfFile.length() - 4) + ".txt";
62 }
63 }
64 //文件输出流,写入文件到textFile
65 output = new OutputStreamWriter(new FileOutputStream(textFile),encoding);
66 //PDFTextStripper来提取文本
67 PDFTextStripper stripper = new PDFTextStripper();
68 //设置是否排序
69 stripper.setSortByPosition(sort);
70 //设置起始页
71 stripper.setStartPage(startPage);
72 //设置结束页
73 stripper.setEndPage(endPage);
74 //调用PDFTextStripper的writeText提取并输出文本
75 stripper.writeText(document, output);
76 }finally{
77 if(output != null){
78 output.close();
79 }
80 if(document != null){
81 document.close();
82 }
83 }
84 }
85
86 /**
87 * test Lucene with pdfbox
88 * @throws IOException
89 */
90 public void LuceneTest() throws IOException{
91
92 String path = "D:\\index";
93 String pdfpath = "D:\\index\\Lucene.Net基本用法.pdf";
94
95 IndexWriter writer = new IndexWriter(path, new StandardAnalyzer(),true);
96 //writer.setMaxFieldLength(10240);
97 //LucenePDFDocument返回由PDF产生的Lucene Document
98 Document d = LucenePDFDocument.getDocument(new File(pdfpath));
99 //System.out.println(d);
100 //写入索引
101 writer.addDocument(d);
102 writer.close();
103
104 //读取d:\index下的索引文件,建立IndexSearcher
105 IndexSearcher searcher = new IndexSearcher(path);
106 //对索引的contents Field进行关键字Query的查找
107 Term t = new Term("contents","优");
108 Term m = new Term("contents","化");
109 PhraseQuery q = new PhraseQuery();
110 q.add(t);
111 q.add(m);
112 //Query q = new TermQuery(t);
113 TopDocCollector co = new TopDocCollector(10);
114 searcher.search(q,co);
115
116 Document document;
117 TopDocs docs = co.topDocs();
118 ScoreDoc[] doc = docs.scoreDocs;
119 //System.out.println(doc.length);
120
121 for(int i=0;i<doc.length;i++){
122 System.out.println("文档编号:" + doc[i].doc);
123 //document = searcher.doc(doc[i].doc);
124 }
125 }
126 /**
127 * @param args
128 */
129 public static void main(String[] args) {
130 // TODO Auto-generated method stub
131 Test test = new Test();
132 try{
133 //test.getText("D:\\index\\Lucene.Net基本用法.pdf");
134 test.LuceneTest();
135 }catch(Exception e){
136 e.printStackTrace();
137 }
138 }
139}
140
【
PDF
Box
】
PDF
Box
操作
PDF
文档之创建
PDF
文档、加载
PDF
文档、添加空白页面、删除页面、获取
总
页数
、添加文本内容、
PDF
Box
坐标系
本文详细介绍了Apache
PDF
Box
组件,包括创建
PDF
文档、加载本地和网络
PDF
、添加空白页面、删除页面、获取
总
页数
、添加单行和多行文本内容的方法,以及
PDF
Box
中的坐标系。通过实例代码展示了具体的操作过程。
Java
使用
PDF
Box
操作
PDF
文件
本文详细介绍
使用
PDF
Box
库进行
PDF
文档操作的方法,包括创建空白页面、读取文字信息、替换内容、插入图片、转换为图片、提取图片、删除页面等功能。
PDF
Box
操作
PDF
文档之创建
PDF
文档、加载
PDF
文档、添加空白页面、删除页面、获取
总
页数
、添加文本内容、
PDF
Box
坐标系-支持Android
本文详细介绍了如何
使用
Apache
PDF
Box
库进行
PDF
文档的操作,包括创建
PDF
文档、加载现有文档、添加空白页面、删除页面、获取
总
页数
、插入文本内容,以及理解
PDF
Box
的坐标系统。
java获取
pdf
文件的
总
页数
本文介绍如何
使用
Java和
pdf
box
库读取
PDF
文件的
总
页数
,包括在pom.xml中添加依赖以及示例代码。
【
Pdf
Box
】
pdf
box
解析
PDF
本文介绍了
使用
Pdf
Box
库解析
PDF
的两种方法:全文解析和区域解析。在全文解析适合文字排列规整的
PDF
,而区域解析适用于文字不规整或表格类
PDF
。通过示例代码展示了如何操作,并建议
使用
Pdf
Box
2.X版本,因为新版本在文字位置准确性上有显著提升。
Web 开发
81,111
社区成员
341,727
社区内容
发帖
与我相关
我的任务
Web 开发
Java Web 开发
复制链接
扫一扫
分享
社区描述
Java Web 开发
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章