How to get the number of pages of a pdf file in Android?

£可爱£侵袭症+ 提交于 2019-12-10 16:35:47

问题


Can you help me find out the number of pages of a pdf document on Android, that will support down to at least api version 16. I am extracting document information but can't see a solution to get the total amount of pages. This Is what I doo so far:

private void extractDocumentDataFromUri(Uri uri)
    {
        String[] dataFileds = new String[]{
                MediaStore.MediaColumns.DATA,
                OpenableColumns.DISPLAY_NAME,
                OpenableColumns.SIZE};

        Cursor cursor = context.getContentResolver().query(uri, dataFileds, null, null, null);
        cursor.moveToFirst();

        contentUrl = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA));
        title = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
        size = cursor.getInt(cursor.getColumnIndex(OpenableColumns.SIZE));

        cursor.close();

        contentType = context.getContentResolver().getType(uri);
    }

回答1:


This solution works if you add the iTextG library to your libs folder. The library contains a class called PdfReader.

Try something like this:

PdfReader reader = new PdfReader("Your file path");
int aa = reader.getNumberOfPages();



回答2:


Try this:

private void countPages(File pdfFile) throws IOException {
    try {

        ParcelFileDescriptor parcelFileDescriptor = ParcelFileDescriptor.open(pdfFile, ParcelFileDescriptor.MODE_READ_ONLY);
        PdfRenderer pdfRenderer = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            pdfRenderer = new PdfRenderer(parcelFileDescriptor);
            totalpages = pdfRenderer.getPageCount();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}


来源:https://stackoverflow.com/questions/30483373/how-to-get-the-number-of-pages-of-a-pdf-file-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!