PDFBox是Java实现的PDF文档协作类库,提供PDF文档的创建、处理以及文档内容提取功能,也包含了一些命令行实用工具。其主要特性包括:
1、提取PDF文件的Unicode文本
2、将PDF切分成多个PDF文件或合并多个PDF文件
3、从PDF表格中提取数据或填写PDF表格
4、验证PDF文件是否符合PDF/A-1b标准
5、使用标准的java API打印PDF文件
6、将PDF文件保存为图像文件,如PNG、JPEG
7、创建一个PDF文件,包含嵌入的字体和图像
8、PDF文件进行数字签名,即对PDF 文档进行加密与解密
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
import org.apache.pdfbox.util.PDFTextStripper;
import static readPDFContent.PDFParse.dateFormat;
* @author Angela
public class PDFReader {
* 获取格式化后的时间信息
* @param calendar 时间信息
* @return
public static String dateFormat( Calendar calendar ){
if( null == calendar )
return null;
String date = null;
String pattern = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat format = new SimpleDateFormat( pattern );
date = format.format( calendar.getTime() );
return date == null ? "" : date;
/**打印纲要**/
public static void getPDFOutline(String file){
try {
FileInputStream fis = new FileInputStream(file);
PDDocument document=PDDocument.load(fis);
PDDocumentCatalog catalog=document.getDocumentCatalog();
PDDocumentOutline outline=catalog.getDocumentOutline();
PDOutlineItem item=outline.getFirstChild();
if(outline!=null){
while(item!=null){
System.out.println("Item:"+item.getTitle());
PDOutlineItem child=item.getFirstChild();
while(child!=null){
System.out.println(" Child:"+child.getTitle());
child=child.getNextSibling();
item=item.getNextSibling();
document.close();
fis.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(PDFBOXReader.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(PDFBOXReader.class.getName()).log(Level.SEVERE, null, ex);
/**打印一级目录**/
public static void getPDFCatalog(String file){
try {
FileInputStream fis = new FileInputStream(file);
PDDocument document=PDDocument.load(fis);
PDDocumentCatalog catalog=document.getDocumentCatalog();
PDDocumentOutline outline=catalog.getDocumentOutline();
if(outline!=null){
PDOutlineItem item=outline.getFirstChild();
while(item!=null){
System.out.println("Item:"+item.getTitle());
item=item.getNextSibling();
document.close();
fis.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(PDFBOXReader.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(PDFBOXReader.class.getName()).log(Level.SEVERE, null, ex);
/**获取PDF文档元数据**/
public static void getPDFInformation(String file){
try {
FileInputStream fis = new FileInputStream(file);
PDDocument document=PDDocument.load(fis);
/** 文档属性信息 **/
PDDocumentInformation info = document.getDocumentInformation();
System.out.println("页数:"+document.getNumberOfPages());
System.out.println( "标题:" + info.getTitle() );
System.out.println( "主题:" + info.getSubject() );
System.out.println( "作者:" + info.getAuthor() );
System.out.println( "关键字:" + info.getKeywords() );
System.out.println( "应用程序:" + info.getCreator() );
System.out.println( "pdf 制作程序:" + info.getProducer() );
System.out.println( "Trapped:" + info.getTrapped() );
System.out.println( "创建时间:" + dateFormat( info.getCreationDate() ));
System.out.println( "修改时间:" + dateFormat( info.getModificationDate()));
document.close();
fis.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(PDFReader.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(PDFReader.class.getName()).log(Level.SEVERE, null, ex);
/**提取pdf文本**/
public static void extractTXT(String file){
try{
FileInputStream fis = new FileInputStream(file);
PDFParser parser = new PDFParser(fis);
parser.parse();
PDDocument document=parser.getPDDocument();
PDFTextStripper stripper = new PDFTextStripper();
String content = stripper.getText(document);
System.out.println( "内容:" + content );
document.close();
fis.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(PDFReader.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(PDFReader.class.getName()).log(Level.SEVERE, null, ex);
* 提取部分页面文本
* @param file pdf文档路径
* @param startPage 开始页数
* @param endPage 结束页数
public static void extractTXT(String file,int startPage,int endPage){
try{
FileInputStream fis = new FileInputStream(file);
PDFParser parser = new PDFParser(fis);
parser.parse();
PDDocument document=parser.getPDDocument();
PDFTextStripper stripper = new PDFTextStripper();
stripper.setStartPage(startPage);
stripper.setEndPage(endPage);
String content = stripper.getText(document);
System.out.println( "内容:" + content );
document.close();
fis.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(PDFReader.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(PDFReader.class.getName()).log(Level.SEVERE, null, ex);
* 提取图片并保存
* @param file PDF文档路径
* @param imgSavePath 图片保存路径
public static void extractImage(String file,String imgSavePath){
try{
FileInputStream fis = new FileInputStream(file);
PDDocument document=PDDocument.load(fis);
/** 文档页面信息 **/
PDDocumentCatalog catalog = document.getDocumentCatalog();
List pages = catalog.getAllPages();
int count = 1;
int pageNum=pages.size();
for( int i = 0; i < pageNum; i++ ){
PDPage page = ( PDPage ) pages.get( i );
if( null != page ){
PDResources resource = page.findResources();
Map<String,PDXObjectImage> imgs = resource.getImages();
for(Map.Entry<String,PDXObjectImage> me: imgs.entrySet()){
PDXObjectImage img = me.getValue();
img.write2file( imgSavePath + count );
count++;
document.close();
fis.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(PDFReader.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(PDFReader.class.getName()).log(Level.SEVERE, null, ex);
* 提取文本并保存
* @param file PDF文档路径
* @param savePath 文本保存路径
public static void extractTXT(String file,String savePath){
try{
FileInputStream fis = new FileInputStream(file);
PDFParser parser = new PDFParser(fis);
parser.parse();
PDDocument document=parser.getPDDocument();
PDFTextStripper stripper = new PDFTextStripper();
Writer writer=new OutputStreamWriter(new FileOutputStream(savePath));
stripper.writeText(document, writer);
writer.close();
document.close();
fis.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(PDFReader.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(PDFReader.class.getName()).log(Level.SEVERE, null, ex);
* 提取部分页面文本并保存
* @param file PDF文档路径
* @param startPage 开始页数
* @param endPage 结束页数
* @param savePath 文本保存路径
public static void extractTXT(String file,int startPage,
int endPage,String savePath){
try{
FileInputStream fis = new FileInputStream(file);
PDFParser parser = new PDFParser(fis);
parser.parse();
PDDocument document=parser.getPDDocument();
PDFTextStripper stripper = new PDFTextStripper();
Writer writer=new OutputStreamWriter(new FileOutputStream(savePath));
stripper.setStartPage(startPage);
stripper.setEndPage(endPage);
stripper.writeText(document, writer);
writer.close();
document.close();
fis.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(PDFReader.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(PDFReader.class.getName()).log(Level.SEVERE, null, ex);
public static void main(String args[]){
String file="F:\\pdf\\2013\\000608_阳光股份_2013年年度报告(更新后)_1.pdf";
String savePath="E:\\result1.txt";
long startTime=System.currentTimeMillis();
extractTXT(file,savePath);
long endTime=System.currentTimeMillis();
System.out.println("读写所用时间为:"+(endTime-startTime)+"ms");
注意:加密的pdf文档上面的程序读不了,无法解析的pdf文档会报异常,印刷版和图片式的pdf文档无法提取出文本,无法解析。
PDFBox是一个用于创建和处理PDF文档的Java库。它可以使用Java代码创建、读取、修改和提取PDF文档中的内容。PDFBox的功能:Extract Text - 使用PDFBox,您可以从PDF文件中提取Unicode文本。Split & Merge - 使用PDFBox,您可以将单个PDF文件分成多个文件,并将它们合并为一个文件。Fill Forms - 使用PDFBox,您可以在文档中填写表单数据。Print - 使用PDFBox,您可以使用标准Java打印API打印PDF文件。
摘要:最近需要将一批PDF文件中的某些数据整理到Excel中,因为文件数量接近20w+,手动更新几乎不现实,于是就提取关键词和内容动手写了个Python小工具,以实现自动完成上述目标。
作者:yooongchun
微信公众号: yooongchun小屋
读取PDF文件找到特定关键字,然后读取其对应的数值提取出来
在Excel中查找对应关键字,然后在对应位置把上面提取出来的内容填进去
基本实现过程:
遍历文件夹,按照特定的要求找出指定类型的PDF文件
解析PDF文件
提取指定内容和对应值
更新数据到Excel
所需工具:
解析PDF文件的模块:pdfminer
操作Excel的模块:xlwt、xlrd、xlutils
注意:要在一个已经存在的Excel中写入数据需要配合xlutils使用,即先copy一个Excel对象,在该对象中进行写入,最后删除原对象而保存copy出来的对象
(5)、5执行效果,将多页的pdf文件进行一页一页分割。如上的示例代码,依次按照顺序执行main方法示例。(2)执行2效果:创建新文件且插入文字。(4)、4执行效果,合并pdf会出现2页。(3)、3执行效果,插入图片成功。在代码的如下位置引入该字体文件。
I am newbie in Apache PDFbox. I want to extract all bookmarks in PDF file using PDFBox library in Java. Any idea how to extract them?解决方案From the PrintBookmarks example in the source code downloadPDDo...
PDF解析入门案例介绍注意点案例创建项目引入依赖读取PDF文本内容读取所有页,所有文本按页读取文本按坐标读取
Apache PDFBox是一个开源Java库,支持PDF文档的开发和转换。 使用此库,您可以开发用于创建,转换和操作PDF文档的Java程序。
PDFbox这个PDF处理类库,我使用过程中,能够满足我在一些场景中的需求,达成了我想要的效果,最后在此做一个使用demo的介绍,希望能够给大家带来帮助!
Apache-PDFbox
PDFvox-快速指南-WIKI
PDF内容是按坐标进行定
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.OutputStreamWriter;
Apache PDFBox库是一个用于处理PDF文档的开源Java工具。该项目允许创建新的PDF文档,操作现有PDF文档,并从PDF文档中提取内容。Apache PDFBox还包括几个命令行实用程序。
因为在apache官网上下载PDFBOX1.2jar包时,数据文件有丢失,未成功下载。所以用旧版本PDFBox-0.7.3的。
下载PDFBox-0.7.3后,可以找到需要的两个jar包,在不同的文件夹里。
1、准备两个jar包 PDFBox-0.7.3.jar,FontBox-0.1.0-dev.jar
import org.pdfbox.pdmodel.PDDocument;im...