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

时间:2015-04-19 19:35:55

标签: 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.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;
}

1 个答案:

答案 0 :(得分:0)

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