'Rect not being drawn on Android PdfDocument canvas

I just switched from using iText to the native android PdfDocument, due to lack of documentation of how to print iText documents.

I am trying to draw a table on the Canvas, by looping through a list and adding a right and left column for each item, represented by using the Rect

The function responsible for drawing the table rows is shown below:

fun drawTableRow(canvas: Canvas, question: Document.H) {
            val textBounds = Rect()

            var cellHeight = if(question.value.isNullOrEmpty()) {
                20f
            } else {
                paint.getTextBounds(question.value, 0, question.value!!.length, textBounds)
                textBounds.height().toFloat()
            }
            if(cellHeight <= 20f) cellHeight = 20f

            paint.textAlign = Paint.Align.LEFT

            val top = yPointer
            val right = (width.toFloat() - (pageMargin * 2)) / 3
            val bottom = height - (yPointer + cellHeight + pageMargin)

            val leftCell = RectF(pageMargin, top, right, bottom)
            val rightCell = RectF(leftCell.right, top, pageMargin, bottom)

            var textY = yPointer + yPadding

            canvas.drawRect(rightCell, contentBgPaint)
            canvas.drawRect(leftCell, contentBgPaint)

            canvas.drawText(question.h.hQuestion.title, 0, question.h.hQuestion.title.length, pageMargin, textY, paint)
            canvas.drawText(question.h.hQuestion.subtitle, 0, question.h.hQuestion.subtitle.length, pageMargin, textY + 16f, paint)
            canvas.drawText(question.value!!, 0, question.value!!.length, leftCell.right, textY, paint)

            yPointer += cellHeight.toInt()
        }

And when running the debugger, the Rect coordinates seem to be set correctly.

Debugger

However, an usual behavior occurs - either only one rectangle is being drawn, or the rectangles are overlapping each other exactly, despite having different coordinates.

Because if I attempt to only draw the right rectangle, it still appears on the left side in the document, with the exact proportions of the left rectangle, as seen in the image below.

Pdf screenshot



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source