Sunday, 15 March 2015

Setting Header and Footer as Image while creating PDF using iTextSharp


I have requirement in SharePoint 2013 project to create the PDF dynamically from the HTML having header and footer on every page. I have used open source iTextSharp dll to achieve the same, There are many samples available to add image to pdf using iTextSharp, however there is one specific scenario where you are creating the PDF by specifying the HTML and that too it should have header and footer on each page. Normally header and footer will be only appear once in case of HTML.

Following few tips you should consider while achieving this functionality:

1. You have specified the margin Top and margin Bottom while creating the document object of iTextSharp.

Document doc = new Document(PageSize.A4, 10, 10, 150, 150)

here the last two parameters are top and bottom margin.

2. You need to implement the event handler, In which you will override the OnEndPage event of the class by deriving it from PdfPageEventHelper class.

public override void OnEndPage(PdfWriter writer, iTextSharp.text.Document document)

3. You need add the image in header and footer using OnEndPage event only.

               PdfPTable footerTbl = new PdfPTable(1);

                footerTbl.TotalWidth = document.PageSize.Width;

                Image logo = Image.GetInstance(new Uri(string.Format("{0}/SiteCollectionImages/Footer.JPG", SiteCollectionUrl)));

                logo.SetAbsolutePosition(0, 0);

                PdfPCell cell = new PdfPCell(logo);

                cell.HorizontalAlignment = Element.ALIGN_RIGHT;

                cell.PaddingRight = 20;

                cell.Border = 0;

                footerTbl.AddCell(cell);

                footerTbl.WriteSelectedRows(0, -1, 0, 90, writer.DirectContent);

                PdfPTable headerTbl = new PdfPTable(1);

                headerTbl.TotalWidth = document.PageSize.Width;

                Image logo1 = Image.GetInstance(new Uri(string.Format("{0}/SiteCollectionImages/header.jpg", SiteCollectionUrl)));

                logo1.ScalePercent(90);

                logo1.SetAbsolutePosition(0, 0);

                PdfPCell cell1 = new PdfPCell(logo1);

                cell1.HorizontalAlignment = Element.ALIGN_RIGHT;

                cell1.Border = 0;

                headerTbl.AddCell(cell1);

                headerTbl.WriteSelectedRows(0, -1, 0, (document.PageSize.Height - 10), writer.DirectContent);

4. Specify the event handler in PdfWriter.

Migration issues for SAP.Connector.dll from Win 2003 to Win 2019 server

Recently I got task of migration of asmx web services from windows 2003 to windows 2019 server. These web services fetch data from SAP us...