将PDF文件读入PdfDocument(一次打印多个PDF文件)

时间:2018-09-21 10:54:36

标签: android pdf kotlin android-print-framework

我正在寻找将多个.pdf文件合并为一个我可以打印的最佳方法。

使用PdfDocument / PrintedPdfDocument似乎是(打印的)最佳方法,但是我找不到将pdf文件读入PdfDocument的好方法,我只能找到有关在页面上绘图的示例。

对于使用PrintDocumentAdapter的打印Im:

class PrintPdfAdapter(private val context: Context, private val paths: List<String>) : PrintDocumentAdapter() {
override fun onLayout(oldAttributes: PrintAttributes, newAttributes: PrintAttributes, cancellationSignal: CancellationSignal?, callback: PrintDocumentAdapter.LayoutResultCallback, bundle: Bundle?) {
    pdfDocument = PrintedPdfDocument(context, newAttributes)

    if (cancellationSignal?.isCanceled == true) {
        callback.onLayoutCancelled()
    } else {
        val builder = PrintDocumentInfo.Builder("print_pdfs")
        builder.setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
                .setPageCount(PrintDocumentInfo.PAGE_COUNT_UNKNOWN)
                .build()
        callback.onLayoutFinished(builder.build(), newAttributes != oldAttributes)
    }
}

override fun onWrite(pageRanges: Array<PageRange>, destination: ParcelFileDescriptor, cancellationSignal: CancellationSignal?, callback: PrintDocumentAdapter.WriteResultCallback) {
    mergePdfFiles()
    val out = FileOutputStream(destination.fileDescriptor)
    FileInputStream(File(paths[0])).copyTo(out)
    out.close()
    callback.onWriteFinished(arrayOf(PageRange.ALL_PAGES))
}
}

对于单个PDF来说效果很好。

0 个答案:

没有答案