讀取pdf中的內容

來源:互聯網
上載者:User

/**

*使用前先備好   PDFBox-0.7.2-log4j.jar

*log4j-1.2.15.jar   兩個包

*/

 

import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.util.PDFTextStripper;

**
 * 將pdf中的內容複寫到txt中
 * @author DanielCooger
 * <a href="mailto:tangjunfeng52099@gmail.com">daniel</a>
 */

public class Pdf {

// 記憶體中儲存的PDF Document
 private PDDocument document = null;
 // 是否排序
 private boolean sort = false;
 // 開始提取頁數
 private int startPage = 1;
 // 結束提取頁數
 private int endPage = Integer.MAX_VALUE;
 
 /**
  *根據輸入的源檔案路徑得到pdf檔案中的內容
  *file 參數為源檔案pdf路徑
  */
 public String readFdf(String file) throws Exception{
  // 注意參數已不是以前版本中的URL.而是File。
  document = PDDocument.load(file);
  // PDFTextStripper來提取文本
  PDFTextStripper stripper = null;
  stripper = new PDFTextStripper();
  // 設定是否排序
  stripper.setSortByPosition(sort);
  // 設定起始頁
  stripper.setStartPage(startPage);
  // 設定結束頁
  stripper.setEndPage(endPage);
  // 調用PDFTextStripper的getText提取pdf中的文本
  return stripper.getText(document);
 }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.