android 顯示pdf檔案內容

來源:互聯網
上載者:User

先要對pdf檔案進行解析,然後顯示出來

解析pdf檔案需要pdfbox的jar包,為:http://pdfbox.apache.org/download.html

下面是解析pdf的代碼:

package com.lingjoin.extractors;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;
import java.util.Date;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
import org.apache.pdfbox.util.PDFTextStripper;
import com.lingjoin.paser.LingJoinFile;

/**
 * PDF解析器
 *
 * @author Ansj
 *
 */
public class PDFExtractor extends AbstractExtractor {
 
 private String getContent(LingJoinFile f) {
  // TODO Auto-generated method stub
  PDDocument doc = null ;
  try {
   doc = PDDocument.load(f);
   PDFTextStripper stripper = new PDFTextStripper();
   /**
    * 設定檔案的資訊
    */
   this.setLingJoinFileInfo(f, doc
     .getDocumentInformation());
   return stripper.getText(doc);
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } finally {
   if (doc != null) {
    try {
     doc.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
  return "";
 }

 private BufferedReader getContentReader(LingJoinFile f) {
  return new BufferedReader(new StringReader(this.getContent(f)));
 }

 /**
 *   
 * 項目名稱:FilePaser
 * 類描述:   設定檔案的資訊
 * 建立人:ANSJ  
 * 建立時間:2010-4-14 下午04:27:57 
 * 修改備忘:  
 * @version   
  */
 private void setLingJoinFileInfo(LingJoinFile f, PDDocumentInformation info) {
  if (info.getAuthor() != null) {
   f.setlAuthor(info.getAuthor());
  }
 }

 public void paserFileToReader(LingJoinFile f) throws Exception {
  f.setlContentReader(this.getContentReader(f)) ;

 }

 public void paserFileToString(LingJoinFile f) throws Exception {
  // TODO Auto-generated method stub
  f.setlContent(this.getContent(f)) ;
 }
 
 public PDFExtractor(Integer typeFlag) {
  // TODO Auto-generated constructor stub
  this.typeFlag = typeFlag ;
 }
 
 private Integer typeFlag = null ;
 
 public Integer getTypeFlag() {
  // TODO Auto-generated method stub
  return typeFlag;
 }
}

上面是用pdfbox實現的,只是部分不全的代碼,同時還有用動態庫so檔案,讀取圖片。

源碼 http://download.csdn.net/detail/dahuaishu2010_/5292196

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.