class MyCanvasView(context: Context) : View(context) { private val paint = Paint() override fun onDraw(canvas: Canvas) { super.onDraw(canvas) paint.color = Color.RED paint.strokeWidth = 10f canvas.drawLine(100f, 100f, 400f, 400f, paint) paint.color = Color.BLUE canvas.drawCircle(200f, 200f, 100f, paint) paint.color = Color.BLACK paint.textSize = 50f canvas.drawText("Hello Canvas", 150f, 600f, paint) } }
fun cratePdf(): File? { val pdfDocument = PdfDocument() val pageInfo = PdfDocument.PageInfo.Builder(595, 842, 1).create() // A4 size val page = pdfDocument.startPage(pageInfo) val canvas = page.canvas // Draw pdfDocument.finishPage(page) pdfDocument.close() return saveInfoFile("sample", pdfDocument) } fun saveInfoFile(titleText: String, pdfDocument: PdfDocument): File? { val storageDir = context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS) val fileName = "${titleText}_document.pdf" val pdfFile = File(storageDir, fileName) try { val outputStream = FileOutputStream(pdfFile) pdfDocument.writeTo(outputStream) pdfDocument.close() outputStream.close() } catch (e: IOException) { return null } return pdfFile }
fun createPdf( qrCode: String, titleText: String, data: ArrayList<Pair<String, String>>, ): File? { val resources = context.resources val pdfDocument = PdfDocument() val rowHeight = resources.getDimensionPixelSize(R.dimen.pdf_row_height) val normalGapSize = resources.getDimensionPixelSize(R.dimen.pdf_gap_size) val qrCodeSize = resources.getDimensionPixelSize(R.dimen.pdf_qrcode_size) val titleTextSize = resources.getDimensionPixelSize(R.dimen.text_size_normal) val contentTextSize = resources.getDimensionPixelSize(R.dimen.text_size_little) val pdfWidth = resources.getDimensionPixelSize(R.dimen.pdf_width) val pdfHeight = (qrCodeSize + normalGapSize + (normalGapSize + (rowHeight * data.size)) + normalGapSize) val pageInfo = PdfDocument.PageInfo.Builder(pdfWidth, pdfHeight, 1).create() val page = pdfDocument.startPage(pageInfo) val canvas = page.canvas var currentVerticalPosition = normalGapSize + rowHeight val qrCodeBitmap = barcodeEncoder.encodeBitmap( qrCode, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, mapOf(EncodeHintType.MARGIN to 0), ) canvas.drawBitmap( qrCodeBitmap, (canvas.width - qrCodeSize), normalGapSize, null ) val titlePaint = Paint() titlePaint.textSize = titleTextSize.toFloat() titlePaint.typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD) canvas.drawText( titleText, normalGapSize.toFloat(), 0f, titlePaint ) currentVerticalPosition += qrCodeSize val tableTitleTextPaint = Paint() tableTitleTextPaint.textSize = contentTextSize.toFloat() tableTitleTextPaint.typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD) val tableValueTextPaint = Paint() tableValueTextPaint.textSize = contentTextSize.toFloat() for (row in data) { canvas.drawText( row.first, normalGapSize.toFloat(), currentVerticalPosition.toFloat(), tableTitleTextPaint ) val textWidth = tableValueTextPaint.measureText(row.second) val secondTextStart = canvas.width - textWidth - normalGapSize canvas.drawText( row.second, secondTextStart, currentVerticalPosition.toFloat(), tableValueTextPaint ) if (data.indexOf(row) != data.lastIndex) { currentVerticalPosition += rowHeight } } pdfDocument.finishPage(page) return saveInfoFile(titleText, pdfDocument) }
val rowHeight = resources.getDimensionPixelSize(R.dimen.pdf_row_height) val normalGapSize = resources.getDimensionPixelSize(R.dimen.pdf_gap_size) val qrCodeSize = resources.getDimensionPixelSize(R.dimen.pdf_qrcode_size) val titleTextSize = resources.getDimensionPixelSize(R.dimen.text_size_little) val contentTextSize = resources.getDimensionPixelSize(R.dimen.text_size_tiny) val pdfWidth = resources.getDimensionPixelSize(R.dimen.pdf_width) val pdfHeight = (qrCodeSize + normalGapSize + (normalGapSize + (rowHeight * data.size)) + normalGapSize)
val qrCodeBitmap = barcodeEncoder.encodeBitmap( qrCodeContent, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, mapOf(EncodeHintType.MARGIN to 0), ) canvas.drawBitmap( qrCodeBitmap, (canvas.width - qrCodeSize), 0f, null, )
val titlePaint = Paint() titlePaint.textSize = titleTextSize.toFloat() titlePaint.typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD) canvas.drawText( titleText, normalGapSize.toFloat(), 0f, titlePaint, )
currentVerticalPosition += qrCodeSize val tableTitleTextPaint = Paint() tableTitleTextPaint.textSize = contentTextSize.toFloat() tableTitleTextPaint.typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD) val tableValueTextPaint = Paint() tableValueTextPaint.textSize = contentTextSize.toFloat() for (row in data) { canvas.drawText( row.first, normalGapSize.toFloat(), currentVerticalPosition.toFloat(), tableTitleTextPaint ) val textWidth = tableValueTextPaint.measureText(row.second) val secondTextStart = canvas.width - textWidth - normalGapSize canvas.drawText( row.second, secondTextStart, currentVerticalPosition.toFloat(), tableValueTextPaint ) if (data.indexOf(row) != data.lastIndex) { currentVerticalPosition += rowHeight } }