如何在Android中使用的iText来打开PDF文件? [英] How to open pdf file using iText in Android?

查看:749
本文介绍了如何在Android中使用的iText来打开PDF文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android的是新。我实施的Andr​​oid利用iText PDF文档。我想在我的应用程序阅读PDF文档。我没有一个想法如何访问PDF文档在我的应用程序。我已经创建,但是当我运行该应用程序我得到例外 FileNotFoundException异常,我也通过进口DD-MS SD卡的PDF文件。但是,PDF文件无法在Android的开放。这里是我的code:

 进口com.itextpdf.text.pdf.PdfReader;
。进口com.itextpdf.text.pdf codec.Base64.InputStream;
公共类MainActivity扩展活动
{    私人静态字符串FILE =xyz.pdf;    / **当第一次创建活动调用。 * /    @覆盖
    公共无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        AssetManager assetManager = getAssets();
        InputStream的ISTR = NULL;
        PdfReader读卡器= NULL;
        字符串str = NULL;
        INT N = 0;
        尝试
        {
            ISTR =(InputStream的)assetManager.open(xyz.pdf);            读者=新PdfReader(ISTR);
            N = reader.getNumberOfPages();
            Log.e(N值:, - >中+ N);
            海峡= reader.getPageContent(2)的ToString();
        }
        赶上(例外五)
        {
            e.printStackTrace();
        }
        TextView的电视=(的TextView)findViewById(R.id.textOne);
        tv.setText(STR);    }}


解决方案

据我所知,iText的是只针对PDF创建,它不包含浏览器part.It是一个分析器,而不是渲染器。所以,你需要选择一些其他的库来查看pdf.But的剧院是在这么一些优秀code例如,你可以查看它是否符合您的要求。

您可以尝试其他的解决方案,而不是iText的..结果
  - 在Android应用中显示PDF 结果
  - http://andpdf.svn.sourceforge.net/viewvc/andpdf/trunk/

和所有的优良的工作code最后..结果
  - code的实例来实现PDF阅读器

I'm new in Android . I'm implementing PDF document using iText in Android . I want to read PDF document in my application. I don't have an idea how to access PDF doc in my app. I have created but when I run the application I get exception for FileNotFoundException and I also import a PDF file in SD card through DD-MS. But the PDF file does not open in Android. Here is my code:

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);

    }

}

解决方案

As far as i know,iText is only for PDF creation, it doesn't contain viewer part.It is a parser, not a renderer. So you need to choose some another library to view pdf.But theare are some excellent code example in SO which you can view whether it meets your requirement..

You can try other solution rather than iText..
- Display PDF within app on Android?
- http://andpdf.svn.sourceforge.net/viewvc/andpdf/trunk/

And an excellent working code last of all..
- Example of code to implement a PDF reader

这篇关于如何在Android中使用的iText来打开PDF文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆