Asp.net ItextSharp错误:文档没有页面

Asp.net ItextSharp错误:文档没有页面,asp.net,pdf,itextsharp,Asp.net,Pdf,Itextsharp,我正在使用itextsharp将我的asp.net页面转换为pdf。单击按钮时,我正在编写以下代码: Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache)

我正在使用itextsharp将我的asp.net页面转换为pdf。单击按钮时,我正在编写以下代码:

        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.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();
但我得到了一个错误:“文档没有页面”
请帮助我消除此错误并生成pdf。为此,需要采取两个步骤:

1) 检查ITextSharp dll是否为最新版本


2) 使用链接按钮而不是按钮,不要使用超链接,因为它会导致超链接按钮出错。

您可以创建一个模板pdf文件吗?关于此链接有一个解决方案:但是页面内容以pdf格式转换为纯格式。有没有办法保持原始标记的pdf格式(颜色等)?检查sw.ToString()是否为string.Empty。@ketan我认为这个问题需要一个理解透彻的答案-它首先需要额外的细节,因为OP已经半年没有出现在这里了。