Home > Mobile >  How can I place an image to the top of PDF using iTextSharp in C#?
How can I place an image to the top of PDF using iTextSharp in C#?

Time:03-14

I have a very simple code that places the png image into PDF

    Document pdfDoc = new Document(PageSize.A4, 25, 25, 25, 10);
    string pathfile = ConfigurationManager.AppSettings["Temp_SaveLocation"];
    string fileName = "SomeName.pdf";
    path = pathfile   fileName;
    PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, new FileStream(path, FileMode.Create));
    pdfDoc.Open();

    Image imghead = Image.GetInstance(templateFolder   "Letterhead.png");

    imghead.SetAbsolutePosition(0, 0);
    pdfDoc.Add(imghead);
    pdfWriter.CloseStream = true;
    pdfDoc.Close();

However, no matter what position for the image I set, that image ends up on a very bottom of a document. I even tried negative values for the absolute position. Nevertheless, the image stays on the very bottom of a document. How can I bring an image to a very top?

Thank you very much in advance

CodePudding user response:

I found out that image will be to the top when this code is removed.

imghead.SetAbsolutePosition(0, 0);

CodePudding user response:

I agree with K J. By setting imghead.SetAbsolutePosition(0, 0), you are setting your image to be at (X,Y) co-ordinate which is mostly lower left co-ordinate of the page.

Generally, lower-left corner of the page coincides with the origin of the coordinate system (0, 0). The upper-right corner of the page coincides with the coordinate (595, 842). You either remove or comment this method imghead.SetAbsolutePosition(0, 0) or adjust the Y co-ordinate of it to move the image towards the upper side of the page.

  • Related