如何打印受密码保护的pdf?

时间:2018-09-21 05:44:26

标签: android pdf printing passwords

  PrintDocumentAdapter pda = new PrintDocumentAdapter() {
                @Override
                public void onWrite(PageRange[] pages, final ParcelFileDescriptor destination, CancellationSignal cancellationSignal, final WriteResultCallback callback) {
                    Log.i("Write", "I Visited in Write");

                    AsyncTask.execute(new Runnable() {
                        @Override
                        public void run() {
                            InputStream input = null;
                            OutputStream output = null;
                            try {

                                String myUrlStr = Constant.URL_FILE + publisherId + "/" + filePath;
                                URL aURL = new URL(myUrlStr);
                                URLConnection conn = aURL.openConnection();
                                conn.connect();
                                input = conn.getInputStream();
                                output = new FileOutputStream(destination.getFileDescriptor());

                                byte[] buf = new byte[1024];

                              /*  byte[] key = generateKey(password);
                                byte[] decode = decodeFile(key, buf);*/
                                int bytesRead;
                                while ((bytesRead = input.read(buf)) > 0) {
                                    output.write(buf, 0, bytesRead);
                                }

                                callback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES});
                            } catch (FileNotFoundException ee) {
                                ee.printStackTrace();
                            } catch (Exception e) {
                                e.printStackTrace();
                            } finally {
                                try {
                                    if (input != null)
                                        input.close();
                                    if (output != null)
                                        output.close();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }

                        }
                    });
  }

                @Override
                public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {

                    if (cancellationSignal.isCanceled()) {
                        callback.onLayoutCancelled();
                        return;
                    }

                    PrintDocumentInfo pdi = new PrintDocumentInfo.Builder(getString(R.string.app_name) + " - " + filePath).setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();

                    callback.onLayoutFinished(pdi, true);
                }
            };
            return pda;
        }

 PrintManager printManager = (PrintManager) PreviewPdfActivity.this.getSystemService(Context.PRINT_SERVICE);
            String jobName = PreviewPdfActivity.this.getString(R.string.app_name) + " Document";
            printManager.print(jobName, pda, null);

使用上面的代码,我能够从服务器URL正确打印pdf,但是如果服务器URL的pdf受密码保护,则Logcat显示错误,无法打印受密码保护的pdf。所以任何人都可以帮助我找到解决方案。我在Google上搜索了很多解决方案,但没有找到合适的答案。

1 个答案:

答案 0 :(得分:0)

保存从服务器收到的pdf。然后使用iText库读取和打印其内容。像这样:

PdfReader reader = new PdfReader("path of pdf file with lock","password".getBytes());

iText依赖项:compile 'com.itextpdf:itextg:5.5.10'

iText