简体   繁体   English

如何在Android中使用iText打开pdf文件?

[英]How to open pdf file using iText in Android?

I'm new in Android . 我是Android新手。 I'm implementing PDF document using iText in Android . 我正在Android中使用iText实现PDF文档。 I want to read PDF document in my application. 我想在我的应用程序中阅读PDF文档。 I don't have an idea how to access PDF doc in my app. 我不知道如何在应用程序中访问PDF文档。 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. 我已经创建了,但是当我运行应用程序时,我得到FileNotFoundException例外,并且我还通过DD-MS将PDF文件导入到SD卡中。 But the PDF file does not open in Android. 但是PDF文件无法在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. 据我所知,iText仅用于PDF创建,它不包含查看器部件。它是解析器,而不是渲染器。 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.. 因此,您需要选择另一个库来查看pdf。但是在SO中有一些出色的代码示例,您可以查看它是否符合您的要求。

You can try other solution rather than iText.. 您可以尝试其他解决方案,而不是iText。
- Render a PDF file using Java on Android - 在Android上使用Java渲染PDF文件
- http://andpdf.svn.sourceforge.net/viewvc/andpdf/trunk/ -http://andpdf.svn.sourceforge.net/viewvc/andpdf/trunk/

And an excellent working code last of all.. 最后,一个出色的工作代码。
- Example of code to implement a PDF reader - 实现PDF阅读器的代码示例

We can Extract data from iText. 我们可以从iText中提取数据。 Here i am extracting text from PDFs file and showing on Edit-text : 在这里,我从PDF文件中提取文本并显示在Edit-text上:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM