Saturday, 28 August 2021

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 :

java.lang.RuntimeException: 
  at android.print.PrintManager$PrintDocumentAdapterDelegate$MyHandler.handleMessage (PrintManager.java:1103)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:246)
  at android.app.ActivityThread.main (ActivityThread.java:8506)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:602)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1139)

code that causing Exception:

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

PrintDocumentAdapter.kt

  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 (input.read(buf).also { bytesRead = it } > 0) {
                            output.write(buf, 0, bytesRead)
                        }
                    }
                    callback.onWriteFinished(arrayOf(PageRange.ALL_PAGES))
                } catch (ee: FileNotFoundException) {
                    //Catch exception
                } catch (e: Exception) {
                    //Catch exception
                } finally {
                    try {
                        input!!.close()
                        output!!.close()
                    } catch (e: IOException) {
                        e.printStackTrace()
                    }
                }
            }
    
            override fun onLayout(
                    oldAttributes: PrintAttributes,
                    newAttributes: PrintAttributes,
                    cancellationSignal: CancellationSignal,
                    callback: LayoutResultCallback,
                    extras: Bundle
            ) {
                if (cancellationSignal.isCanceled) {
                    callback.onLayoutCancelled()
                    return
                }
                val pdi = PrintDocumentInfo.Builder("Name of file")
                    .setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build()
                callback.onLayoutFinished(pdi, true)
            }
        }


from How to print password protected PDF in android?

No comments:

Post a Comment