0

I am using iTextg to split an image to span multiple pages, but I am not able to set Top Margin for each page. I tried setting margins in Document() constructor, I also tried setMargins()method before I use the document.open() method. Also adding empty lines or paragraph is not working. I think I am doing something wrong here.

 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))
      }

}

enter image description here

SuperHaker
  • 178
  • 1
  • 15
  • 1
    When you set the margins on a `Document`, iText assumes that you are going to let iText define the layout (high-level approach). That's not what you're doing. You add the image to a `PdfContentByte` instance (low-level approach). When you decide to add content at the low level, the margins you defined on the high level are ignored (for reasons that seem obvious to me). – Bruno Lowagie Feb 04 '18 at 12:40
  • @BrunoLowagie Makes sense – SuperHaker Feb 08 '18 at 21:26

0 Answers0