Chevy Mark Sunderland

Chevy Mark Sunderland

  • NA
  • 188
  • 167.3k

Add logo image for each page on pdf file by iTextSharp

Aug 11 2016 8:10 AM
Hi there, hope in your help.
 
I have been trying to add an image to all pages using iTextSharp.
 
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.
  1. private void CreatePdf()  
  2.     {  
  3.         Response.ContentType = "application/pdf";  
  4.         Response.AddHeader("content-disposition""attachment;filename="test.pdf");  
  5.         Response.Cache.SetCacheability(HttpCacheability.NoCache);  
  6.   
  7.         StringWriter sw = new StringWriter();  
  8.         HtmlTextWriter hw = new HtmlTextWriter(sw);  
  9.   
  10.         pnPrint.RenderControl(hw);  
  11.   
  12.         StringReader sr = new StringReader(sw.ToString());  
  13.         string imagepath = Server.MapPath(".") + "\\Img\\logo.jpg";  
  14.   
  15.         Document pdfDoc = new Document(PageSize.A4, 20f, 20f, 10f, 20f);  
  16.   
  17.         try  
  18.         {  
  19.             HTMLWorker htmlparser = new HTMLWorker(pdfDoc);  
  20.             PdfWriter.GetInstance(pdfDoc, Response.OutputStream);  
  21.             pdfDoc.Open();  
  22.   
  23.             for (int i = 0; i < 5; i++)  
  24.             {  
  25.                 Image image = Image.GetInstance(imagepath);  
  26.                 image.Alignment = Image.ALIGN_LEFT;  
  27.                 pdfDoc.Add(image);  
  28.                 htmlparser.Parse(sr);  
  29.             }  
  30.         }  
  31.         catch (Exception ex)  
  32.         {  
  33.             throw ex;  
  34.         }  
  35.         finally  
  36.         {  
  37.             pdfDoc.Close();  
  38.             Response.End();  
  39.         }  
  40.     }  
 

Answers (4)