Click here to Skip to main content
15,891,621 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
sir,
bellow is my code for convert to pdf.but nothing displays in pdf
C#
protected void Button1_Click(object sender, EventArgs e)

        {
            GeneratePDF();
        }
        private void GeneratePDF()
        {
            DataTable dtTemp = new DataTable();
            dtTemp.Columns.Add("Customer ID");
            dtTemp.Columns.Add("First Name");
            dtTemp.Columns.Add("Last Name");
            dtTemp.Columns.Add("Address");
            dtTemp.Rows.Add("0001", "Peter", "p", "2376, 00800, calicut");
            dtTemp.Rows.Add("0002", "Alfred", "k", "3453, 00800, calicut");
            dtTemp.Rows.Add("0003", "Alice", "n", "6056, 00800, calicut");
            dtTemp.Rows.Add("0004", "Denis", "h", "5672, 00800, calicut");
            dtTemp.Rows.Add("0005", "Paul", "k", "8734, 00800, calicut");
            Document pdfDoc = new Document(PageSize.A4, 30, 30, 40, 25);
            var filename = Server.MapPath("test4.pdf");
            

            // var output = new FileStream(Path.Combine("C:\\Users\\apr13mpsip\\Downloads", filename), FileMode.Create);

            //iTextSharp.text.pdf.PdfWriter.GetInstance(doc1, output);

            using (var output = File.Create(filename))
            {
                PdfWriter writer = PdfWriter.GetInstance(pdfDoc, output);
            }
            
            //System.IO.MemoryStream mStream = new System.IO.MemoryStream();
            //PdfWriter writer = PdfWriter.GetInstance(pdfDoc, mStream);
            int cols = dtTemp.Columns.Count;
            int rows = dtTemp.Rows.Count;
            pdfDoc.Open();
            iTextSharp.text.Table pdfTable = new iTextSharp.text.Table(cols, rows);
            pdfTable.BorderWidth = 1;
          
            pdfTable.Padding = 1;
            pdfTable.Spacing = 1;

            //creating table headers
            for (int i = 0; i < cols; i++)
            {
                Cell cellCols = new Cell();
                Font ColFont = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.BOLD);
                Chunk chunkCols = new Chunk(dtTemp.Columns[i].ColumnName, ColFont);
                cellCols.Add(chunkCols);
                pdfTable.AddCell(cellCols);

            }
            //creating table data (actual result)
            for (int k = 0; k < rows; k++)
            {
                for (int j = 0; j < cols; j++)
                {
                    Cell cellRows = new Cell();
                    Font RowFont = FontFactory.GetFont(FontFactory.HELVETICA, 12);
                    Chunk chunkRows = new Chunk(dtTemp.Rows[k][j].ToString(), RowFont);
                    cellRows.Add(chunkRows);
                    pdfTable.AddCell(cellRows);

                }
            }

            pdfDoc.Add(pdfTable);
            
           
          
            //Response.ContentType = "application/octet-stream";
            //Response.AddHeader("Content-Disposition", "attachment; filename=Report.pdf");
           
            //Response.Clear();
            //Response.BinaryWrite(mStream.ToArray());

            //Response.End();
           
           

          
     

        }



[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 9-Oct-13 21:07pm
v2

1 solution

Um.
You do realise what a using block does, I assume?
So, how do you expect this to work:
C#
using (var output = File.Create(filename))
{
    PdfWriter writer = PdfWriter.GetInstance(pdfDoc, output);
}
You create a memory stream, you connect it to a PDF document...and then you close it and throw it away...
You don't try to write it, save it or send it anywhere, so the whole of your code does a total of nothing...:laugh:

So look at extending the using block, and actually doing something with the data once you have added your table and such like!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900