Unable to add image in footer section of PDF using itextsharp library

27 Views Asked by At

Using library iTextSharp 5.5.13.3 version in my dotnet Widnows application 4.7.2, not able to insert an image inside footer in my pdf document, please help. some code i have tried from internet it doesn't display footer image at all, however i am able to insert image in header and it displays fine.

Please find below code i used for header image

 protected internal class MyEventHandler : PdfPageEventHelper
 {
     protected Image img;
     protected string HeaderCenter;
     protected string HeaderRight;
     protected string FooterLeft;
     protected string FooterRight;

     public override void OnEndPage(PdfWriter writer, Document document)
     {
         var contentStream = writer.DirectContent;
         PdfDocument pdfDoc = contentStream.PdfDocument;
         Rectangle pageSize = document.PageSize;

         float LeftX = 10;
         float CenterX = pageSize.Width / 2;
         float RigthX = pageSize.Width - 10;

         float HeaderY = pageSize.Height - 30;
         float FooterY = 10;

         string baseDir = AppDomain.CurrentDomain.BaseDirectory;

         string fullpath = Path.Combine(baseDir, "world-image.jpg");

         img = Image.GetInstance(fullpath);

         if (img != null)
         {
             img.SetAbsolutePosition(0, 0);
             img.ScaleAbsolute(840, 40);

             var headerImageTemplate = contentStream.CreateTemplate(img.Width, img.Height);
             headerImageTemplate.AddImage(img);
             
             contentStream.AddTemplate(headerImageTemplate, 0, HeaderY - 20);
         }
        
         if (!string.IsNullOrEmpty(HeaderCenter))
         {
             var p = new Phrase(HeaderCenter, new Font(_enclosing._baseFontRegular, FONT_SIZE));
             ColumnText.ShowTextAligned(contentStream, Element.ALIGN_CENTER, p, CenterX, HeaderY, 0, PdfWriter.RUN_DIRECTION_RTL, ColumnText.AR_LIG);
         }

         if (!string.IsNullOrEmpty(HeaderRight))
         {
             contentStream.BeginText();
             contentStream.SetFontAndSize(_enclosing._baseFontRegular, FONT_SIZE);
             contentStream.ShowTextAligned(Element.ALIGN_RIGHT, HeaderRight, RigthX, HeaderY, 0);
             contentStream.EndText();
         }

         if (!string.IsNullOrEmpty(FooterLeft))
         {
             var p = new Phrase(FooterLeft, new Font(_enclosing._baseFontRegular, FONT_SIZE));
             ColumnText.ShowTextAligned(contentStream, Element.ALIGN_LEFT, p, LeftX, FooterY, 0, PdfWriter.RUN_DIRECTION_RTL, ColumnText.AR_LIG);

         }

         if (!string.IsNullOrEmpty(FooterRight))
         {
             var rect = new Rectangle(pageSize.Right - 240, 0, pageSize.Right - 5, 60);
             ColumnText ct = new ColumnText(contentStream);
             ct.SetSimpleColumn(rect);
             ct.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
             ct.Alignment = Element.ALIGN_RIGHT;
             ct.AddElement(new Paragraph(FooterRight));
             ct.Go();
         }

     }

     internal MyEventHandler(ITextPDFSharpHelper _enclosing)
     {
         if (!string.IsNullOrEmpty(_enclosing._pageHeader.left))
         {

             var imageStr = _enclosing._pageHeader.left;
             img = Image.GetInstance(imageStr);

         }
         HeaderCenter = _enclosing._pageHeader.center;
         HeaderRight = _enclosing._pageHeader.right;
         FooterLeft = _enclosing._pageFooter.left;
         FooterRight = _enclosing._pageFooter.right;

         this._enclosing = _enclosing;

     }
     private readonly ITextPDFSharpHelper _enclosing;
 }

i am using MyEventHandler class to insert image in header, same way i tried for footer, but it is not displaying

Please help..

0

There are 0 best solutions below