【问题标题】:How to print password protected pdf?如何打印受密码保护的pdf?
【发布时间】:2018-09-21 05:44:26
【问题描述】:
  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 的错误。所以任何人都可以帮我找到解决方案。我搜索了很多解决方案,但没有找到任何合适的答案。

【问题讨论】:

    标签: android pdf printing passwords


    【解决方案1】:

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

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

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

    iText

    【讨论】:

    • 嘿,谢谢你的回答,但我想问一下我可以使用服务器或内部存储的 iText 库直接打印 pdf,否则我必须先创建 Pdf
    • 您需要先创建 pdf。如果不输入密码也可以打印,那密码有什么意义呢:)
    • 不,我不认为这个解决方案是合适的,因为在大多数情况下,iText 不能正确地从 pdf 中提取图像,所以如果您有任何其他解决方案,请向我推荐任何其他解决方案,是的,谢谢您的回答再一次。
    猜你喜欢
    • 2021-10-16
    • 2011-12-23
    • 2021-03-28
    • 1970-01-01
    • 2015-07-20
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多