在ITEXT7中,如何插入签证持有人信息而不覆盖当前内容?

huangapple go评论72阅读模式
英文:

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

问题

我正在使用itext 7.0.0我正在编写一个PDF文档在末尾插入一个包含姓名/日期/签名的部分就像这样

然而在某些情况下最后一部分会覆盖已经存在的部分例如

这是我如何插入姓名/日期/签名矩形的代码

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
}

如何确保不覆盖已有内容
英文:

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:

在ITEXT7中,如何插入签证持有人信息而不覆盖当前内容?

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

在ITEXT7中,如何插入签证持有人信息而不覆盖当前内容?

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 ?

答案1

得分: 0

我已经选择了另一种方法来解决我的问题。

与其绘制一个矩形,我建立了一个只有一列和一个单元格的表格。

单元格的高度设置为足够大,以便通过表格绘制的矩形足够大,表格的宽度设置为足够大。

最后,我添加了一个向右的水平对齐。

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)

在ITEXT7中,如何插入签证持有人信息而不覆盖当前内容?

英文:

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)

在ITEXT7中,如何插入签证持有人信息而不覆盖当前内容?

huangapple
  • 本文由 发表于 2020年9月4日 18:30:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63739406.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定