javaandroidpdfmupdf

How to open password protected PDF using MuPDF


I want to open password protected PDF files using MuPDF but I don't know if MuPDF provides any convenient method to do so. I am showing the simple PDFs as following and it is working fine:

File file = [PATH TO THE FILE];

if (file.exists()) {
    Intent intent = new Intent(MainActivity.this, MuPDFActivity.class);
    intent.setAction(Intent.ACTION_VIEW);
    intent.setData(Uri.fromFile(file));
    startActivity(intent);
} 

I have tried to search for it on Google but I could not find any information about it.


Solution

  • I did this by customizing the MuPDFActivity.java class.

    While reading the code in onCreate method i came to know that it is has condition which checks that whether it needs password or not as:

    if (core != null && core.needsPassword()) {
    

    and then it shows dialog withe EditText to input the password. Then password is verified via the function.

    core.authenticatePassword(mPassword)
    

    I sent the password of the file as String extra to MuPDFActivity class and remove passed it directly to the function.

    if (core != null && core.needsPassword()) {
       if (core.authenticatePassword(mPassword)) {
          createUI(savedInstanceState);
       } else {
          requestPassword(savedInstanceState);
       }
       return;
    }
    

    You can ask me for complete code if anyone will need help.

    Thanks.