DImuthuUpe / AndroidPdfViewer

Android view for displaying PDFs rendered with PdfiumAndroid
Apache License 2.0
8.05k stars 1.85k forks source link

Password protected PDFs not working #1172

Open mgursch opened 6 months ago

mgursch commented 6 months ago

Im using the latest stable and beta version from the library. Just implemented the PDFView like in the library description.

Load the pdf from url into local cache. I open it with the following snippet

bindings.pdfView .fromUri(Uri.fromFile(file)) .defaultPage(0) .enableSwipe(true) .swipeHorizontal(true) .enableDoubletap(true) .defaultPage(0) .enableAnnotationRendering(false) .password("myPass") .load()

If i duplicate the pdf and remove the password, its working. So the integration should be fine - only the password is not working.

Any hints what im doing wrong? Multiple times checked the password (im able to open the pdf with the same password on my macbook).

nnoli90 commented 5 months ago

I faced the same issue. i solved it by 1) catching the exception thrown and checking if it was caused because of password 2) show a dialog requesting user to enter password 3) reload the pdf again using the password entered by the user

here is my code

` pdfView.fromUri(documentFile.getUri()) .enableSwipe(true) // allows to block changing pages using swipe .swipeHorizontal(false) .enableDoubletap(true) .defaultPage(0) .fitEachPage(true) .spacing(5) .onTap(new OnTapListener() { @Override public boolean onTap(MotionEvent e) { nav_toggle = !nav_toggle; toggleNavbar(nav_toggle); return false; } }) .scrollHandle(new DefaultScrollHandle(this)) .linkHandler(new DefaultLinkHandler(pdfView)) .onError(new OnErrorListener() { @Override public void onError(Throwable t) { if (t.getMessage() == null){ return; }

                        if (t.getMessage().contains("Password")){   // check if error was caused by password
                            DialogManager.enterPassword(context, new DialogManager.Password_entered() {
                                @Override
                                public void document_password(String password) {
                                    contains_password = true;
                                    pdfView.fromUri(documentFile.getUri())
                                            .enableSwipe(true) // allows to block changing pages using swipe
                                            .swipeHorizontal(false)
                                            .enableDoubletap(true)
                                            .defaultPage(0)
                                            .fitEachPage(true)
                                            .spacing(5)
                                            .onTap(new OnTapListener() {
                                                @Override
                                                public boolean onTap(MotionEvent e) {
                                                    nav_toggle = !nav_toggle;
                                                    toggleNavbar(nav_toggle);
                                                    return false;
                                                }
                                            })
                                            .scrollHandle(new DefaultScrollHandle(context))
                                            .linkHandler(new DefaultLinkHandler(pdfView))
                                            .password(password)
                                            .onError(new OnErrorListener() {
                                                @Override
                                                public void onError(Throwable t) {
                                                    if (t.getMessage() == null){
                                                        return;
                                                    }
                                                    if (t.getMessage().contains("Password")){
                                                        Toast.makeText(PDFReader.this, getString(R
                                                                .string.incorrect_password), Toast.LENGTH_SHORT).show();
                                                        finish();
                                                    }
                                                }
                                            })
                                            .load();
                                }
                            }, new DialogManager.Dismissed() {
                                @Override
                                public void dismissed(boolean value) {
                                    finish();
                                }
                            });
                        }
                    }
                })
                .load();`