Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I Want To Create A PDF File From Data Fetch From The Database in ASP.Net and c#, i also would like to have formating data in pdf file. and also want to show the generated pdf to web page by some pdf viewer control.
thanks in advance.
have a nice day.
Posted

you can use itextsharp for this purpose. have a look at this:
Creating PDF documents with iTextSharp[^]
 
Share this answer
 
You can datatable data to PDF and set PDF format by try below code:
C#
//Set table title
   PdfBrush brush1 = PdfBrushes.Black;
   PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold));
   PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
   page.Canvas.DrawString("Country List", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1);
   y = y + font1.MeasureString("Country List", format1).Height;
   y = y + 5;
   //create data table
   PdfTable table = new PdfTable();
   table.Style.CellPadding = 2;
   table.Style.BorderPen = new PdfPen(brush1, 0.75f);
   table.Style.DefaultStyle.BackgroundBrush = PdfBrushes.SkyBlue;
   table.Style.DefaultStyle.Font = new PdfTrueTypeFont(new Font("Arial", 10f));
   table.Style.AlternateStyle = new PdfCellStyle();
   table.Style.AlternateStyle.BackgroundBrush = PdfBrushes.LightYellow;
   table.Style.AlternateStyle.Font = new PdfTrueTypeFont(new Font("Arial", 10f));
   table.Style.HeaderSource = PdfHeaderSource.ColumnCaptions;
   table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.CadetBlue;
   table.Style.HeaderStyle.Font = new PdfTrueTypeFont(new Font("Arial", 11f, FontStyle.Bold));
   table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
   table.Style.ShowHeader = true;
   using (OleDbConnection conn = new OleDbConnection())
        {
            conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=demo.mdb";
            OleDbCommand command = new OleDbCommand();
            command.CommandText
                = " select Name, " as Flag, Capital, Continent, Area, Population, Flag as FlagData from country ";
            command.Connection = conn;
            using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(command))
            {
                DataTable dataTable = new DataTable();
                dataAdapter.Fill(dataTable);
                dataTable.Columns.Add(new DataColumn("FlagImage", typeof(PdfImage)));
                table.DataSourceType = PdfTableDataSourceType.TableDirect;
                table.DataSource = dataTable;
            }
        }
  float width
            = page.Canvas.ClientSize.Width
                – (table.Columns.Count + 1) * table.Style.BorderPen.Width;
        table.Columns[0].Width = width * 0.21f;
        table.Columns[0].StringFormat
            = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
        table.Columns[1].Width = width * 0.10f;
        table.Columns[1].StringFormat
            = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
        table.Columns[2].Width = width * 0.19f;
        table.Columns[2].StringFormat
            = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
        table.Columns[3].Width = width * 0.21f;
        table.Columns[3].StringFormat
            = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
        table.Columns[4].Width = width * 0.12f;
        table.Columns[4].StringFormat
            = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle);
        table.Columns[5].Width = width * 0.17f;
        table.Columns[5].StringFormat
            = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle);
        table.BeginRowLayout += new BeginRowLayoutEventHandler(table_BeginRowLayout);
        table.EndCellLayout += new EndCellLayoutEventHandler(table_EndCellLayout);
        PdfTableLayoutFormat tableLayout = new PdfTableLayoutFormat();
        tableLayout.Break = PdfLayoutBreakType.FitElement;
        tableLayout.Layout = PdfLayoutType.Paginate;
        tableLayout.EndColumnIndex = table.Columns.Count – 2 – 1;
        PdfLayoutResult result = table.Draw(page, new PointF(0, y), tableLayout);
        y = y + result.Bounds.Height + 5;
        PdfBrush brush2 = PdfBrushes.Gray;
        PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 9f));
        page.Canvas.DrawString(String.Format("* {0} countries in the list.", table.Rows.Count),
        font2, brush2, 5, y);


and then, use a PDF viewer to show it on your aspx page.Please note that above code needs a third party PDF library but do not need Adobe Acrobat, You can give it a try.: PDF library[^]Good Luck!
 
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