Search code examples
c#pdfitext

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


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


Solution

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

    imghead.SetAbsolutePosition(0, 0);