读取pdf需要pdfbox.jar及pdfBox.log4j.jar;

   实现代码如下:

  

 1 package com.hksoft.tools;
 2 
 3 import java.io.File;
 4 import java.io.FileOutputStream;
 5 import java.io.OutputStreamWriter;
 6 import java.io.Writer;
 7 import java.net.MalformedURLException;
 8 import java.net.URL;
 9 
10 import org.pdfbox.pdmodel.PDDocument;
11 import org.pdfbox.util.PDFTextStripper;
12 
13 public class ReadPdf {
14     public void readFdf(String file) throws Exception {
15            // 是否排序
16            boolean sort = false;
17            // pdf文件名
18            String pdfFile = file;
19            // 输入文本文件名称
20            String textFile = null;
21            // 编码方式
22            String encoding = "UTF-8";
23            // 开始提取页数
24            int startPage = 1;
25            // 结束提取页数
26            int endPage = Integer.MAX_VALUE;
27            // 文件输入流,生成文本文件
28            Writer output = null;
29            // 内存中存储的PDF Document
30            PDDocument document = null;
31            try {
32             try {
33              // 首先当作一个URL来装载文件,如果得到异常再从本地文件系统//去装载文件
34              URL url = new URL(pdfFile);
35             //注意参数已不是以前版本中的URL.而是File。
36             document = PDDocument.load(pdfFile);
37              // 获取PDF的文件名
38              String fileName = url.getFile();
39              // 以原来PDF的名称来命名新产生的txt文件
40              if (fileName.length() > 4) {
41               File outputFile = new File(fileName.substring(0, fileName
42                 .length() - 4)
43                 + ".txt");
44               textFile = outputFile.getName();
45              }
46             } catch (MalformedURLException e) {
47              // 如果作为URL装载得到异常则从文件系统装载
48            //注意参数已不是以前版本中的URL.而是File。
49             document = PDDocument.load(pdfFile);
50              if (pdfFile.length() > 4) {
51               textFile = pdfFile.substring(0, pdfFile.length() - 4)
52                 + ".txt";
53              }
54             }
55             // 文件输入流,写入文件倒textFile
56             output = new OutputStreamWriter(new FileOutputStream(textFile),
57               encoding);
58             // PDFTextStripper来提取文本
59             PDFTextStripper stripper = null;
60             stripper = new PDFTextStripper();
61             // 设置是否排序
62             stripper.setSortByPosition(sort);
63             // 设置起始页
64             stripper.setStartPage(startPage);
65             // 设置结束页
66             stripper.setEndPage(endPage);
67             // 调用PDFTextStripper的writeText提取并输出文本
68             stripper.writeText(document, output);
69            } finally {
70             if (output != null) {
71              // 关闭输出流
72              output.close();
73             }
74             if (document != null) {
75              // 关闭PDF Document
76              document.close();
77             }
78            }
79     }
80 
81 }

这是方法类,使用可以用一下方法

    public String readPdf(){
        ReadPdf pdfReader = new ReadPdf();
           try {
            // 取得E盘下的SpringGuide.pdf的内容
            pdfReader.readFdf("E:\\11.pdf");
          //  Runtime.getRuntime().exec("C:\\Program Files (x86)\\Adobe\\Reader 11.0\\Reader\\AcroRd32.exe E:\\11.pdf");
           } catch (Exception e) {
            e.printStackTrace();
           }
           return SUCCESS;
    }

这样在你制定的pdf文档目录中会生成 同名的11.txt文档

posted on 2014-04-02 17:18  blogFree  阅读(3333)  评论(0编辑  收藏  举报