首页 > 解决方案 > 如何在android中读取块文件并显示为pdf?

问题描述

是否可以将 pdf 文件转换为一些块,然后读取这些块并在 PdfViewer android 中显示这些块?如果可能,请指导我如何做到这一点。

我找到了将pdf转换为块的这种方法:

private void getBytesFromFile(File file) throws IOException {
    FileInputStream is = new FileInputStream(file); //videorecorder stores video to file

    java.nio.channels.FileChannel fc = is.getChannel();
    java.nio.ByteBuffer bb = java.nio.ByteBuffer.allocate(10000);

    int chunkCount = 0;

    byte[] bytes;

    while(fc.read(bb) >= 0){
        bb.flip();
        //save the part of the file into a chunk
        bytes = bb.array();
        storeByteArrayToFile(bytes, mRecordingFile + "." + chunkCount);//mRecordingFile is the (String)path to file
        chunkCount++;
        bb.clear();
    }
}

private void storeByteArrayToFile(byte[] bytesToSave, String path) throws IOException {
    FileOutputStream fOut = new FileOutputStream(path);
    try {
        fOut.write(bytesToSave);
    }
    catch (Exception ex) {
        Log.e("ERROR", ex.getMessage());
    }
    finally {
        fOut.close();
    }
}

我使用AndroidPdfViewer来显示 pdf,但如果您知道其他显示 pdf 的库或方法,请提及。谢谢。

标签: androidpdf

解决方案


您可以借助 PDF 线性化来实现这一点。基本上,这是 PDF 中的一个标准功能,其中数据以这种方式组织,以实现内容的按需流式传输。为此,您还需要使用支持线性化的 PDF 查看器(例如https://www.pdftron.com/documentation/android/guides/viewer/view-online-pdfs/


推荐阅读