0

我正在使用 pdfDocument 创建我的数据的 pdf,但我只能创建单页。有时我会传递 1000 个需要多页的数据,但我无法弄清楚如何在到达第一个末尾时创建下一页页..

这是我的代码:

object PDFGenerator {
    private const val TAG = "PDFGenerator"
    var pageHeight = 1120
    var pagewidth = 792

    var logoHeight = 50
    var logoWidth = 50

    fun generatePDF(context: Context,
        headerBuilder: HeaderBuilder,
        outputFileName: String = "GeneratedPDF",
        logo: Bitmap,
        tableTitle: TableTitleBuilder,
        valueList: MutableList<TableValuesBuilder>) {

        val pdfDocument = PdfDocument()
        Log.d(TAG, "generatePDF: starting")

        val paint = Paint()
        val title = Paint()

        val pageInfo = PageInfo.Builder(pagewidth, pageHeight, 100).create()
        val myPage = pdfDocument.startPage(pageInfo)

        val canvas: Canvas = myPage.canvas
        val scaledLogo = Bitmap.createScaledBitmap(logo, logoWidth, logoHeight, false);
        canvas.drawBitmap(scaledLogo, 56F, 40F, paint)

        /**
         * Configuring PDF Header
         */
        title.textSize = 26F
        title.typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD)
        title.color = ContextCompat.getColor(context, R.color.brandTitle)
        canvas.drawText(headerBuilder.title, 120F, 65F, title)
        title.textSize = 18F
        title.typeface = Typeface.create(Typeface.DEFAULT, Typeface.NORMAL)
        title.color = ContextCompat.getColor(context, R.color.brandSubTitle)
        canvas.drawText(headerBuilder.subTitle, 120F, 85F, title)

        /**
         * Configuring table titles
         */
        var xTitlePos = 50F
        val yTitlePos = 150F
        title.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
        title.color = ContextCompat.getColor(context, R.color.titleText)
        title.textSize = 25F
        canvas.drawText(tableTitle.name1, xTitlePos, yTitlePos, title)
        xTitlePos += 200F
        canvas.drawText(tableTitle.name2, xTitlePos, yTitlePos, title)
        xTitlePos += 200F
        canvas.drawText(tableTitle.name3, xTitlePos, yTitlePos, title)
        xTitlePos += 200F
        canvas.drawText(tableTitle.name4, xTitlePos, yTitlePos, title)

        /**
         * Inserting values in table
         */
        var xValuePos = 50F
        var yValuePos = 185F
        title.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
        title.color = ContextCompat.getColor(context, R.color.valueText)
        title.textSize = 25F
        valueList.forEach {
            canvas.drawText(it.value1, xValuePos, yValuePos, title)
            xValuePos += 200F
            canvas.drawText(it.value2, xValuePos, yValuePos, title)
            xValuePos += 200F
            canvas.drawText(it.value3, xValuePos, yValuePos, title)
            xValuePos += 200F
            canvas.drawText(it.value4, xValuePos, yValuePos, title)
            xValuePos = 50F
            yValuePos += 30F
        }

        pdfDocument.finishPage(myPage)
        val file = File(Environment.getExternalStorageDirectory(), "$outputFileName.pdf")
        Log.d(TAG, "generatePDF: $file")
        try {
            pdfDocument.writeTo(FileOutputStream(file))
            Toast.makeText(context, "PDF file generated successfully.", Toast.LENGTH_SHORT).show()
        } catch (e: IOException) {
            e.printStackTrace()
        }
        pdfDocument.close()
        Log.d(TAG, "generatePDF: ending")
    }
}

一旦到达当前页面的末尾,我如何生成下一页..

提前致谢。

4

0 回答 0