我不断收到错误消息
当我尝试使用 columnText 写入 PDF 时。setSimpleColumn()
以前工作正常,但现在这段代码抛出错误,当我尝试使用它时setColumns()
它也抛出错误。我想不出是什么导致了错误。我应该以某种方式关闭 columnText 吗?
直接相关的代码是:
fun testBox(content: List, font: List, page: Int, leftLimit: Float, rightLimit: Float, topLimit: Float, bottomLimit: Float){
val columnText = ColumnText(setCanvas("$filepath$name.pdf",page))
columnText.alignment = ALIGN_JUSTIFIED
columnText.runDirection = RUN_DIRECTION_RTL
columnText.setSimpleColumn(leftLimit, bottomLimit, rightLimit, getRectangle("LETTER").top-topLimit)
var i = 0
while (i < content.size) {
columnText.addText(Chunk(content[i], font[i]))
columnText.go()
i++
}
}
如果需要,我会显示更多代码,但我认为其余部分与问题无关。
我真的很难过,我找不到关于这个问题的太多信息。
这是生成的文件: https ://drive.google.com/file/d/135EhLyiyDj6iAexUJ0upRdG6eXHYXVdw/view?usp=sharing
编辑:我忘记了我创建函数setCanvas
是为了保存一堆代码,这里是函数:
fun setCanvas(file: String, page: Int): PdfContentByte? {
val reader = PdfReader(file)
val stamper = PdfStamper(reader, FileOutputStream(File(file)))
if(reader.numberOfPages < page){ stamper.insertPage(reader.numberOfPages + (page-reader.numberOfPages), reader.getPageSize(1) ?: getRectangle("LETTER")); if (page - reader.numberOfPages != 1){throw Error("EMPTY PAGE!")}}
return stamper.getOverContent(page)
}
编辑2:[结合两个功能]
testBox(listOf(content1,content2,content3), listOf(font,fontBold,font), 1, document.left(), document.right(), document.bottom(),document.top())
fun testBox(content: List<String>, font: List<Font>, page: Int, leftLimit: Float, rightLimit: Float, topLimit: Float, bottomLimit: Float){
val reader = PdfReader("$filepath$name.pdf")
val stamper = PdfStamper(reader, FileOutputStream(File("$filepath$name - edit.pdf")))
if(reader.numberOfPages < page){ stamper.insertPage(reader.numberOfPages + (page-reader.numberOfPages), reader.getPageSize(1) ?: getRectangle("LETTER")); if (page - reader.numberOfPages > 0){throw Error("EMPTY PAGE!")}}
val columnText = ColumnText(stamper.getOverContent(page))
columnText.alignment = ALIGN_JUSTIFIED
columnText.runDirection = RUN_DIRECTION_RTL
columnText.setSimpleColumn(leftLimit, bottomLimit, getRectangle("LETTER").right - rightLimit, getRectangle("LETTER").top- topLimit)
var i = 0
while (i<content.size) {
columnText.addText(Chunk(content[i], font[i]))
columnText.go()
i++
}
}
链接: https ://drive.google.com/file/d/1ybvDVSxKOJdbnA2fSRjEmxlDszIktWTL/view?usp=sharing