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

This ITextSharp C# code generates empty pdf ? why?

我是从不相关的提交按钮调用此方法的。 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();
}

这里是Bindgv();

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

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