java pdfbox_java利用pdfbox处理pdf
刚开始以为java读取pdf向读取txt文件一样简单,图样图森普!乱码问题!
在网上找了下资料,发现Apache的PDFBOX,下面写一下PDFBOX读取PDF的代码。
直接搬过来
Create a blank PDF
This small sample shows how to create a new PDF document using PDFBox.
1 //Create a new empty document
2 PDDocument document = newPDDocument();3
4 //Create a new blank page and add it to the document
5 PDPage blankPage = newPDPage();6 document.addPage( blankPage );7
8 //Save the newly created document
9 document.save("BlankPage.pdf");10
11 //finally make sure that the document is properly12 //closed.
13 document.close();
Hello World using a PDF base font
This small sample shows how to create a new document and print the text "Hello World" using one of the PDF base fonts.
//Create a document and add a page to it
PDDocument document = newPDDocument();
PDPage page= newPDPage();
document.addPage( page );//Create a new font object selecting one of the PDF base fonts
PDFont font =PDType1Font.HELVETICA_BOLD;//Start a new content stream which will "hold" the to be created content
PDPageContentStream contentStream = newPDPageContentStream(document, page);//Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World"
contentStream.beginText();
contentStream.setFont( font,12);
contentStream.moveTextPositionByAmount(100, 700 );//注意这个坐标,(0,0)为本页的左下角
contentStream.drawString( "Hello World");
contentStream.endText();//Make sure that the content stream is closed:
contentStream.close();//Save the results and ensure that the document is properly closed:
document.save( "Hello World.pdf");
document.close();
Read PDF
下面是我参考网上的代码自己尝试的,官网没有具体例子介绍
其实整个过程就是 加载Document(pdf文档) 利用IO流写入到TXT文件
1 packagetools;2
3 importjava.io.File;4 importjava.io.FileNotFoundException;5 importjava.io.FileWriter;6 importjava.io.IOException;7 importjava.net.MalformedURLException;8 importjava.net.URL;9 importorg.apache.pdfbox.pdmodel.PDDocument;10 importorg.apache.pdfbox.util.PDFTextStripper;11
12 public classPDFHandler {13 public static voidreadPDF(String pdfFile) {14 String txtFile = null;15 PDDocument doc = null;16 FileWriter writer = null;17 URL url = null;18 try{19 url = newURL(pdfFile);20 } catch(MalformedURLException e) {21 //有异常说明无法转成url,以文件系统处理
22 url = null;23 }24
25 if(url != null) {//url处理
26 try{27 doc = PDDocument.load(url);//加载文档
28 String fileName =url.getFile();29 if(fileName.endsWith(".pdf")) { //得到新文件的文件名
30 File outFile = new File(fileName.replace(".pdf", ".txt"));31 txtFile =outFile.getName();32 } else{33 return;34 }35 } catch(IOException e) {36 e.printStackTrace();37 return;38 }39 } else {//文件系统处理
40 try{41 doc =PDDocument.load(pdfFile);42 if(pdfFile.endsWith(".pdf")) {43 txtFile = pdfFile.replace(".pdf", ".txt");44 } else{45 return;46 }47 } catch(IOException e) {48 e.printStackTrace();49 return;50 }51 }52 try{53 writer = newFileWriter(txtFile);54 PDFTextStripper textStripper = new PDFTextStripper();//读取PDF到TXT中的操作类
55 textStripper.setSortByPosition(false);//这个看了下官方说明,不是很确定是什么意思,但是为了提高效率最好设为false,缺省为false
56 textStripper.setStartPage(1);//起始页,缺省为第一页
57 textStripper.setEndPage(2);//结束页,缺省为最后一页
58 textStripper.writeText(doc, writer);//最重要的一步,写入到txt
59 } catch(FileNotFoundException e) {60 e.printStackTrace();61 } catch(IOException e) {62 e.printStackTrace();63 } finally{64 if(doc != null) {65 try{66 doc.close();67 } catch(IOException e) {68 e.printStackTrace();69 }70 }71 if(writer!= null) {72 try{73 writer.close();74 } catch(IOException e) {75 e.printStackTrace();76 }77 }78 }79 }80 public static voidmain(String[] args) {81 readPDF("resource/正则表达式.pdf");82 }83 }
本来处理pdf这个需求是在学习Lucene过程中提出的,不过在官网上看到了这个
Lucene Integration
Document luceneDocument = LucenePDFDocument.getDocument( ... );
好吧!
原文:http://www.cnblogs.com/erbin/p/3893450.html
java pdfbox_java利用pdfbox处理pdf相关推荐
- 如何利用pdfbox将pdf解析为txt
利用apache的pdfbox将pdf解析为txt文件,需要的最基本包如下: pdfbox-0.7.3.jar fontbox-2.0.7.jar commons-logging-1.2.jar 点我 ...
- 利用pdfbox读取pdf文件内容和图片
最近用pdfbox读取pdf文件中的内容和图片,可以获取每一页的内容和图片,但有个问题是没法获取图片在页面的位置.源码如下: package com.util; import java.awt.ima ...
- 【Java】基于Pdfbox解析PDF文档中指定位置的文字和图片
1.1 PDFBOX介绍 Apache PDFBox是一个开源Java库,支持PDF文档的开发和转换. 我们可以使用PDFBox开发可以创建,转换和操作PDF文档的Java程序.PDFBox的主要功能 ...
- java pdfbox_Java使用PDFBox开发包实现对PDF文档内容编辑与保存
pdfbox开发包下载地址:http://pdfbox.apache.org/ 程序实现了PDF文档的创建,读入,与修改PDF内容并保存. 可能有个前提,PDF文档不是加密的,如果加密怎么办,我没研究 ...
- JAVA利用pdfbox将pdf转图片
1.引入依赖 <dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</ar ...
- java中使用pdfBox打印pdf;java web打印pdf;静默打印;jar程序打包成exe文件;exe4j的使用
java编写打印控件 web打印一直是个棘手的问题,市面上的第三方打印插件也是贵的离谱,这里给出一个可行的解决方案. SpringBoot项目的web服务,实现的功能: 获取电脑可用的打印机列表 设置 ...
- html给box加超链接,java – 如何使用pdfbox在pdf中添加超链接
我想在使用PDFBOX创建的PDF中添加一个超链接,这样我点击一些文本示例"点击此处"将重定向到URL.我尝试使用PDAnnotationLink和PDActionURI,但如何在 ...
- java中利用itext编辑pdf
最近项目需要,在调研如何在pdf中增加标识字样,用来区分版本.最后确定用itext来实现. itext的官网是:http://www.itextpdf.com/ 代码如下: Java代码 /** ...
- java 使用Apache PDFBox 对 PDF 文件进行剪裁
org.apache.pdfbox pdfbox 2.0.24
最新文章
- 以下关于python缩进_Python 的缩进是不是反人类的设计?
- SpringBoot - 优雅的实现【自定义参数校验】高级进阶
- Linux命令使用汇总
- PicGo github配置
- java异常类层次结构图
- gwt-2.8.2下载_GWT EJB3 Maven JBoss 5.1集成教程
- G1垃圾收集器之SATB
- MyBatis自学(1):MyBatis概述
- c语言程序可以单独编译,c语言的函数能单独进行编译吗?
- leetcode题库1314-- 矩阵区域和
- 一个免费、大小仅几MB但超好用的卸载工具——Geek Uninstaller
- C# .NET弹出窗口大全
- 简析新型传感器的通信方式——SENT 信号
- 实例013:所有水仙花数pyt 打印出所有的“水仙花数“,所谓“水仙花数“是指一个三位数,其各位数字立方和等于该数本身。 例如:153是一个“水仙花数“,因为153=1的三次方+5的三次方+3的三次方
- 面试官没想到我对redis数据结构这么了解,直接给offer
- 7.17正则表达式与re模块
- mysql中文日期转换_mysql 日期转换
- 华硕u4000u 重装window7 重装win7 重装window10 重装win10 重装系统 戴尔 联想 鸿基 重装系统远程协助
- PHP发送邮件---phpmailer
- 用c语言验证5阶魔方矩阵,穷举法打印n阶魔方矩阵
热门文章
- SQL的注入对于安全测试的重要性~
- android qq空间效果,更新UI设计 Android手机QQ空间1.2详测
- 万全的Error:Flash Download failed-“Cortex-M3“解决方案
- 1-10 面向对象之多态
- 使用Python进行自动化录屏
- 使用Tomcat10.0.10搭建一个文件下载服务器
- (课外扩展)2-3-4树和红黑树的关系
- 【Unity3D自学记录】failed to update unity web player解决办法。
- android去除ImageButton白色边框
- 区块链房屋溯源项目搭建教程