0

I am using the following code to generate a PDF using iTextSharp from a GridView however the generated PDF is not visible to me. How can I view it in my html page?

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 Answer 1

1

Your question is a little unclear but if your code is correct (and I know it isn't 100% based on the seventh last line) then you're not actually adding your PdfPTable to the 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();
5
  • well actually i just want to generate a pdf from gridview and view it. Commented Jul 16, 2015 at 13:58
  • implemented your code but still not showing the generated pdf Commented Jul 16, 2015 at 14:13
  • What does "showing" mean more specifically. Are you getting a PDF file that's blank? Are you getting an error message? Are you get HTML?
    – Chris Haas
    Commented Jul 16, 2015 at 16:17
  • Pardon my english. I cant see the output. The code runs successfully without any errors but nothing is displayed on the screen. I mean i cant see the pdf file if it is generated or not. Commented Jul 16, 2015 at 17:36
  • And when you right-click and view source what do you see?
    – Chris Haas
    Commented Jul 16, 2015 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.