Read or open a PDF file using iText in android

2019-05-07 19:37发布

i am new to android application development. using iText i had done the PDF creation n write on that created file now i want to read that PDF file. how to open or read a PDF file using iText.

Examples will be appreciable..

thenx in advance.....!!!

which is the best library to render the PDF file..???? JPedal / iText / gnujpdf or anyother.....?????

2条回答
一夜七次
2楼-- · 2019-05-07 20:06

You can create your own PDF Viewer using iText, you can fetch Images for the specific page and simply display that image in a Scroll View.
But for using this approach, you will have to implement an efficient cache and set the specific pages threshold that will be made on initial run and progressively.
Here is the link, that will facilitate you:

public void makeImageFromPDF throws DocumentException,
        IOException {

    String INPUTFILE = Environment.getExternalStorageDirectory()
            .getAbsolutePath()+"/YOUR_DIRECTORY/inputFile.pdf";
    String OUTPUTFILE = Environment.getExternalStorageDirectory()
            .getAbsolutePath()+"/YOUR_DIRECTORY/outputFile.pdf";


    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document,
            new FileOutputStream(OUTPUTFILE));
    document.open();

    PdfReader reader = new PdfReader(INPUTFILE);

    int n = reader.getNumberOfPages();
    PdfImportedPage page;
    // Traversing through all the pages
    for (int i = 1; i <= n; i++) {
            page = writer.getImportedPage(reader, i);
            Image instance = Image.getInstance(page);
            //Save a specific page threshold for displaying in a scroll view inside your App
    }
    document.close();
}

You can also use this link as a reference:
Reading a pdf file using iText library
I hope this helps.

查看更多
我想做一个坏孩纸
3楼-- · 2019-05-07 20:28

Actually, iText is only for PDF creation, it doesn't contains viewer part. So, you need to choose some another library. You can follow the link provided by Azharahmed to find some useful libraries.

查看更多
登录 后发表回答