使用pdfbox怎么获得pdf的总页数?

xchh1220 2009-04-22 06:09:23
使用pdfbox怎么获得pdf的总页数?
...全文
520 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧