AndroidPrintDocumentAdapter:第一页的后半部分没有反映在第二页上

w8f9ii69  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(169)

我有一个printdocumentadapter,它只打印2页文档的第一页。有什么想法吗?我怎么打印这两页?从下面的代码中,它只复制第一页,不在两页之间拆分
printedrecipeview是一个自定义pdf创建者

class RecipePrintAdapter(
    private val context: Context,
    private val printContent: PrintContent,
    private val onPrintFinish: () -> Unit
) : PrintDocumentAdapter() {

    data class PrintContent(val recipe: Recipe, val privateNotes: List<PrivateNote>) {
        val jobName get() = recipe.name.replace(' ', '_')
    }

    private lateinit var printContext: Context
    private lateinit var printAttributes: PrintAttributes
    private lateinit var printedView: PrintedRecipeView
    private var oldPageRenderWidth = 0
    private var oldPageRenderHeight = 0

    override fun onLayout(
        oldAttributes: PrintAttributes,
        newAttributes: PrintAttributes,
        cancellationSignal: CancellationSignal,
        callback: LayoutResultCallback,
        extras: Bundle
    ) {
        if (cancellationSignal.isCanceled) {
            callback.onLayoutCancelled()
            return
        }

        printAttributes = newAttributes

        var contentHeight = 0
        var contentWidth = 0

        val density = max(
            printAttributes.resolution?.horizontalDpi ?: 0,
            printAttributes.resolution?.verticalDpi ?: 0
        )

        printAttributes.mediaSize?.let { size ->
            contentWidth = millsToDip(size.widthMils, density)
            contentHeight = millsToDip(size.heightMils, density)
        }

        doLayout(contentWidth, contentHeight, density)

        val documentInfo = PrintDocumentInfo.Builder("${printContent.jobName}.pdf")
            .setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
            .setPageCount(2)
            .build()

        callback.onLayoutFinished(documentInfo, true)
    }

    override fun onFinish() {
        onPrintFinish()
    }

    private fun doLayout(pageRenderWidth: Int, pageRenderHeight: Int, density: Int) {
        oldPageRenderWidth = pageRenderWidth
        oldPageRenderHeight = pageRenderHeight

        context.setTheme(R.style.AppTheme)
        printContext = context

        printedView = PrintedRecipeView(printContext)
        printedView.setup(printContent)
        layoutView(printedView, density)
    }

    private fun layoutView(
        view: View,
        density: Int
    ) {
        val mediaSize = PrintAttributes.MediaSize.ISO_A4
        val layoutWidth = millsToDip(mediaSize.widthMils, density)

        measureViewByWidth(view, layoutWidth)
        view.layout(0, 0, view.measuredWidth, view.measuredHeight)
    }

    override fun onWrite(
        pageRanges: Array<out PageRange>,
        destination: ParcelFileDescriptor,
        cancellationSignal: CancellationSignal,
        callback: WriteResultCallback
    ) {
        if (cancellationSignal.isCanceled) {
            callback.onWriteCancelled()
            return
        }

        val pdfDocument = PrintedPdfDocument(printContext, printAttributes)

        val scale: Float = min(
            pdfDocument.pageContentRect.width().toFloat() / printedView.measuredWidth.toFloat(),
            pdfDocument.pageContentRect.height().toFloat() / printedView.measuredHeight.toFloat()
        )

        for (i in 0 until 2) {
            pdfDocument.startPage(i).also { page ->
                page.canvas.scale(scale, scale)
                printedView.draw(page.canvas)
                pdfDocument.finishPage(page)
            }
        }

        try {
            pdfDocument.writeTo(FileOutputStream(destination.fileDescriptor) as OutputStream?)
        } catch (e: IOException) {
            callback.onWriteFailed(context.getString(R.string.print_error_message))
            return
        } finally {
            pdfDocument.close()
        }

        callback.onWriteFinished(arrayOf(PageRange.ALL_PAGES))
    }

    private fun measureViewByWidth(view: View, width: Int) {
        view.measure(
            View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),
            View.MeasureSpec.makeMeasureSpec(oldPageRenderHeight, View.MeasureSpec.EXACTLY)
        )
    }

    private fun millsToDip(mills: Int, density: Int) =
        (density.toFloat() * mills.toFloat() / 1000f).toInt()
}

它在末尾生成两页,但第二页与第一页重复

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题