1

I am using this code to mail a datatable in the form of a pdf. But on converting the data using html parser, it is showing the exception as Stack Empty. However I have done this before and it worked, I just cant put my finger on what am I missing now. Please help!!!!!

using (StringWriter sw = new StringWriter()) { using (HtmlTextWriter hw = new HtmlTextWriter(sw)) { StringBuilder sb = new StringBuilder(); //sb.Append("<table width='100%' cellspacing='0' cellpadding='2'>"); foreach (DataColumn column in dt.Columns) { //sb.Append("<th style = 'background-color: #D20B0C;color:#ffffff'>"); sb.Append(column.ColumnName); sb.Append("</th>"); } sb.Append("</tr>"); foreach (DataRow row in dt.Rows) { sb.Append("<tr>"); foreach (DataColumn column in dt.Columns) { sb.Append("<td>"); sb.Append(row[column]); sb.Append("</td>"); } sb.Append("</tr>"); } sb.Append("</table>"); using (MemoryStream memoryStream = new MemoryStream()) { PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream); pdfDoc.Open(); htmlparser.Parse(sr);//here is the error pdfDoc.Close(); byte[] bytes = memoryStream.ToArray(); memoryStream.Close(); MailMessage mm = new MailMessage(txtEmail.Text.Trim(), email.Trim()); mm.Subject = txtSubject.Text.Trim(); //mm.CC.Add = txtcc.Text.Trim(); mm.Body = txtBody.Text.Trim(); mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "iTextSharpPDF.pdf")); mm.IsBodyHtml = true; SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; smtp.EnableSsl = true; NetworkCredential NetworkCred = new NetworkCredential(); NetworkCred.UserName = txtEmail.Text.Trim(); NetworkCred.Password = txtPassword.Text.Trim(); smtp.UseDefaultCredentials = true; smtp.Credentials = NetworkCred; smtp.Port = 587; smtp.Send(mm); } 

} }

7
  • what is sr variable. If it is a HTML string then put a breakpoint on that line. And copy the HTML string and run some online html parser tool and check where is error. It looks like there is an issue in HTML string. Commented Aug 24, 2018 at 6:00
  • StringReader sr = new StringReader(sb.ToString()); Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); sr is the string reader and sb is the string builder Commented Aug 24, 2018 at 6:02
  • are you getting your html in string builder means sb.ToString() from html file or you build your html as hard coded in your program? Commented Aug 24, 2018 at 6:05
  • getting html in string builder Commented Aug 24, 2018 at 6:09
  • could u please add your string builder code in post ? Commented Aug 24, 2018 at 6:32

2 Answers 2

2

1) Make you changes like below while building your html via StringBuilder like

StringBuilder sb = new StringBuilder(); //Open table sb.Append("<table width='100%' cellspacing='0' cellpadding='2'>"); //Open first tr for all of th sb.Append("<tr>"); foreach (DataColumn column in dt.Columns) { sb.Append("<th style = 'background-color: #D20B0C;color:#ffffff'>"); sb.Append(column.ColumnName); sb.Append("</th>"); } //Close first tr for all of th sb.Append("</tr>"); foreach (DataRow row in dt.Rows) { //Open tr for all of rows sb.Append("<tr>"); foreach (DataColumn column in dt.Columns) { sb.Append("<td>"); sb.Append(row[column]); sb.Append("</td>"); } //Close tr for all of rows sb.Append("</tr>"); } //Close table sb.Append("</table>"); 

2) Use XMLWorkerHelper instead of HTMLWorker because now HTMLWorker is deprecated. and not more support by iTextSharp. like

PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream); pdfDoc.Open(); XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr); pdfDoc.Close(); byte[] bytes = memoryStream.ToArray(); memoryStream.Close(); 

See this link

Try once may it help you.

Sign up to request clarification or add additional context in comments.

2 Comments

@Tanvi Dutta, view the answer might be it help you :)
@Tanvi Dutta, if answer was helpful to you then accept it or let me know if you have any issue :)
1

You are missing openning tags, that's why component throws exception, since it reads closing tags which were not opened and stack is empty (probably it uses stack to track openning/closing tags):

using (StringWriter sw = new StringWriter()) { using (HtmlTextWriter hw = new HtmlTextWriter(sw)) { StringBuilder sb = new StringBuilder(); sb.Append("<table width='100%' cellspacing='0' cellpadding='2'>"); sb.Append("<tr>"); foreach (DataColumn column in dt.Columns) { sb.Append("<th>"); sb.Append(column.ColumnName); sb.Append("</th>"); } sb.Append("</tr>"); foreach (DataRow row in dt.Rows) { sb.Append("<tr>"); foreach (DataColumn column in dt.Columns) { sb.Append("<td>"); sb.Append(row[column]); sb.Append("</td>"); } sb.Append("</tr>"); } sb.Append("</table>"); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.