我使用iTextg将图片拆分为多个页面,但我无法为每个页面设置上边距。我尝试在Document()构造函数中设置边距,在使用document.open()方法之前我也尝试过setMargins()方法。添加空行或段也无效。我想我在这里做错了什么。
private fun addImage(pdfWriter: PdfWriter, document: Document, byteArray: ByteArray) {
var image: Image? = null
try {
image = Image.getInstance(byteArray)
} catch (e: Exception) {
e.printStackTrace()
}
image!!.scaleToFit(PageSize.A4)
// image.setAbsolutePosition(0f, 0f)
val content = pdfWriter.directContent
val width = PageSize.A4.width.toDouble()
val heightRatio = image.height * width / image.width
var nPages: Int = (heightRatio / PageSize.A4.height).toInt()
val difference = heightRatio % PageSize.A4.height
while (nPages >= 0) {
document.newPage()
// document.add(Paragraph("Empty space\n\n\n"))
document.add(Chunk.NEWLINE)
content.addImage(image, width, 0.0, 0.0, heightRatio, 0.0,
-((--nPages * PageSize.A4.height) + difference - 20f))
}
}