Как распечатать защищенный паролем PDF? - PullRequest
0 голосов
/ 21 сентября 2018
  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);

С помощью приведенного выше кода я могу правильно распечатать PDF-файл с URL-адреса сервера, но если PDF-файл с URL-адреса сервера защищен паролем, Logcat отображает ошибку не может распечатать PDF-файл с защитой паролем.Так может кто-нибудь, пожалуйста, помогите мне найти решение.Я много гуглил для решения, но не нашел подходящего ответа.

1 Ответ

0 голосов
/ 21 сентября 2018

Сохраните PDF-файл, полученный с сервера.Затем используйте библиотеку iText для чтения и печати содержимого.Примерно так:

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

Зависимость iText: compile 'com.itextpdf:itextg:5.5.10'

iText

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...