Click here to Skip to main content
15,909,039 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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);
            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);
            pdfDoc.Close();
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment; filename=Report.pdf");
            Response.Clear();
            Response.BinaryWrite(mStream.ToArray());

            Response.End();
     

        }
Posted
Updated 9-Oct-13 1:53am
v6
Comments
Mart Rijkers 9-Oct-13 7:13am    
Is this a httpresponse? And what exactly is your question?
vineethnair 9-Oct-13 7:21am    
sir,
i want to save this converted pdf file to temporary folder(virtual path not physical path)..
ZurdoDev 9-Oct-13 7:21am    
Where are you stuck then?
vineethnair 9-Oct-13 7:23am    
i tried many codes for save this pdf file to virtual folder..but not working...
ZurdoDev 9-Oct-13 7:44am    
Why won't it work? Where won't it work? Does pdfDoc have a SaveAs method?

If this is a web application, this means that Response is actually HttpResponse. In that case you can use

C#
string filename = "wherever you want";
Response.WriteFile(filename);
 
Share this answer
 
If you need this:
"after download i need the copy of this pdf file to folder in my web application(not system folder)"

then you should not use HttpResponse but plain File IO to save the file. Use "Server.MapPath" to find the file location on your webserver. For safety you can save the file in a seperate folder and restrict file access on all other folders.
 
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