【问题标题】:itextsharp pdf not showingitextsharp pdf不显示
【发布时间】:2015-07-16 12:59:10
【问题描述】:

我正在使用以下代码从 GridView 使用 iTextSharp 生成 PDF,但是生成的 PDF 对我来说是不可见的。如何在我的 html 页面中查看它?

GridView1.Visible = false;
SqlConnection sql = Connection.con();
sql.Open();

SqlCommand cmd = new SqlCommand("spGetSalesbyCustomer", sql);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@CustomerId", Convert.ToInt32(TextBox1.Text));
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable dd = new DataTable();

adp.Fill(dd);

GridView2.DataSource = dd;
GridView2.DataBind();
int cellCount = GridView2.Columns.Count;
sql.Close();

if (cellCount > 0)
{


    GridView2.AllowPaging = false;
    GridView2.DataBind();

    BaseFont bf = BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") + @"\fonts\ARIALUNI.TTF", BaseFont.IDENTITY_H, true);

    iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(cellCount);
    int[] widths = new int[cellCount];
    for (int x = 0; x < cellCount; x++)
    {
        widths[x] = (int)GridView2.Columns[x].ItemStyle.Width.Value;
        string cellText = Server.HtmlDecode(GridView2.HeaderRow.Cells[x].Text);

        //Set Font and Font Color
        iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL);
        //font.Color = new Color(GridView2.HeaderStyle.ForeColor);
        iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(12, cellText, font));

        //Set Header Row BackGround Color
        //cell.BackgroundColor = new Color(GridView2.HeaderStyle.BackColor);


        table.AddCell(cell);
    }
    table.SetWidths(widths);

    for (int i = 0; i < GridView2.Rows.Count; i++)
    {
        if (GridView2.Rows[i].RowType == DataControlRowType.DataRow)
        {
            for (int j = 0; j < GridView2.Columns.Count; j++)
            {
                string cellText = Server.HtmlDecode(GridView2.Rows[i].Cells[j].Text);

                //Set Font and Font Color
                iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL);
                //font.Color = new Color(GridView2.RowStyle.ForeColor);
                iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(12, cellText, font));

                //Set Color of row
                if (i % 2 == 0)
                {
                    //Set Row BackGround Color
                    //cell.BackgroundColor = new Color(GridView2.RowStyle.BackColor);
                }

                table.AddCell(cell);
            }
        }
    }

    //Create the PDF Document
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();

    int pages = pdfDoc.;
    pdfDoc.Close();
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Write(pdfDoc);
    Response.End();

【问题讨论】:

    标签: c# asp.net pdf itextsharp


    【解决方案1】:

    你的问题有点不清楚,但如果你的代码是正确的(我知道它不是 100% 基于倒数第七行)那么你实际上并没有将你的 PdfPTable 添加到 Document:

    //Create the PDF Document
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
    
    //Bind a writer to our document abstraction and our output stream
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    
    //Open the document for writing
    pdfDoc.Open();
    
    //This next line is a syntax error
    //int pages = pdfDoc.;
    
    //Add the table to the PDF
    pdfDoc.Add(table);
    
    //Close the document
    pdfDoc.Close();
    
    //ASP.Net/HTTP stuff
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    
    //Do not use this next line, it doesn't do what you think it does
    //Response.Write(pdfDoc);
    
    Response.End();
    

    【讨论】:

    • 其实我只是想从gridview生成一个pdf并查看它。
    • 实现了你的代码,但仍然没有显示生成的 pdf
    • “展示”的具体含义是什么。您是否收到一个空白的 PDF 文件?您收到错误消息吗?你有 HTML 吗?
    • 请原谅我的英语。我看不到输出。代码成功运行,没有任何错误,但屏幕上没有显示任何内容。我的意思是我看不到pdf文件是否生成。
    • 当您右键单击并查看源代码时,您会看到什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-30
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多