这个ITextSharp C#代码生成空pdf?为什么?

这个ITextSharp C#代码生成空pdf?为什么?,c#,asp.net,pdf,webforms,itextsharp,C#,Asp.net,Pdf,Webforms,Itextsharp,我从一个不相关的提交按钮调用这个方法。Bindgv()函数将数据库绑定到gridview,然后此代码生成 空PDF,我不知道为什么。如果有人能解决这个问题,我将非常感激 private void ExportToPdf() { gv.AllowPaging = true; Bindgv(); //Create a table PdfPTable table = new PdfPTable(gv.Columns.Count); table.Spacin

我从一个不相关的提交按钮调用这个方法。
Bindgv()函数将数据库绑定到gridview,然后此代码生成
空PDF,我不知道为什么。如果有人能解决这个问题,我将非常感激

 private void ExportToPdf()
 {
    gv.AllowPaging = true;
    Bindgv();

    //Create a table
    PdfPTable table = new PdfPTable(gv.Columns.Count);
    table.SpacingAfter = table.SpacingBefore = 5;

    //Set the column widths
    int[] widths = new int[gv.Columns.Count];
    for (int x = 0; x < gv.Columns.Count; x++)
    {
        widths[x] = (int)gv.Columns[x].ItemStyle.Width.Value;
        string cellText = Server.HtmlDecode(gv.HeaderRow.Cells[x].Text);
        PdfPCell cell = new PdfPCell(new Phrase(cellText));
        cell.BackgroundColor = new BaseColor(System
                           .Drawing.ColorTranslator.FromHtml("#008000"));
        table.AddCell(cell);
    }
    table.SetWidths(widths);

    //Transfer rows from GridView to table
    for (int i = 0; i < gv.Rows.Count; i++)
    {
        //does not enter here
        if (gv.Rows[i].RowType == DataControlRowType.DataRow)
        {
            for (int j = 0; j < gv.Columns.Count; j++)
            {
                string cellText = Server.HtmlDecode
                                  (gv.Rows[i].Cells[j].Text);
                iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(cellText));

                //Set Color of Alternating row
                if (i % 2 != 0)
                {
                    cell.BackgroundColor = new BaseColor(System.Drawing
                                        .ColorTranslator.FromHtml("#C2D69B"));
                }
                table.AddCell(cell);
            }
        }
    }

    //Create the PDF Document
    Document pdfDoc = new Document(PageSize.A4);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    pdfDoc.Add(table);
    pdfDoc.Close();
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;" +
                                   "filename=BillHistory.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Write(pdfDoc);
    Response.End();
}

我认为您需要处理GridView的事件,并将PDF创建为该事件的一部分。

在调试模式下,在调用BindGv methodits 23后检查gv.Rows.Count的结果。我也教过这段代码可能是问题所在,但事实并非如此。我只是用pdfDoc.Add(新段落(“Hello World”))替换pdfDoc.Add(表)来尝试你的代码,它可以工作。你能试试吗。如果是这样,则网格视图将数据加载到PDFTables时一定会出现问题。如果您将PDF直接发送到响应输出流。虽然这对您的资源肯定有好处,但这也意味着不能发送任何内容长度的标题。有些浏览器需要该标题。@PraveenPaulose是的,它也适用于我。我正在更新问题以给出Bindgv();方法的内容也一样。我从Adobe pdf reader打开原始文件时收到此错误此页面中存在错误acrobat可能无法正确显示页面
private void Bindgv()
{
    var idParam = new SqlParameter
    {
        ParameterName = "accNo",
        Value = txtAccountNo.Text
    };

    List<BillHistory> bh = new List<BillHistory>();
    bh = db.ExecuteStoreQuery<BillHistory>("exec BillHistory @accNo", idParam).ToList();
    gv.DataSource = bh;
    gv.DataBind();


    customer_registration cus = db.customer_registration.SingleOrDefault(p => p.account_number == txtAccountNo.Text);
    lblAccountNo.InnerHtml = cus.account_number;
    lblMeterNo.InnerHtml = cus.meter_number;
    lblPremises.InnerHtml = cus.apartment_number + "," + cus.house_name + "," + cus.street_name;
    lblBillingAddress.InnerHtml = cus.apartment_number + "," + cus.house_name + "," + cus.street_name;
    lblOwner.InnerHtml = cus.customer_name;
}