Search code examples
javakotlinitext7

In ITEXT7, how to insert a visa holder without overwriting current content?


I am using itext 7.0.0. I am writing a PDF and at the end, I insert a section that will contain Name/date/signature, like so:

enter image description here

However, in some cases, the last section is overwriting an already existing section, for example:

enter image description here

Here is how I insert the name/date/signature rectangle:

private fun placeVisa(document: Document): Document {
        val pdfCanvas = PdfCanvas(document.pdfDocument.getPage(document.pdfDocument.numberOfPages))
        val width = 200f
        val height = 100f
        val rectangle = Rectangle((document.pdfDocument.defaultPageSize.width - width) * 0.95f, // x position
                height / 2, // y position
                width, // actual width
                height) // actual height
        pdfCanvas.rectangle(rectangle)
        pdfCanvas.stroke()
        val canvas = Canvas(pdfCanvas, document.pdfDocument, rectangle)
        val rectangleContent = Paragraph(Text("Name, date and signature :"))
        rectangleContent.marginLeft = 5f
        canvas.add(rectangleContent)
        return document

How can I make it so I does not overwrite content if there is some ?


Solution

  • I have chosen another approach to my problem.

    Instead of drawing a rectangle, I build a table with one column and one cell.

    THe height of the cell is set so that the rectanblge drawn by the table is large enough, and the witdh of the table is set so that it is large enough.

    Finally, I added an horizontal alignement to the right.

    val table = Table(1)
    val cell = Cell(1,1)
    cell.add("Name, date and signature :")
    cell.height = 75f
    table.setWidth(200f)
    table.addCell(cell)
    table.setHorizontalAlignment(HorizontalAlignment.RIGHT)
    document.add(table)
    

    enter image description here