【问题标题】:This ITextSharp C# code generates empty pdf ? why?此 ITextSharp C# 代码生成空 pdf 吗?为什么?
【发布时间】:2015-04-19 19:35:55
【问题描述】:

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

【问题讨论】:

  • 在调试模式下,调用BindGv方法后检查gv.Rows.Count的结果是什么
  • 它 23。我也教过这段代码是问题所在,但事实并非如此。
  • 我刚刚尝试了您的代码,将 pdfDoc.Add(table) 替换为 pdfDoc.Add(new Paragraph("Hello World")) 并且它有效。你能试试吗。如果是这样,那肯定是网格视图将数据加载到您的 pdfTable 中的问题
  • 您将 PDF 直接发送到响应输出流。虽然这肯定对您的资源有好处,但这也意味着无法发送内容长度标头。某些浏览器需要该标头。
  • @PraveenPaulose 是的,它也对我有用。我正在更新问题以提供 Bindgv();方法内容也。我从 Adob​​e pdf 阅读器打开原始文件时收到此错误 此页面中存在错误 acrobat 可能无法正确显示页面

标签: c# asp.net pdf webforms itextsharp


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    • 1970-01-01
    • 2018-06-22
    • 2013-02-02
    • 2015-07-28
    • 2012-09-21
    相关资源
    最近更新 更多