apache pdfbox
转 http://www.blogjava.net/sxyx2008/archive/2010/07/23/326890.html

/**//*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.future.pdfbox.image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
public class ExtractImages 

{ 
public static void main(String[] args) throws IOException
{
PDDocument doc = PDDocument.load("F:\\1.pdf");
int pageCount = doc.getPageCount();
System.out.println(pageCount);
List pages = doc.getDocumentCatalog().getAllPages(); 
for(int i=;i<pages.size();i++)
{
PDPage page = (PDPage)pages.get(i);
BufferedImage image = page.convertToImage();
Iterator iter = ImageIO.getImageWritersBySuffix("jpg");
ImageWriter writer = (ImageWriter)iter.next();
File outFile = new File("C:/"+i+".jpg");
FileOutputStream out = new FileOutputStream(outFile);
ImageOutputStream outImage = ImageIO.createImageOutputStream(out);
writer.setOutput(outImage);
writer.write(new IIOImage(image,null,null));
}
doc.close();
System.out.println("over");
}
} 
使用PDFBox处理PDF文档http://www.cnblogs.com/hejycpu/archive/2009/01/19/1378380.html
1、使用PDFBox处理PDF文档
PDF全称Portable Document Format,是Adobe公司开发的电子文件格式。这种文件格式与操作系统平台无关,可以在Windows、Unix或Mac OS等操作系统上通用。
PDF文件格式将文字、字型、格式、颜色及独立于设备和分辨率的图形图像等封装在一个文件中。如果要抽取其中的文本信息,需要根据它的文件格式来进行解析。幸好目前已经有不少工具能帮助我们做这些事情。
2、PDFBox的下载
最常见的一种PDF文本抽取工具就是PDFBox了,访问网址http://sourceforge.net/projects/pdfbox/,进入如图7-1所示的下载界面。读者可以在该网页下载其最新的版本。本书采用的是PDFBox-0.7.3版本。PDFBox是一个开源的Java PDF库,这个库允许你访问PDF文件的各项信息。在接下来的例子中,将演示如何使用PDFBox提供的API,从一个PDF文件中提取出文本信息。
3、在Eclipse中配置
以下是在Eclipse中创建工程,并建立解析PDF文件的工具类的过程。
(1)在Eclipse的workspace中创建一个普通的Java工程:ch7。
(2)把下载的PDFBox-0.7.3.zip解压。
(3)进入external目录下,可以看到,这里包括了PDFBox所有用到的外部包。复制下面的Jar包到工程ch7的lib目录下(如还未建立lib目录,则先创建一个)。
l bcmail-jdk14-132.jar
l bcprov-jdk14-132.jar
l checkstyle-all-4.2.jar
l FontBox-0.1.0-dev.jar
l lucene-core-2.0.0.jar
然后再从PDFBox的lib目录下,复制PDFBox-0.7.3.jar到工程的lib目录下。
(4)在工程上单击右键,在弹出的快捷菜单中选择“Build Path->Config Build Path->Add Jars”命令,把工程lib目录下面的包都加入工程的Build Path。
4、使用PDFBox解析PDF内容
在刚刚创建的Eclipse工程中,创建一个ch7.pdfbox包,并创建一个PdfboxTest类。该类包含一个getText方法,用于从一个PDF中获取文本信息,其代码如下。
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
apache pdfbox的更多相关文章
- APache PDFbox API使用(1)----简单介绍
因为项目的须要.近期在学习APache PDFbox API,Apache PDFbox API是Apache Java 开源社区中个一个项目,其受Apache 版权 V2的保护,其提供了以下的功能 ...
- Apache PDFbox开发指南之PDF文档读取
转载请注明来源:http://blog.csdn.net/loongshawn/article/details/51542309 相关文章: <Apache PDFbox开发指南之PDF文本内容 ...
- 1.6.3 Uploading Data with Solr Cell using Apache Tika
1. Uploading Data with Solr Cell using Apache Tika solr使用Apache Tika工程的代码提供了一个框架,用于合并所有不同格式的文件解析器为so ...
- 使用pdfbox分页保存pdf为图片
一.背景 pdfbox作为Apache开源的PDF操作工具,允许创建新的PDF文档,操作现有文档,以及从文档中提取内容的能力.Apache PDFBox还包括一些命令行实用工具.本文楼主主要介绍其中的 ...
- PDFBox 打印带背景的文件速度慢
打印慢的原因 java的RasterPrinterJob会执行很多次printPage方法 他应该是按块填充的, 如果页面元素非常复杂, 那么printPage方法可能会执行十几次. 而如果你用了如下 ...
- Java 使用PDFBox提取PDF文件中的图片
今天做PDF文件解析,遇到一个需求:提取文件中的图片并保存.使用的是流行的apache开源jar包pdfbox, 但还是遇到坑了,比如pdfbox版本太高或太低都不能用!!这个包竟然没有很好地做好兼容 ...
- Working with PDF files in C# using PdfBox and IKVM
I have found two primary libraries for programmatically manipulating PDF files; PdfBox and iText. T ...
- Apache 项目列表功能分类便于技术选型
big-data (49): Apache Accumulo Apache Airavata Apache Ambari Apache Apex Apache Avro Apache Be ...
- java 用PDFBox 删除 PDF文件中的某一页
依赖: <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox-app ...
随机推荐
- 直接通过浏览器打开Android App 应用
点击浏览器中的URL链接,启动特定的App. 首先做成HTML的页面,页面内容格式例如以下: <a href="[scheme]://[host]/[path]?[query]&quo ...
- Cocos2d-x 3.1.1 学习日志9--一“上一下其乐无穷”游戏开发系列一
下载地址:http://app.mi.com/search?keywords=%E4%B8%80%E4%B8%8A%E4%B8%80%E4%B8%8B%E5%85%B6%E4%B9%90%E6%97% ...
- End-to-End Tracing of Ajax/Java Applications Using DTrace
End-to-End Tracing of Ajax/Java Applications Using DTrace By Amit Hurvitz, July 2007 Aja ...
- Struts工作流程
Java Web 都是使用线程来处理用户的请求(request)的,一次请求对应一个处理线程.Struts 2会为每个处理线程分配一个Action对象, 将提交的参数注射到Action属性中,并调用A ...
- Google Map API v2 番外篇 关于gps位置偏差及修正方法探讨
我的手机是M35C,在我自己的map activity中,通过gps获取到的经纬度比实际地址总是有500米左右的偏差. 在网上搜索了很多,都说这个是测绘局为了保密故意弄成这样的.gps全球定位系统获得 ...
- 30种mysql优化sql语句查询的方法<转>
转自百度文库 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中使用!=或<>操作符,否 ...
- Python获取web页面信息
import sys, urllib2 # req = urllib2.Request(sys.argv[1]) req = urllib2.Request('http://www.sina.com. ...
- apache安全之修改或隐藏版本信息
修改apache版本信息 在安装之前,编辑原文件httpd-2.2.31/include/ap_release.h文件如下: 40 #define AP_SERVER_BASEVENDO ...
- TransactionScope简单用法
记录TransactionScope简单用法,示例如下: void Test() { using (TransactionScope scope = new TransactionScope()) { ...
- jquery ajax获取和解析数据
最近项目中用到了ajax技术,之前虽然写过一点点,但是没有系统的总结过.趁着刚刚用过,手热就记录一下,方便以后查阅. $.ajax中的参数 $.ajax的函数格式: $.ajax({ type: 'P ...