2013-06-25 72 views
0

我是Android新手。我正在Android中使用iText實現PDF文檔。我想在我的應用程序中閱讀PDF文檔。我不知道如何在我的應用中訪問PDF文檔。我創建了,但是當我運行該應用程序時,我得到了FileNotFoundException的異常,並且我還通過DD-MS將SD卡中的PDF文件導入。但PDF文件不能在Android中打開。這裏是我的代碼:如何在Android中使用iText打開pdf文件?

import com.itextpdf.text.pdf.PdfReader; 
import com.itextpdf.text.pdf.codec.Base64.InputStream; 


public class MainActivity extends Activity 
{ 

    private static String FILE = "xyz.pdf"; 

    /** Called when the activity is first created. */ 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     AssetManager assetManager = getAssets(); 
     InputStream istr = null; 
     PdfReader reader = null; 
     String str = null; 
     int n = 0; 
     try 
     { 
      istr =(InputStream) assetManager.open("xyz.pdf"); 

      reader=new PdfReader(istr); 
      n=reader.getNumberOfPages(); 
      Log.e("n value:","-> " +n); 
      str=reader.getPageContent(2).toString(); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
     TextView tv = (TextView) findViewById(R.id.textOne); 
     tv.setText(str); 

    } 

} 
+0

請參考此答案http://stackoverflow.com/questions/13005902/itext-as-text-extracting-reading-from-pdf -on-android ,你應該在發佈問題之前做一些計算器 – dhams

回答

5

據我所知,iText的是隻針對PDF創建,它不包含觀衆part.It是一個分析器,而不是渲染器。所以,你需要選擇一些其他的庫,查看pdf.But的劇院在SO一些優秀的代碼示例,您可以查看它是否符合您的要求..

你可以嘗試其他的解決方案,而不是iText的..
- Render a PDF file using Java on Android
- http://andpdf.svn.sourceforge.net/viewvc/andpdf/trunk/

和所有最後的工作代碼。
- Example of code to implement a PDF reader

0

我們可以從iText中提取數據。我在這裏提取的PDF文件中的文本,並顯示在編輯文本:

String pat = data.getData().getPath(); 
File f = new File(pat); 

static PdfReader read; 
read = new PdfReader(new FileInputStream(f)); 

PdfReaderContentParser parser; 
parser = new PdfReaderContentParser(read); 

StringWriter strw; 
strw = new StringWriter(); 

TextExtractionStrategy stretegy; 
stretegy = parser.processContent(j, new SimpleTextExtractionStrategy()); 

strw.write(stretegy.getResultantText()); 

String da = strw.toString(); 

edt1.setText(da);