Yousuf Zaman
Yousuf Zaman

Reputation: 191

Open Password Protected PDF file within a viewer in android

I am developing a pdf reader, as android don't have any native pdf viewer so i have to use third party pdf viewer. in that case i have chose the Adobe pdf viewer for android. I can open pdf file which are stored in sdcard of my device. Now i want to open password protected pdf file from my application. If user wants to open password protected pdf file manually then use have to provide password while opening. but i want to open those password pdf file from my application without any password prompt. Application provide the password,[apps knows the password] and without any password prompt, pdf will open.

currently if i want to open any password protected pdf file from my application then a password prompt is appeared and needs a password to open it.

I am using this code to open pdf from my stored pdf files in the SDCARD.

====

File pdfFile = new File(fileLocation); 
if (pdfFile.exists()) 
{ 
    Uri pdfFilepathUri = Uri.fromFile(pdfFile); 
    Intent intent = new Intent(Intent.ACTION_VIEW);    

    intent.setDataAndType(pdfFilepathUri, "application/pdf");      
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    try 
    { 
        startActivity(intent);
    } 
    catch (ActivityNotFoundException e) 
    { 
        Toast.makeText(this,"No Application Available to View PDF : "+fileLocation,Toast.LENGTH_LONG).show(); 
    } 
} 
 else 
Toast.makeText(this,"File cannot found : "+fileLocation,Toast.LENGTH_SHORT).show(); 

====

can anybody please help how can i provide the password from the application, so that it can open pdf file automatically without prompting any password window. ?

Upvotes: 2

Views: 9219

Answers (3)

Persian Code
Persian Code

Reputation: 1

If PDF in SD card:

private void displayPDF() {
    
     dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Data/1.pdf/");
         
            pdfView.fromFile(dir)
                    .password("12345678")
                    .load();
}

Upvotes: -1

Michael Berry
Michael Berry

Reputation: 72294

In short, you can't. There's no PDF readers I'm aware of out there that do support this, but even if there was, you can't rely on a user having that particular reader installed - so there's no way to get this to work universally.

However, you can get around this by using a library that handles all the heavy lifting of the PDF rendering to build your own basic PDF view within your app, then use that view to open the PDF. Some such libraries, such as the AndroidPdfViewer library linked allows you to specify the password, eg:

pdfView.fromFile(file)
    .password("password123")
    .load();

This would then enable you to offer a universal solution to displaying password protected PDF files, and also have the additional security benefit of not leaking the password between contexts.

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006759

If user wants to open password protected pdf file manually then use have to provide password while opening. but i want to open those password pdf file from my application without any password prompt.

You are welcome to contact the authors of various PDF viewing apps to see if they support passing of a PDF password via an Intent extra or something. There certainly is nothing in the documented ACTION_VIEW protocol that is designed to support this scenario.

Upvotes: 1

Related Questions