iTextSharp中HTMLWorker上的Stack Empty异常

时间:2018-08-24 05:56:15

标签: c# asp.net itext

我正在使用此代码以pdf形式邮寄数据表。但是在使用html解析器转换数据时,它将异常显示为Stack Empty。但是,我之前已经做过了,而且奏效了,我只是不能将手指放在我现在想念的东西上。请帮忙!!!!!

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);
            }

} }

2 个答案:

答案 0 :(得分:2)

1)通过html来构建StringBuilder时,使您像下面这样进行更改

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)使用XMLWorkerHelper代替HTMLWorker,因为现在不推荐使用HTMLWorkeriTextSharp不再提供更多支持。像

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

查看此link

尝试一次,可能对您有帮助。

答案 1 :(得分:1)

您缺少打开标签,这就是组件抛出异常的原因,因为它读取未打开的关闭标签并且堆栈为空(可能它使用堆栈来跟踪打开/关闭标签):

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>");