Avoid overlapping of content and footer while generating PDF using C#

Last Reply one year ago By kalpesh

Posted one year ago

[Table]: i am adding columns to the table by using add more button.

In the footer i have:

[signature] Finance Director

As i am adding more rows to the table, content area gets overlaps to the footer of PDF.How to avoid it?

   private void GenearatePDf(string strheader, string strFooter, string strContact, string strExihibition, string strPaymentDetails)
   {
       try
       {
           StringBuilder sbHtmlText = new StringBuilder();
           string hdr1 = Server.MapPath(".") + "\\images\\header.jpg";
           string footer1 = Server.MapPath(".") + "\\images\\footerr.jpg";
           string footer2 = Server.MapPath(".") + "\\images\\footer_nosignn.jpg";
           iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(Server.MapPath(".") + "\\images\\md.jpg");
           iTextSharp.text.Image iHeader = iTextSharp.text.Image.GetInstance(hdr1);
 
           iTextSharp.text.Image jpg1;
           if (chkIncludeSignature.Checked)
           {
               jpg1 = iTextSharp.text.Image.GetInstance(footer1);
           }
           else
           {
               jpg1 = iTextSharp.text.Image.GetInstance(footer2);
           }
 
           isSingle = ISSingleEvent();
           if (isSingle)
           {
               f0 = f1;
           }
 
           jpg.ScaleToFit(360, 360);
           jpg1.ScaleToFit(830, 160);
           iHeader.ScaleToFit(900, 80);
 
           jpg.Alignment = iTextSharp.text.Image.UNDERLYING;
           iHeader.Alignment = iTextSharp.text.Image.UNDERLYING;
           iHeader.Alignment = Element.ALIGN_CENTER;
 
           jpg.SetAbsolutePosition(95, 315);
           jpg1.SetAbsolutePosition(45, 2);
           iHeader.SetAbsolutePosition(42, 750);
 
           Chunk chunk1 = new Chunk("\n");
           Phrase ph1 = new Phrase(chunk1);
           PdfPTable tblBlank = new PdfPTable(1);
           tblBlank.TotalWidth = 900f;
           tblBlank.WidthPercentage = 100;
           tblBlank.SetTotalWidth(new float[] { 700f });
           PdfPCell cellBlank = new PdfPCell();
 
           cellBlank.BorderWidth = 0;
           cellBlank.AddElement(ph1);
           tblBlank.Rows.Add(new PdfPRow(new PdfPCell[] { cellBlank }));
           tblBlank.SpacingBefore = 24f;
           if (isSingle)
           {
               tblBlank.SpacingBefore = 28f;
           }
 
           //***************** INVOICE NUMBER ,DATE,TO,ADDITIONAL INFO ****************
           PdfPCell cellHdrNo = new PdfPCell();
           PdfPCell cellClmnNo = new PdfPCell();
           PdfPCell cellValNo = new PdfPCell();
 
           cellHdrNo.PaddingLeft = 5f;
           cellHdrNo.BorderWidth = 0;
           cellClmnNo.BorderWidth = 0;
           cellValNo.BorderWidth = 0f;
          // cellHdrNo.PaddingTop = -2f;
 
           cellHdrNo.AddElement(new Phrase("No", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.BOLD)));
           cellClmnNo.AddElement(new Phrase(":", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.NORMAL)));
           cellValNo.AddElement(new Phrase(txtInvoiceNumber.Text.ToUpper(), new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.NORMAL)));
 
           PdfPTable tblAddInfo = new PdfPTable(3);
           tblAddInfo.TotalWidth = 420f;
           tblAddInfo.WidthPercentage = 100;
           tblAddInfo.SetTotalWidth(new float[] { 24f, 5f, 300f });
           tblAddInfo.Rows.Add(new PdfPRow(new PdfPCell[] { cellHdrNo, cellClmnNo, cellValNo }));
           //Date
           PdfPCell cellHdrDate = new PdfPCell();
           PdfPCell cellClmnDate = new PdfPCell();
           PdfPCell cellValDate = new PdfPCell();
 
           cellHdrDate.PaddingLeft = 5f;
           cellClmnDate.BorderWidth = 0;
           cellHdrDate.BorderWidth = 0;
           cellValDate.BorderWidth = 0f;
           //cellHdrDate.PaddingTop = -2f;
           //cellClmnDate.FixedHeight = 18f;
           //cellHdrDate.FixedHeight = 18f;
           //cellValDate.FixedHeight = 18f;
 
           cellHdrDate.AddElement(new Phrase("Date", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.BOLD)));
           cellClmnDate.AddElement(new Phrase(":", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.NORMAL)));
           // cellValDate.AddElement(new Phrase(txtDate.Text.Trim().ToUpper(), new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.NORMAL)));
           DateTime dt = DateTime.ParseExact(txtDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);//added by chetan
           string formateDate = dt.ToString("MMMM dd, yyyy");//added by chetan
           cellValDate.AddElement(new Phrase(formateDate, new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.NORMAL)));//added by chetan
 
           tblAddInfo.TotalWidth = 420f;
           tblAddInfo.WidthPercentage = 100;
           tblAddInfo.SetTotalWidth(new float[] { 24f, 5f, 300f });
           tblAddInfo.Rows.Add(new PdfPRow(new PdfPCell[] { cellHdrDate, cellClmnDate, cellValDate }));
 
           //To *******************************
           PdfPCell cellHdrTo = new PdfPCell();
           PdfPCell cellClmnTo = new PdfPCell();
           PdfPCell cellValTo = new PdfPCell();
 
           cellHdrTo.PaddingLeft = 5f;
           cellClmnTo.BorderWidth = 0;
           cellHdrTo.BorderWidth = 0;
           cellValTo.BorderWidth = 0f;
           //cellValTo.PaddingTop = -2f;
           //cellClmnTo.PaddingTop = -2f;
 
           cellHdrTo.AddElement(new Phrase("To", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.BOLD)));
           cellClmnTo.AddElement(new Phrase(":", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.NORMAL)));
           cellValTo.AddElement(new Phrase("M/S " + txtCompanyName.Text.Trim().ToUpper(), new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.NORMAL)));
 
           tblAddInfo.TotalWidth = 420f;
           tblAddInfo.WidthPercentage = 100;
           tblAddInfo.SetTotalWidth(new float[] { 24f, 5f, 300f });
           tblAddInfo.Rows.Add(new PdfPRow(new PdfPCell[] { cellHdrTo, cellClmnTo, cellValTo }));
 
           //EOC TO**************************************
 
           //****** ATTN  *******************************
           PdfPCell cellHdrAttn = new PdfPCell();
           PdfPCell cellClmnAttn = new PdfPCell();
           PdfPCell cellValAttn = new PdfPCell();
 
           cellHdrAttn.PaddingLeft = 5f;
           cellClmnAttn.BorderWidth = 0;
           cellHdrAttn.BorderWidth = 0;
           cellValAttn.BorderWidth = 0f;
 
           cellHdrAttn.AddElement(new Phrase("ATTN", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.BOLD)));
           cellClmnAttn.AddElement(new Phrase(":", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.NORMAL)));
           cellValAttn.AddElement(new Phrase(txtContactPerson.Text.Trim().ToUpper(), new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.NORMAL)));
 
           tblAddInfo.TotalWidth = 420f;
           tblAddInfo.WidthPercentage = 100;
           tblAddInfo.SetTotalWidth(new float[] { 24f, 5f, 300f });
           tblAddInfo.Rows.Add(new PdfPRow(new PdfPCell[] { cellHdrAttn, cellClmnAttn, cellValAttn }));
 
           //EOC ATTN **************************************
 
           // BOC Contact***********************************
 
           PdfPTable tblCon = new PdfPTable(1);
 
           PdfPCell cellContact = new PdfPCell();
           cellContact.PaddingTop = 3f;
           cellContact.BorderWidth = 0;
 
 
           iTextSharp.text.html.simpleparser.StyleSheet CSS = new iTextSharp.text.html.simpleparser.StyleSheet();
           var htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(strContact), CSS);
           for (int k1 = 0; k1 < htmlarraylist.Count; k1++)
           {
               var ele = (IElement)htmlarraylist[k1];
               cellContact.AddElement(ele);
           }
           tblCon.WidthPercentage = 100;
           tblCon.HorizontalAlignment = 1;
 
           tblCon.Rows.Add(new PdfPRow(new PdfPCell[] { cellContact }));
 
 
 
 
           ////******************** Exibition header   ****************                
           PdfPTable tblExhibition = GetPDFExihibition();
           //**********
 
 
           //******************* Notes ******************       
           PdfPTable tblNotes = new PdfPTable(1);
           tblNotes = GetNotesTable();
 
           //*****************
 
           sbHtmlText.Append("<HTML><BODY STYLE=FONT-FAMILY: 'ARIAL';FONT-SIZE:11 !IMPORTANT;><TABLE  STYLE=VALIGN:TOP;><TR><TD>");
           // sbHtmlText.Append(strContact);
           sbHtmlText.Append(strExihibition);
           //sbHtmlText.Append(strPaymentDetails);
 
           //sbHtmlText.Append(strFooter);
           sbHtmlText.Append("</TD></TR></TABLE></BODY></HTML>");
 
           Response.ContentType = "application/pdf";
           Response.AddHeader("content-disposition", "attachment;filename=Invoice_" + txtCompanyName.Text.Trim() + ".pdf");
           Response.Cache.SetCacheability(HttpCacheability.NoCache);
           StringWriter sw = new StringWriter();
 
           StringReader srHtmlText = new StringReader(sbHtmlText.ToString());
           //Document pdfDocument = new Document(PageSize.A4, 40f, 40f, 15f, 2f);
           Document pdfDocument = new Document(PageSize.A4, 40f, 40f, 15f, 60f);//added by chetan
 
           iTextSharp.text.html.simpleparser.HTMLWorker htmlparser = new iTextSharp.text.html.simpleparser.HTMLWorker(pdfDocument);
 
           PdfWriter pdfDoc1 = PdfWriter.GetInstance(pdfDocument, Response.OutputStream);
           var page = new ITextEvents();
           page.email = txtExpogrEmail.Text.Trim();
           pdfDoc1.PageEvent = page;
 
 
 
           pdfDocument.Open();
           pdfDocument.Add(ph1);
           pdfDocument.Add(tblBlank);
           pdfDocument.Add(tblAddInfo);
 
           pdfDocument.Add(tblCon);
           pdfDocument.Add(tblExhibition);
           pdfDocument.Add(tblNotes);
           pdfDocument.Add(iHeader);
 
           pdfDocument.Add(jpg);
           pdfDocument.Add(jpg1);
 
           htmlparser.Parse(srHtmlText);
           pdfDocument.Close();
           Response.Write(pdfDocument);
           Response.End();
       }
       catch (Exception ex)
       {
       } 
   }

 

Posted one year ago

As you have sets Footer by setting ScaleToFit and SetAbsolutePosition so it will always come in last either there will be any content or not.
If content is reached to the end of page it will always override to the jpg1 part as it already sets by ScaleToFit and SetAbsolutePosition at the end of page.

chetan says:
jpg1.ScaleToFit(830, 160);

 

chetan says:
jpg1.SetAbsolutePosition(45, 2);

 1) If you need to add signature in the last when the pdf content get end just add the pdfDocument.add(jpg1) in last after htmlparser.parse(srHtmlText) also comment the both above line which sets ScaleToFit and SetAbsolutePosition of jpg1.


2) Or if you need sets Header and footer in each page the refer the below link for your reference.

Add Header and Footer to PDF using iTextSharp C#