How to print password protected PDF in android?

I am printing pdf using the below code which works fine for normal pdfs but crashes with password-protected pdf. is there any way by which we can print password-protected pdf or stop applications from crashing. application crashes even print() called inside a try-catch block.

Note: I am using com.github.barteksc:android-pdf-viewer for viewing protected pdfs

Exception :

  at android.print.PrintManager$PrintDocumentAdapterDelegate$MyHandler.handleMessage (
  at android.os.Handler.dispatchMessage (
  at android.os.Looper.loop (
  at (
  at java.lang.reflect.Method.invoke (Native Method)
  at$ (
  at (

code that causing Exception:

val printManager = this.getSystemService(Context.PRINT_SERVICE) as PrintManager
        val jobName = this.getString(R.string.app_name) + " Document"
            printManager.print(jobName, pda, null)
            Toast.makeText(this,"Can't print pdf file",Toast.LENGTH_SHORT).show()


  var pda: PrintDocumentAdapter = object : PrintDocumentAdapter() {
            override fun onWrite(
                    pages: Array<PageRange>,
                    destination: ParcelFileDescriptor,
                    cancellationSignal: CancellationSignal,
                    callback: WriteResultCallback
            ) {
                var input: InputStream? = null
                var output: OutputStream? = null
                try {
                    input = uri?.let { contentResolver.openInputStream(it) }
                    output = FileOutputStream(destination.fileDescriptor)
                    val buf = ByteArray(1024)
                    var bytesRead: Int
                    if (input != null) {
                        while ( { bytesRead = it } > 0) {
                            output.write(buf, 0, bytesRead)
                } catch (ee: FileNotFoundException) {
                    //Code to Catch FileNotFoundException
                } catch (e: Exception) {
                   //Code to Catch exception
                } finally {
                    try {
                    } catch (e: IOException) {
            override fun onLayout(
                    oldAttributes: PrintAttributes,
                    newAttributes: PrintAttributes,
                    cancellationSignal: CancellationSignal,
                    callback: LayoutResultCallback,
                    extras: Bundle
            ) {
                if (cancellationSignal.isCanceled) {
                val pdi = PrintDocumentInfo.Builder("Name of file")
                callback.onLayoutFinished(pdi, true)

OR if not possible then how to get password removed from pdf.


You need not to generate pdf without password to print. as you said you are using barteksc:android-pdf-viewer for viewing pdfs which uses PDfium for rendering pdf which has a method to render bitmap from method.

void getBitmaps() {
    ImageView iv = (ImageView) findViewById(;
    ParcelFileDescriptor fd = ...;
    int pageNum = 0;
    PdfiumCore pdfiumCore = new PdfiumCore(context);
    try {
        PdfDocument pdfDocument = pdfiumCore.newDocument(fd);

        pdfiumCore.openPage(pdfDocument, pageNum);

        int width = pdfiumCore.getPageWidthPoint(pdfDocument, pageNum);
        int height = pdfiumCore.getPageHeightPoint(pdfDocument, pageNum);

        // ARGB_8888 - best quality, high memory usage, higher possibility of OutOfMemoryError
        // RGB_565 - little worse quality, twice less memory usage
        Bitmap bitmap = Bitmap.createBitmap(width, height,
        pdfiumCore.renderPageBitmap(pdfDocument, bitmap, pageNum, 0, 0,
                width, height);
        //if you need to render annotations and form fields, you can use
        //the same method above adding 'true' as last param


        printInfo(pdfiumCore, pdfDocument);

        pdfiumCore.closeDocument(pdfDocument); // important!
    } catch(IOException ex) {

store these bitmaps in arraylist and print them using android print framework