0

I'm creating a PDF, and I add an image to it.

The image is 256x256 pixels, there's nothing special about it.

I set its position by

nImg.SetAbsolutePosition(30, 30)

However, the image seems to be placed at the bottom of the page.

The PDF looks like this:

enter image description here

I expected it to be placed on the top left corner when I call SetAbsolutePosition(30, 30).

What might be going on here?

This is the full code:

       Dim nFs As System.IO.FileStream = New FileStream(sThis, FileMode.Create)

        Dim nDocument As Document = New Document(PageSize.A4, 25, 25, 25, 25)
        Dim nWriter As PdfWriter = PdfWriter.GetInstance(nDocument, nFs)
        nDocument.Open()

        Dim nCb As PdfContentByte = nWriter.DirectContent
        Dim nImg As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance("d:\myimage.png")

        nImg.SetAbsolutePosition(30, 30)
        nCb.AddImage(nImg)

        nDocument.Close()
        nWriter.Close()

        nFs.Close()
tmighty
  • 10,734
  • 21
  • 104
  • 218
  • @BrunoLowagie I forget this EVERYTIME I spend time with iTextSharp again. I expect the wrapper to work with top-left coordinate system insted of X-Y coordinate system. I'm not saying they're stupid people, but other solutions exist. MS and over companies do not use X-Y coordinate system when dealing with forms, etc. Maybe you can make your comment the answer. – tmighty Dec 20 '17 at 21:28

1 Answers1

4

First you need to know where to find the origin of a PDF page: Where is the Origin (x,y) of a PDF page? Usually, the (0, 0) coordinate coincides with the lower-left corner. There are exceptions when the /MediaBox is created in a special way, or when there's a /CropBox, but that doesn't seem to be the case.

Then you need to know that the x-axis points to the right and the y-axis points to the left:

enter image description here

This is explained in the answer to the question How should I interpret the coordinates of a rectangle in PDF?

Knowing that:

  1. The origin is in the lower-left corner,
  2. The y-axis points upwards,

it's normal that the image you are adding at position (30, 30) is close to the bottom of the page.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165