【问题标题】:can to print pdf using predefined print framework?可以使用预定义的打印框架打印 pdf 吗?
【发布时间】:2021-11-14 08:56:07
【问题描述】:

如何在android中使用预定义的PDF框架打印PDF

Print-Spooler Api如何打印PDF文件

【问题讨论】:

    标签: android-print-manager


    【解决方案1】:

    这段代码对我有用

    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) {
                        //Code to Catch FileNotFoundException
                    } catch (e: Exception) {
                       //Code to 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)
                }
            }
    

    【讨论】:

    • 此代码是否适用于 docx 文件?
    • 不,它仅适用于 pdf
    • 那么如何打印docx文件
    猜你喜欢
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多