PdfRenderer不访问受密码保护的pdf

时间:2017-10-18 13:20:25

标签: android security pdf pdfrenderer

    public void onBrowse(View view) {
    Intent chooseFile;
    Intent intent;
    chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
    chooseFile.addCategory(Intent.CATEGORY_OPENABLE);
    chooseFile.setType("file/*");
    intent = Intent.createChooser(chooseFile, "Choose a file");
    startActivityForResult(intent, ACTIVITY_CHOOSE_FILE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode != RESULT_OK) return;
    String path = "";
    if (requestCode == ACTIVITY_CHOOSE_FILE) {
        Uri uri = data.getData();
        Log.v("Path:", uri.getPath());
        try {
            openRenderer(HomeActivity.this, uri.getPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


private void openRenderer(Context context, String path) throws IOException {
    // In this sample, we read a PDF from the assets directory.
    File file = new File(path);
    if (!file.exists()) {
        // Since PdfRenderer cannot handle the compressed asset file directly, we copy it into
        // the cache directory.
        InputStream asset = context.getAssets().open(FILENAME);
        FileOutputStream output = new FileOutputStream(file);
        final byte[] buffer = new byte[1024];
        int size;
        while ((size = asset.read(buffer)) != -1) {
            output.write(buffer, 0, size);
        }
        asset.close();
        output.close();
    }


    mFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
    if (mFileDescriptor != null) {
        try {
            mPdfRenderer = new PdfRenderer(mFileDescriptor);
        } catch (SecurityException e) {
            Log.v("password", e.getMessage());
            e.printStackTrace();
        }
    }

}

问题是受密码保护的pdf正在获得安全性异常。

java.lang.SecurityException: cannot create document. Error: 4
10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at android.graphics.pdf.PdfRenderer.nativeCreate(Native Method)
10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at android.graphics.pdf.PdfRenderer.(PdfRenderer.java:157)
10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at com.sbw.pdfrendererview.HomeActivity.openRenderer(HomeActivity.java:205)
10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at com.sbw.pdfrendererview.HomeActivity.onActivityResult(HomeActivity.java:157)
10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at android.app.Activity.dispatchActivityResult(Activity.java:7273)
10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at android.app.ActivityThread.deliverResults(ActivityThread.java:4520)
10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:4567)
10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at android.app.ActivityThread.-wrap22(ActivityThread.java)
10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1695)
10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at android.os.Looper.loop(Looper.java:154)
10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6780)
10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
10-18 09:19:16.562 19058-19058/com.sbw.pdfrendererview W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)

2 个答案:

答案 0 :(得分:2)

PdfRenderer并非旨在呈现任意PDF文件。它专为打印时在应用程序中创建的PDF文档的打印预览而设计。因此,PdfRenderer不支持受密码保护的PDF文件。

答案 1 :(得分:2)

@Sumanta Banerjee使用iText pdf库,您可以打开受密码保护的pdf。遵循代码段

try { 
   PdfReader pdfReader = new PdfReader(String.valueOf(file));
   if (pdfReader.isEncrypted()){
     //Pass your password to view encrypted pdf.
   }
} catch(IOException) {
   e.printStackTrace(); 
}