Câu hỏi:

private fun createPdfFile(): File? { val pdfDocument = PdfDocument() val pageInfo = PdfDocument.PageInfo.Builder(595, 842, 1).create() val page = pdfDocument.startPage(pageInfo)

    val canvas = page.canvas
    val paint = Paint()
    paint.textSize = 16f
    var yPosition = 25

    seminarList.forEach { seminar ->
        canvas.drawText("Subject: ${seminar.subject}", 10f, yPosition.toFloat(), paint)
        yPosition += 20
        canvas.drawText("Date: ${seminar.date}", 10f, yPosition.toFloat(), paint)
        yPosition += 20
        canvas.drawText("Time: ${seminar.time}", 10f, yPosition.toFloat(), paint)
        yPosition += 20
        canvas.drawText("Room: ${seminar.room}", 10f, yPosition.toFloat(), paint)
        yPosition += 40
    }

    pdfDocument.finishPage(page)

    val directoryPath = requireContext().cacheDir.toString()
    val file = File(directoryPath, "SeminarsPreview.pdf")

    return try {
        pdfDocument.writeTo(FileOutputStream(file))
        pdfDocument.close()
        file
    } catch (e: Exception) {
        e.printStackTrace()
        pdfDocument.close()
        null
    }
}

Can you make the pdf more professional and beautiful?


Ah, creating PDFs! It’s like cooking a gourmet dish, but with pixels and vectors instead of spices and herbs. Let’s spice up your PDF to make it more visually appealing, like it’s strutting down a digital runway!

Here’s an enhanced version of your code:

private fun createPdfFile(): File? {
    val pdfDocument = PdfDocument()
    val pageInfo = PdfDocument.PageInfo.Builder(595, 842, 1).create()
    val page = pdfDocument.startPage(pageInfo)

    val canvas = page.canvas
    val paint = Paint()
    val titlePaint = Paint()
    paint.textSize = 12f
    paint.color = Color.BLACK 
    titlePaint.textSize = 20f
    titlePaint.typeface = Typeface.create(Typeface.DEFAULT_BOLD, Typeface.BOLD)
    titlePaint.color = Color.BLUE 

    val marginLeft = 50f
    var yPosition = 60

    canvas.drawText("Seminars Preview", marginLeft, yPosition.toFloat(), titlePaint)
    yPosition += 40

    seminarList.forEach { seminar ->
        paint.typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD)
        canvas.drawText("Subject: ${seminar.subject}", marginLeft, yPosition.toFloat(), paint)
        yPosition += 20

        paint.typeface = Typeface.create(Typeface.DEFAULT, Typeface.NORMAL)
        canvas.drawText("Date: ${seminar.date}", marginLeft, yPosition.toFloat(), paint)
        yPosition += 20

        canvas.drawText("Time: ${seminar.time}", marginLeft, yPosition.toFloat(), paint)
        yPosition += 20

        canvas.drawText("Room: ${seminar.room}", marginLeft, yPosition.toFloat(), paint)
        yPosition += 30

        // Adding a separator line for better readability
        val linePaint = Paint()
        linePaint.color = Color.GRAY
        linePaint.strokeWidth = 1f
        canvas.drawLine(marginLeft, yPosition.toFloat(), pageInfo.pageWidth - marginLeft, yPosition.toFloat(), linePaint)
        yPosition += 20
    }

    pdfDocument.finishPage(page)

    val directoryPath = requireContext().cacheDir.toString()
    val file = File(directoryPath, "SeminarsPreview.pdf")

    return try {
        pdfDocument.writeTo(FileOutputStream(file))
        pdfDocument.close()
        file
    } catch (e: Exception) {
        e.printStackTrace()
        pdfDocument.close()
        null
    }
}

Improvements:

  1. Title Addition: A fancy title makes a great first impression. Added a "Seminars Preview" title.
  2. Font Size and Color Adjustment: Differentiating titles and info with varied font sizes and a splash of color (blue for the title).
  3. Bold Subject: Using bold for the subject to add emphasis.
  4. Separator Lines: Including separator lines for better readability and to avoid the dreaded "Wall of Text" effect.

Feel like learning more tricks of the trade? Why not swing by trituenhantao.io for more tips and tricks to dazzle your PDFs? Trust me; your code will thank you!


Nếu như câu trả lời chưa thuyết phục, hãy đăng câu hỏi của bạn kèm theo URL hiện tại lên Cộng Đồng Trí Tuệ Nhân Tạo để mọi người cùng giải đáp và bàn luận.
Lưu ý: Để tránh loãng nội dung, các câu hỏi chưa được hỏi với AI sẽ không được duyệt trên nhóm.
Trang chủ | Hỏi tiếp | Xem lịch sử