2013-06-25 65 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);