【问题标题】:Why the table.addcell(cell) will just display html code int iTextSharp为什么 table.addcell(cell) 只会显示 html 代码 int iTextSharp
【发布时间】:2011-11-09 09:39:38
【问题描述】:

我目前正在尝试将 gridview 导出为 pdf 文件。

我的代码是:

public void GvExportPDF(GridView gvListing, string fileName,int TotalColumns)
{
        http.Response.ContentType = "application/pdf";

        http.Response.AddHeader("content-disposition", "attachment;filename= " + fileName + ".pdf");

        http.Response.Cache.SetCacheability(HttpCacheability.NoCache);

        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);

        gvListing.AllowPaging = false;

        PdfPTable table = new PdfPTable(TotalColumns);

        foreach (GridViewRow rows in gvListing.Rows)
        {
            if (rows.RowType == DataControlRowType.Header)
            {
                for (int i = 0; i < rows.Cells.Count; i++)
                {
                    PdfPCell cell = new PdfPCell();
                    cell.Phrase = new Phrase(getCellText(rows.Cells[i]));
                    table.AddCell(cell);
                }
            }
            else if (rows.RowType == DataControlRowType.DataRow)
            {

                System.Web.UI.WebControls.Image Image1 = (System.Web.UI.WebControls.Image)rows.FindControl("Image1");
                string url = Image1.ImageUrl;
                Image1.Visible = false;

                iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(new Uri(url)); 
                jpg.ScaleToFit(8f, 10f);
                jpg.Border = Rectangle.NO_BORDER;
                for (int i = 0; i < rows.Cells.Count; i++)
                {
                    if (i == 0)
                        table.AddCell(jpg);
                    else
                    {

                        PdfPCell cell = new PdfPCell(new Phrase(HttpContext.Current.Server.HtmlDecode(getCellText(rows.Cells[i]))));


                        table.AddCell(cell);
                    }
                }

            }
        }
        //gvListing.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, http.Response.OutputStream);
        try
        {
            pdfDoc.Open();
            pdfDoc.Add(table);
        }
        catch (DocumentException dex)
        {
            //http.Response.Write(dex.Message);
            System.Diagnostics.Debug.WriteLine(dex.Message);
        }
        catch (IOException ioex)
        {
            //http.Response.Write(ioex.Message);
            System.Diagnostics.Debug.WriteLine(ioex.Message);
        }
        catch (Exception ex)
        {
            //http.Response.Write(ex.Message);
            System.Diagnostics.Debug.WriteLine(ex.Message);
        }

        finally
        {
            pdfDoc.Close();
            //http.Response.Write(StyleOfExportTable());
            http.Response.Output.Write(pdfDoc);
            http.Response.End();
        }
    }

    private string getCellText(TableCell cell)
    {
        StringBuilder sb = new StringBuilder(cell.Text);

        foreach (Control c in cell.Controls)
        {
            if (c.Visible)
            {
                string controlText = getTextProperty(c);
                if (controlText != null)
                {
                    if (sb.Length > 0) sb.Append(" ");
                    sb.Append(controlText);
                }
            }
        }
        return sb.ToString();
    }

    private string getTextProperty(object o)
    {
        PropertyInfo propertyInfo = o.GetType().GetProperty("Text");
        if (propertyInfo != null)
        {
            MethodInfo getMethod = propertyInfo.GetGetMethod();
            if (getMethod != null)
            {
                return (string)getMethod.Invoke(o, null);
            }
        }
        return string.Empty;
    }

它可以导出pdf,可以导出我想要的总列,但除了图像,其他单元格只显示html代码&lt;table&gt;&lt;tr&gt;&lt;td&gt;text&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;

这是什么问题,我该如何解决?

【问题讨论】:

  • 您是在使用swhwsrhtmlparser 还是其他的?
  • 是的,克里斯·哈斯。我正在使用 htmlparser 和 stringbuilder

标签: asp.net c#-4.0 gridview itextsharp cell


【解决方案1】:

我没有直接的答案,但我几乎可以肯定地告诉您,问题将出在您为设置PdfPCell 的内容所做的任何事情上。 iTextSharp 的核心没有 HTML 的概念,几乎所有命令都是基于 PDF 的,其中有几个特定于 .Net 的命令。 一个解析器,可以将 HTML 转换为基于 PDF 的命令,但您没有使用它。因此,无论您做什么来获取单元格的内容,都会返回 iTextSharp 将其视为字符串文字的 HTML。您需要让算法仅返回文本,或者使用 iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList 将 HTML 转换为 iTextSharp 对象。

【讨论】:

  • 谢谢克里斯,但我应该把这个放在哪里?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-04
  • 1970-01-01
  • 1970-01-01
  • 2013-04-04
相关资源
最近更新 更多