itextsharp pdf没有显示

时间:2015-07-16 12:59:11

标签: c# asp.net pdf itextsharp

我使用以下代码从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();

1 个答案:

答案 0 :(得分: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();