首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >itextsharp不显示

itextsharp不显示
EN

Stack Overflow用户
提问于 2015-07-16 20:59:11
回答 1查看 1K关注 0票数 0

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

代码语言:javascript
复制
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();
EN

回答 1

Stack Overflow用户

发布于 2015-07-16 21:45:42

您的问题有点模糊,但是如果您的代码是正确的(我知道它不是百分之百地基于倒数第七行),那么您实际上并没有将您的PdfPTable添加到Document

代码语言:javascript
复制
//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();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31454884

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档