Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Password protected PDFs not working #1172

Open
mgursch opened this issue Dec 15, 2023 · 1 comment
Open

Password protected PDFs not working #1172

mgursch opened this issue Dec 15, 2023 · 1 comment

Comments

@mgursch
Copy link

mgursch commented Dec 15, 2023

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
Copy link

nnoli90 commented Jan 28, 2024

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();`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants