java从pdf中提取文本

一(单文件转换):下载pdfbox包,百度搜pdfbox.(fontbox-1.8.16.jar和pdfbox-app-1.8.16.jar)

 1 package pdf;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileOutputStream;
 5 import java.io.OutputStreamWriter;
 6 
 7 import org.apache.pdfbox.pdfparser.PDFParser;
 8 import org.apache.pdfbox.pdmodel.PDDocument;
 9 import org.apache.pdfbox.util.PDFTextStripper;
10 
11 /**
12  * 
13  * @author 大汉
14  *
15  */
16 public class PdfToTxt {
17 
18     public PdfToTxt() {
19         super();
20         // TODO Auto-generated constructor stub
21     }
22 
23     /**
24      * 
25      * @param filename
26      * @return
27      * @throws Exception
28      */
29     public String GetTextFromPdf(String filename) throws Exception {
30         
31         String content = null;    
32         PDDocument pdfdocument = null;
33         
34         FileInputStream is = new FileInputStream(filename);
35         PDFParser parser = new PDFParser(is);
36         
37         parser.parse();        
38         pdfdocument = parser.getPDDocument();     
39         PDFTextStripper stripper = new PDFTextStripper();
40          content = stripper.getText(pdfdocument);
41          return content;    
42          } 
43     
44     /**
45      * 
46      * @param args
47      */
48     public static void main(String[] args) {
49         PdfToTxt pdfToTxt = new PdfToTxt();
50         try {
51             //获取pdf文件路径
52             String pdf = pdfToTxt.GetTextFromPdf("E:/2019a.pdf");
53             //输出到txt文件
54             OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("E:/aa.txt"));
55             osw.write(pdf);
56             osw.flush();
57             osw.close();
58         }catch (Exception e){
59             e.printStackTrace();
60         }
61         
62     }
63         
64 }

还可以这样:(第二种方法)

  1 package pdf;
  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.apache.pdfbox.pdmodel.PDDocument;
 11 import org.apache.pdfbox.util.PDFTextStripper;
 12 
 13 /**
 14  * 批量转换
 15  * @author 大汉
 16  *
 17  */
 18 public class BatchPdfToTxt {
 19 
 20     public BatchPdfToTxt() {
 21         super();
 22         // TODO Auto-generated constructor stub
 23     }
 24 
 25     public static void readPdf(String file) throws Exception {
 26         // 是否排序
 27         boolean sort = false;
 28         // pdf文件名
 29         String pdfFile = file;
 30         // 输入文本文件名称
 31         String textFile = null;
 32         // 编码方式
 33         String encoding = "UTF-8";
 34         // 开始提取页数
 35         int startPage = 1;
 36         // 结束提取页数
 37         int endPage = Integer.MAX_VALUE;
 38         // 文件输入流,生成文本文件
 39         Writer output = null;
 40         // 内存中存储的PDF Document
 41         PDDocument document = null;
 42         try {
 43             try {
 44                 // 首先当作一个URL来装载文件,如果得到异常再从本地文件系统//去装载文件
 45                 URL url = new URL(pdfFile);
 46                 //注意参数已不是以前版本中的URL.而是File。
 47                 document = PDDocument.load(pdfFile);
 48                 // 获取PDF的文件名
 49                 String fileName = url.getFile();
 50                 // 以原来PDF的名称来命名新产生的txt文件
 51                 if (fileName.length() > 4) {
 52                     File outputFile = new File(fileName.substring(0, fileName.length() - 4)+ ".txt");
 53                     textFile ="E:/"+outputFile.getName();
 54                 }
 55             } catch (MalformedURLException e) {
 56                 // 如果作为URL装载得到异常则从文件系统装载
 57                 //注意参数已不是以前版本中的URL.而是File。
 58                 document = PDDocument.load(pdfFile);
 59                 if (pdfFile.length() > 4) {
 60                     textFile = pdfFile.substring(0, pdfFile.length() - 4)+ ".txt";
 61                 }
 62             }
 63             // 文件输入流,写入文件倒textFile
 64             output = new OutputStreamWriter(new FileOutputStream(textFile),encoding);
 65             // PDFTextStripper来提取文本
 66             PDFTextStripper stripper = null;
 67             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             
 77             System.out.println(textFile + " 输出成功!");
 78         } finally {
 79             if (output != null) {
 80                 // 关闭输出流
 81                 output.close();
 82             }
 83             if (document != null) {
 84                 // 关闭PDF Document
 85                 document.close();
 86             }
 87         }
 88     }
 89     /**
 90      * 
 91      * @param args
 92      */
 93     public static void main(String[] args) {
 94         try {
 95             //注意此处的绝对地址格式,最好要用这一种。
 96             readPdf("E:/用户行为排序算法.pdf");
 97         } catch (Exception e) {
 98             e.printStackTrace();
 99         }
100     }
101 }

效果图:

總結:唯一的缺點是不能顯示圖片,請看下一篇:----------------------->>>>>>>>PDF转WORD.

猜你喜欢

转载自www.cnblogs.com/csh520mjy/p/10601259.html