locked
Add logo image for each page on pdf file created by iTextSharp in c# RRS feed

  • Question

  • User1872364149 posted

    Hi there, hope in your help.

    I have been trying to add an image to all pages using iTextSharp Library.

    The below code correctly it inserted all information from asp Panel "on Print" but only inserts an image in the first page.

    Is there any way to insert the image in the same way in all pages?

    Can you help me ?

    Thank you in advance.

    private void CreatePdf()
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename="test.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
    
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
    
            pnPrint.RenderControl(hw);
    
            StringReader sr = new StringReader(sw.ToString());
            string imagepath = Server.MapPath(".") + "\\Img\\logo.jpg";
    
            Document pdfDoc = new Document(PageSize.A4, 20f, 20f, 10f, 20f);
    
            try
            {
                HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                pdfDoc.Open();
    
                for (int i = 0; i < 5; i++)
                {
                    Image image = Image.GetInstance(imagepath);
                    image.Alignment = Image.ALIGN_LEFT;
                    pdfDoc.Add(image);
                    htmlparser.Parse(sr);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                pdfDoc.Close();
                Response.End();
            }
        }
    
    
    

    Thursday, August 11, 2016 12:06 PM

Answers

  • User2053451246 posted

    Do you get any specific error?  And this is definitely a typo:

    (@"C:\\intepub\\wwwroot\\img\\Logo.jpg")

    Should be inetpub.  And you should access files like this:

    Server.MapPath("~/img/logo.jpg")

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 11, 2016 2:10 PM

All replies

  • User2053451246 posted

    You need to look into the PdfPageEventHelper of iTextSharp.  It provides a method called OnEndPage that allows such things.  Basically, treat the image as being in the header or footer of the document, and use absolute positioning to put it where you want, instead of just in the flow of the page content.

    http://stackoverflow.com/questions/2321526/pdfptable-as-a-header-in-itextsharp/2326193#2326193

    Thursday, August 11, 2016 1:17 PM
  • User1872364149 posted

    Thank you for help, but in my case don't worked : the file pdf can't opened.

    My new code :

        class _events : PdfPageEventHelper
        {
            public override void OnEndPage(PdfWriter writer, Document document)
            {
                PdfPTable table = new PdfPTable(1);
    
                table.TotalWidth = document.PageSize.Width - document.LeftMargin - document.RightMargin;
                PdfPTable table2 = new PdfPTable(2);
    
                PdfPCell cell2 = new PdfPCell(Image.GetInstance(@"C:\\intepub\\wwwroot\\img\\Logo.jpg"));
                cell2.Colspan = 2;
                table2.AddCell(cell2);
    
                PdfPCell cell = new PdfPCell(table2);
                table.AddCell(cell);
    
                table.WriteSelectedRows(0, -1, document.LeftMargin, document.PageSize.Height - 36, writer.DirectContent);
            }
        }
    
    
        private void CreatePdfFiles1()
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=test.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
    
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            pnPrint.RenderControl(hw);
    
            StringReader sr = new StringReader(sw.ToString());
            string imagepath = Server.MapPath(".") + "\\Img\\Logo.jpg";
            Document pdfDoc = new Document(PageSize.A4, 20f, 20f, 10f, 20f);
    
            try
            {
    
                _events e = new _events();
                PdfWriter pw = PdfWriter.GetInstance(pdfDoc, new FileStream("test.pdf", FileMode.Create));
                pw.PageEvent = e;
    
                HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                pdfDoc.Open();
    
                for (int i = 0; i < 5; i++)
                {
                    Image image = Image.GetInstance(imagepath);
                    image.Alignment = Image.ALIGN_LEFT;
                    pdfDoc.Add(image);
                    htmlparser.Parse(sr);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                pdfDoc.Close();
                Response.End();
            }
        }

    Thursday, August 11, 2016 1:35 PM
  • User2053451246 posted

    Do you get any specific error?  And this is definitely a typo:

    (@"C:\\intepub\\wwwroot\\img\\Logo.jpg")

    Should be inetpub.  And you should access files like this:

    Server.MapPath("~/img/logo.jpg")

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 11, 2016 2:10 PM
  • User1872364149 posted

    Do you get any specific error?  And this is definitely a typo:

    (@"C:\\intepub\\wwwroot\\img\\Logo.jpg")

    Should be inetpub.  And you should access files like this:

    Server.MapPath("~/img/logo.jpg")

    Hi, I don't have error only pdf file can't opened because file not supported.

    I have tried modified code without success because the output not change : only inserts an image in the first page:

            string imagepath = @"\Img\logo.jpg";
            Document pdfDoc = new Document(PageSize.A4, 20f, 20f, 10f, 20f);
    
            try
            {
                _events e = new _events();
                PdfWriter pw = PdfWriter.GetInstance(pdfDoc, new FileStream("test.pdf", FileMode.Create));
                pw.PageEvent = e;
    
                HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                pdfDoc.Open();
                for (int i = 0; i < 5; i++)
                {
                    Image image = Image.GetInstance(Server.MapPath(imagepath));
                    image.Alignment = Image.ALIGN_LEFT;
                    pdfDoc.Add(image);
                    htmlparser.Parse(sr);
                }
            }

     

    Friday, August 12, 2016 6:59 AM