英文:
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:
However, in some cases, the last section is overwriting an already existing section, for example:
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)
英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论