Open Password-Protected PDFs in Android

PDF documents can be secured with a password or with other security methods. PSPDFKit can handle protected and secured documents and write annotations and forms into a password-protected document the same as it would a non-protected document.

Sometimes documents are password secured but have an empty password set. This might sound paradoxical, but it isn’t unusual in the PDF world. In such a case, PSPDFKit will attempt to unlock documents with an empty password before showing the password dialog. This way, users don’t get confused. This behavior is the same as in Adobe Acrobat.

Password Dialog

When opening a password-protected PDF, PSPDFKit for Android will show a special prompt screen that allows a user to enter the password. This doesn’t require any additional setup and will work out of the box.

Supplying Login Credentials Programmatically

If you want to open the password-protected file transparently, i.e. by supplying the credentials programmatically so that the password prompt isn’t displayed at all, this can be achieved by using the passwords method of the PdfActivityIntentBuilder class. The code for this is similar to the following snippet:

val fileUri = Uri.parse("file:///path/to/your/document.pdf")
val intent = PdfActivityIntentBuilder.fromUri(context, fileUri)
    .passwords("file_password_goes_here")
    .build()
context.startActivity(intent)
final Uri fileUri = Uri.parse("file:///path/to/your/document.pdf");
final Intent intent = PdfActivityIntentBuilder.fromUri(context, fileUri)
    .passwords("file_password_goes_here")
    .build();
context.startActivity(intent)

If you’re using PdfUiFragment instead of PdfActivity, the idea is similar, but you’ll use the PdfUiFragmentBuilder:

val fileUri = Uri.parse("file:///path/to/your/document.pdf")
val fragment = PdfUiFragmentBuilder.fromUri(context, fileUri)
    .passwords("file_password_goes_here")
    .build()
context.startActivity(intent)
final Uri fileUri = Uri.parse("file:///path/to/your/document.pdf");
final PdfUiFragment fragment = PdfUiFragmentBuilder.fromUri(context, fileUri)
    .passwords("file_password_goes_here")
    .build();
context.startActivity(intent)

When your setup uses PdfFragment, you can achieve the above in a slightly different way, by using any of the overloads of newInstance that allow you to provide a list of passwords for the documents:

val fileUri = Uri.parse("file:///path/to/your/document.pdf")
val fragment = PdfFragment.newInstance(
    listOf(fileUri),
    listOf("file_password_goes_here"),
    configurationBuilder.build()
)
final Uri fileUri = Uri.parse("file:///path/to/your/document.pdf");
final PdfFragment fragment = PdfFragment.newInstance(
    Collections.singletonList(fileUri),
    Collections.singletonList("file_password_goes_here"),
    configurationBuilder.build()
)