Код: Выделить всё
val pageSize = newAttributes.mediaSize?.heightMils
Это то, что я использую для установки высоты растрового изображения, но кажется, что растровое изображение короче фактического размера страницы, и поэтому внизу есть пустое пространство.
Это то, что я использую для установки высоты растрового изображения, но кажется, что растровое изображение короче фактического размера страницы. p>
Я проверил конвертацию пикселей в милы и наоборот применяется там, где надо. Поскольку я использую атрибут высоты из медиа для печати на странице 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
?, 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)
}
Код: Выделить всё

Подробнее здесь: https://stackoverflow.com/questions/786 ... dfdocument