PdfDocument 的内容高度不正确

问题描述 投票:0回答:1

我正在根据滚动视图的内容创建 PDF。然而,每页底部都有一个空白区域。我根据传递给 PrintDocumentAdapter 的 onLayout 方法的 newAttributes 计算高度。

val pageSize = newAttributes.mediaSize?.heightMils

这就是我用来设置位图高度的方法,但似乎位图比实际页面大小短,因此底部有空白。

我检查了像素到密耳的转换,反之亦然。由于我使用媒体属性的高度来打印到 pdf 页面,因此我希望整个页面都有内容。我在这里可能缺少什么?

代码:

printManager.print(jobName, object : PrintDocumentAdapter() {
      override fun onLayout(oldAttributes: PrintAttributes?, newAttributes: PrintAttributes, cancellationSignal: CancellationSignal?, callback: LayoutResultCallback?, extras: Bundle?) {
        pdfDocument = PrintedPdfDocument(context, newAttributes)

        if (cancellationSignal?.isCanceled == true) {
          callback?.onLayoutCancelled()
          return
        }

        // This value is in mils, which is thousands of an inch
        pageSize = newAttributes.mediaSize?.heightMils!!

          // Convert to mils
        val contentSize = scrollView.getChildAt(0).height * 10
        

        numberOfPages = ceil(((contentSizeMils / pageSize).toDouble())).toInt()
        if (numberOfPages > 0) {
          PrintDocumentInfo.Builder(jobName)
            .setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
            .setPageCount(numberOfPages)
            .build()
            .also { info ->
              callback?.onLayoutFinished(info, true)
            }
        } else {
          callback?.onLayoutFailed("Page count calculation failed.")
        }
      }

      override fun onWrite(pages: Array<PageRange>?, destination: ParcelFileDescriptor?, cancellationSignal: CancellationSignal?, callback: WriteResultCallback?) {
        for (i in 0 until numberOfPages) {

          // Convert to pixel
          val printableHeight = pageSize / 10

          val pageInfo = PdfDocument.PageInfo.Builder(scrollView.getChildAt(0).width, printableHeight, i).create()
          val page = pdfDocument?.startPage(pageInfo)
          val canvas = page?.canvas

          // Calculate the portion of the content to be drawn for this page
          val start = i * printableHeight
          val end = ((i + 1) * printableHeight)

          // Create bitmap of the portion of the scroll view content for this page
          val bitmap = Bitmap.createBitmap(scrollView.getChildAt(0).width, end - start, Bitmap.Config.ARGB_8888)

          val contentCanvas = Canvas(bitmap)
          contentCanvas.translate(0f, (-start).toFloat()) // Translate canvas to the appropriate position
          scrollView.getChildAt(0).draw(contentCanvas)

          // Draw the bitmap onto the canvas
          canvas?.drawBitmap(bitmap, 0f, 0f, null)

          pdfDocument?.finishPage(page)
        }

        try {
          pdfDocument?.writeTo(FileOutputStream(destination?.fileDescriptor))
        } catch (e: IOException) {
          callback?.onWriteFailed(e.toString())
          return
        } finally {
          pdfDocument?.close()
          pdfDocument = null
        }
        callback?.onWriteFinished(pages)

      }

enter image description here

android printing android-print-framework android-print-manager
1个回答
0
投票

找出原因: 当我使用从 onLayout() 的 PrintAtributes 接收的高度时,我使用的宽度是滚动视图的宽度。滚动视图宽度大于 PrintAtributes 中的宽度,因此内容被拉伸以适合给定宽度。宽度的拉伸导致每个 pdf 页面底部出现空白。

解决方案: 在将位图写入画布之前缩放页面。

© www.soinside.com 2019 - 2024. All rights reserved.