0

我正在使用 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))
      }

}

在此处输入图像描述

4

0 回答 0