I am creating pdf of my data using pdfDocument but I am able to create single page only.. Sometimes I am passing 1000 of data which needs multiple pages but I am unable to figure out how can I create next page once reaching the end of first page..
Here is my code :
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")
}
}
how I can generate next page once reaching the end of current page..
Thanks in Advance.