【问题标题】:ITextSharp Error while exporting GridView to PDF将 GridView 导出为 PDF 时出现 ITextSharp 错误
【发布时间】:2012-12-12 13:32:28
【问题描述】:

我尝试将GridView 导出为 PDF。它给出了错误:

无法将“iTextSharp.text.html.simpleparser.CellWrapper”类型的对象转换为“iTextSharp.text.Paragraph”类型。

这里会报错

htmlparser.Parse(sr);

【问题讨论】:

    标签: c# gridview itextsharp export-to-pdf


    【解决方案1】:

    您应该在数据绑定和解析之前禁用排序和分页。

    代码如下:

    Response.ContentType = "application/pdf";
    
    Response.AddHeader("content-disposition","attachment;filename=GridViewExport.pdf");
    
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    
    StringWriter sw = new StringWriter();
    
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    
    GridView1.AllowPaging = false;     <----
    
    GridView1.AllowSorting = false;    <----
    
    GridView1.DataBind();
    
    GridView1.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, Response.OutputStream);
    
    pdfDoc.Open();
    
    htmlparser.Parse(sr);
    
    pdfDoc.Close();
    
    Response.Write(pdfDoc);
    
    Response.End(); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多