Setting top margin iText Android

169 Views Asked by At

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

0

There are 0 best solutions below