需要pdfbox和log4j的包
举个例子:
import org.pdfbox.pdfparser.*;
import org.pdfbox.util.PDFTextStripper;
import java.io.*;
/**
 * 测试pdfbox
 * @author kingfish
 * @version 1.0
 */
public class TestPdf {
  public static void main(String[] args) throws Exception{
    FileInputStream fis = new FileInputStream("c://intro.pdf");
    PDFParser p = new PDFParser(fis);
    p.parse();   
    PDFTextStripper ts = new PDFTextStripper();   
    String s = ts.getText(p.getPDDocument());
    System.out.println(s);
    fis.close();
  }
}

--------------------------------------------------------------------------------

import java.io.*;
import java.util.*;
import com.etymon.pj.*;
import com.etymon.pj.object.*;
import com.etymon.pj.exception.*;
/**
* This is a wrapper for the Pj PDF parser
*/
public class PjWrapper {
Pdf pdf;
PjCatalog catalog;
PjPagesNode rootPage;
public PjWrapper(String PdfFileName,String TextFileName)throws
IOException, PjException {
pdf = new Pdf(PdfFileName);
// hopefully the catalog can never be a reference...
catalog = (PjCatalog) pdf.getObject(pdf.getCatalog());
// root node of pages tree is specified by a reference in the catalog
rootPage = (PjPagesNode) pdf.resolve(catalog.getPages());
}
public static void main (String [] args) throws IOException, PjException
{
/*PjWrapper testWrapper = new PjWrapper(args[0]);
LinkedList textList = testWrapper.getAllText();*/
}
/**
* Returns as much text as we can extract from the PDF.
* This currently includes:
*
* NOTE: Pj does not support LZW, so some text in some PDF's may not
* be indexable
*/
public LinkedList getAllText() throws PjException {
LinkedList stringList = new LinkedList();
Iterator streamIter = getAllContentsStreams().iterator();
PjStream stream;
String streamData;
String streamText;
boolean moreData;
int textStart, textEnd;
//System.out.println("Going through streams...");
while(streamIter.hasNext()) {
//System.out.println("Getting next stream");
stream = (PjStream) streamIter.next();
//System.out.println("Adding text from stream with filter: "
+getFilterString(stream);
stream = stream.flateDecompress();
//System.out.println("Adding text from stream with filter
afterdecompress: " + getFilterString(stream));
streamData = new String(stream.getBuffer());
streamText = new String();
moreData = true;
textStart = textEnd = 0;
while(moreData) {
if ((textStart = streamData.indexOf('(', textEnd + 1)) < 0) {
moreData = false;
break;
}
if ((textEnd = streamData.indexOf(')', textStart + 1)) < 0) {
moreData = false;
break;
}
try {
streamText +=
PjString.decodePdf(streamData.substring(textStart,textEnd + 1));
} catch (Exception e) {
System.out.println("malformed string: " +
streamData.substring(textStart, textEnd + 1));
}
}
//if(streamText.equals("inserted text"))
System.out.println(streamText);
if (streamText.length() > 0)
stringList.add(streamText);
}
return stringList;
}
public static String getFilterString(PjStream stream) throws PjException
{
String filterString = new String();
PjObject filter;
//System.out.println("getting filter from dictionary");
if ((filter = stream.getStreamDictionary().getFilter()) == null) {
//System.out.println("Got null filter");
return "";
}
//System.out.println("got it");
// filter should either be a name or an array of names
if (filter instanceof PjName) {
//System.out.println("getting filter string from simple name");
filterString = ((PjName) filter).getString();
} else {
//System.out.println("getting filter string from array of names");
Iterator nameIter;
Vector nameVector;
if ((nameVector = ((PjArray) filter).getVector()) == null) {
//System.out.println("got null vector for list of names");
return "";
}
nameIter = nameVector.iterator();
while (nameIter.hasNext()) {
filterString += ((PjName) nameIter.next()).getString();
if (nameIter.hasNext())
filterString += " ";
}
}
//System.out.println("got filter string");
return filterString;
}
/**
* Performs a post-order traversal of the pages tree
* from the root node and gets all of the contents streams
* @returns a list of all the contents of all the pages
*/
public LinkedList getAllContentsStreams() throws
InvalidPdfObjectException {
return getContentsStreams(getAllPages());
}
/**
* Get contents streams from the list of PjPage objects
* @returns a list of all the contents of the pages
*/
public LinkedList getContentsStreams(LinkedList pages) throws
InvalidPdfObjectException {
LinkedList streams = new LinkedList();
Iterator pageIter = pages.iterator();
PjObject contents;
while(pageIter.hasNext()) {
contents = pdf.resolve(((PjPage)pageIter.next()).getContents());
// should only be a stream or an array of streams (or refs to
streams)
if (contents instanceof PjStream)
streams.add(contents);
else{
Iterator streamsIter = ((PjArray)contents).getVector().iterator();
while(streamsIter.hasNext())
streams.add(pdf.resolve((PjObject)streamsIter.next()));
}
}
return streams ;
}
/**
* Performs a post-order traversal of the pages tree
* from the root node.
* @returns a list of all the PjPage objects
*/
public LinkedList getAllPages() throws InvalidPdfObjectException {
LinkedList pages = new LinkedList();
getPages(rootPage, pages);
return pages;
}
/**
* Performs a post-order traversal of the pages tree
* from the node passed to it.
* @returns a list of all the PjPage objects under node
*/
public void getPages(PjObject node, LinkedList pages) throws
InvalidPdfObjectException {
PjPagesNode pageNode = null;
// let's hope pdf's don't have pointers to pointers
if (node instanceof PjReference)
pageNode = (PjPagesNode) pdf.resolve(node);
else
pageNode = (PjPagesNode) node;
if (pageNode instanceof PjPage) {
pages.add(pageNode);
return;
}
// kids better be an array and not a reference to one
Iterator kidIterator = ((PjArray) ((PjPages)
pageNode).getKids()).getVector().iterator();
while(kidIterator.hasNext()) {
getPages((PjObject) kidIterator.next(), pages);
}
}
public Pdf getPdf() {
return pdf;
}
}

java 读取pdf相关推荐

  1. java读取pdf文本转换html

    java读取pdf文本转换html 完整代码地址 也就两个文件 java读取pdf中的纯文字,这里使用的是pdfbox工具包 maven引入如下配置 <dependency><gro ...

  2. Java读取pdf模板,并动态生成pdf文件,如动态生成准考证

    Java读取pdf模板,并动态生成pdf文件,如动态生成准考证 ​ 前几天遇到了一个生成准考证的需求,并提供用户下载,然后百度了一圈还是觉得使用itextpdf这个框架好用点.但是还需要找到一个能创建 ...

  3. 用Java读取pdf中的数据

    用Java简单的读取pdf文件中的数据: 第一步:下载PDFBox-0.7.2.jar.提供一个下载地址:[url]http://pdfhome.hope.com.cn/Resource.aspx?C ...

  4. java读取pdf总结

    第三方软件 1.pdfbox PDFBox 0.7.3.PDFBox是一个开源的对pdf文件进行操作的库. PDFBox-0.7.3.jar加入classpath.同时FontBox1.0.jar加入 ...

  5. Java 读取PDF中表格的工具

    目录 1.方法1:Spire.PDF 1.1 Maven仓库下载导入 1.2 读取PDF中的表格 1.2.1 代码 1.2.2 表格内容 1.2.3 读取结果 2.方法2:Tabula 2.1 Mav ...

  6. java 读取pdf各级标题_Java那些事之pdf读取

    今天说说java中的pdf读取,实话说,这是一个很恼人的任务.现在网络上的pdf各种各样,分析出来的文本也是千差万别,目前最流行的jar包pdfbox也只能做到把文字分析出来,并无法很好的控制分析的顺 ...

  7. Java 读取PDF中的文本和图片

    本文将介绍通过Java程序来读取PDF文档中的文本和图片的方法.分别调用方法extractText()和extractImages()来读取. 使用工具:Free Spire.PDF for Java ...

  8. java 读取pdf、word、Excel文件

    用到的jar: itextpdf-5.5.8.jar   (PDF) poi.jar public class FileUtils {/*** 判断文件是否存在* * @Title: isExcite ...

  9. java pdf分页显示,java读取pdf(可分页读取)

    需要pdfbox和log4j的包 举个例子: import org.pdfbox.pdfparser.*; import org.pdfbox.util.PDFTextStripper; import ...

  10. java 读取pdf签名域_Java给PDF签名

    1.引入依赖 com.e-iceblue e-iceblue http://repo.e-iceblue.cn/repository/maven-public/ e-iceblue spire.pdf ...

最新文章

  1. mysql之触发器详解_学习笔记之MySQL触发器详解
  2. 第二章 选择符和属性
  3. Asp.net中水晶报表的使用
  4. 「PowerBI」分析服务多维数据结构重回关系数据库的一大绝招
  5. 网络层:IP协议详解(IP协议真的得看这篇)
  6. MySQL 服务无法启动--服务没有报告任何错误 ---Failed to find valid data directory.
  7. 媒体查询 200304
  8. php _call call_user_func_array,PHP call_user_func和call_user_func_array函数的简单理解与应用分析...
  9. python-12:怎么在爬虫代码中伪装header
  10. 按搜索量排名前100位访问量最高的网站(截至2020年)
  11. java求正方形的面积周长_三角形正方形 圆和正五边形的周长和面积计算 java实现.doc...
  12. chromium浏览器定制 | 高匿名爬虫随机指纹
  13. 卷死我了,终于毕业了!!
  14. 云计算基础:云计算运用越来越广泛,我们应该如何去学习云计算
  15. 传智播客成都java培训中心的发展
  16. QT命令行编译时出现'mingw32-make'不是内部或外部命令,也不是可运行的程序或批处理文件
  17. redsocks+iptables+socks5服务商
  18. DAM | 提升品牌自播引流能力的「幕后」推手 | 数字内容管理 引流营销资源
  19. java数值运算divide_Java常用的计算机数字运算函数
  20. Python-count函数

热门文章

  1. odd在c语言中的意思,odd是什么意思(自动驾驶汽车的ODD介绍)
  2. 【华为OD机试真题 JAVA】5键键盘的输出
  3. JAVA读取文件内容,按格式输出文件
  4. 高效的王者荣耀视频英雄检测与识别
  5. Tableau豆瓣电影项目实战作业 Day9
  6. 电脑右键打印不见了_求救电脑高手电脑突然出现一个打印机之类的东东后,右键就失常了,原 爱问知识人...
  7. 2015年北京12区各大高中实力榜,大排名!
  8. kvm基础设置 网桥
  9. P图软件瘦脸眼睛放大和手动瘦身功能
  10. 数据结构 | 线性查找