You must Sign In to post a response.
  • Category: Visual Studio

    How to create pdf file in c# windows application

    i need to create pdf file what i having in datagridview . not database for example i having 10 columns in database but i showed in datagridview 5 columns only ...any tell source code with example
  • #741734

    There are many PDFwriter files, i am giving few links for code examples


  • #741735

    You can add the grid values into the temp datatable.
    try it bellow code will help you

    protected void btnExport_Click(object sender, EventArgs e)
    DataTable dtExport = new DataTable();
    for (int j = 0; j < GridView1.Rows.Count; j++)
    DataRow dr;
    GridViewRow row = GridView1.Rows[j];
    dr = dtExport .NewRow();
    for (int i = 0; i < row.Cells.Count; i++)
    dr[i] = row.Cells[i].Text;

    dtExport .Rows.Add(dr);

    lblerr.Text = "";
    Response.Buffer = true;
    //Excel sheet Header Text
    string filename = " Summary Report" + DateTime.Now.ToString() + ". pdf ";
    Response.AddHeader("content-disposition", "attachment; filename=" + filename + ".pdf");
    Response.ContentType = "application/";
    string tab = "";
    //Display column name
    foreach (DataColumn dc in dtExport.Columns)
    HttpContext.Current.Response.Write(tab + dc.ColumnName.ToUpper());
    tab = "\t";
    int i;
    //get table value
    foreach (DataRow dr in dtExport.Rows)
    tab = "";
    for (i = 0; i < dtExport.Columns.Count; i++)
    HttpContext.Current.Response.Write(tab + dr[i].ToString());
    tab = "\t";

  • #741759

    You can use below code if you have iTextSharp dll. You may download the dll from
    Code for exporting data to Excel/Word/PDF and all will be same except the two lines of code where we specifically mention PDF.

    private void ExportToExcel()
    string Excelfilename = "Test_pdf_Export" + DateTime.Now;
    Response.Buffer = true;
    Response.ContentType = "application/pdf";
    Response.Charset = "";
    this.EnableViewState = false;
    System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

    Asheej T K

  • #741762
    you can use the third part to create dll like Itextsharp.dll.

    You will get many example after some google with this.

    Hope this helps you

    Shalini Rathore

  • #741763
    using (FileStream fs = new FileStream(Server.MapPath(@"Files\FileForPdf.pdf"), FileMode.Create))
    using (iTextSharp.text.Document doc = new iTextSharp.text.Document(PageSize.A4, 10f, 10f, 10f, 0f))

    PdfWriter writer = PdfWriter.GetInstance(doc, fs);
    doc.Add(new Paragraph("hello I am paragraph"));



    This will replace the existing blank file with the new one.

    Shalini Rathore

  • #741790
    There are multiple way to create a PDF file, as said by Asheej you can use Response Obejct for create PDF file.
    - Other way is to create Word file export all grdiview content to word and then use SaveAs() method of word document and convert it to PDF file.
    - Other way is to use iTextSharp library to create PDF file see below sample link

    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #751911
    You can use free PDFSharp library for create pdf file from C#


  • #769499
    In one sentence, use ZetPDF. It's a powerful and the fastest pdf SKD for .NET applications to Develop high performance .NET applications having the ability to Create, Edit, Convert, Protect or Print PDF documents, without requiring Adobe Acrobat. Download it from

  • #769502
    You can use Free Spire.PDF library to easily create PDF from datagridview. Get Free Spire.PDF dll from NuGet ( and refer to the following code:

    PdfDocument pdf = new PdfDocument();

    PdfPageBase page =pdf.Pages.Add();

    PdfTable table = new PdfTable();

    table.DataSource = dataGridView1.DataSource;

    table.Style.ShowHeader = true;

    table.Style.CellPadding = 2;

    PdfTableLayoutFormat tableLayout = new PdfTableLayoutFormat();
    tableLayout.Break = PdfLayoutBreakType.FitElement;
    tableLayout.Layout = PdfLayoutType.Paginate;

    table.BeginRowLayout += newBeginRowLayoutEventHandler(table_BeginRowLayout);

    table.Draw(page, new RectangleF(10, 50, 300, 300), tableLayout);

    private static void table_BeginRowLayout(objectsender, BeginRowLayoutEventArgs args)
    PdfCellStyle cellstyle = new PdfCellStyle();
    cellstyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
    cellstyle.Font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 9f), true);
    args.CellStyle = cellstyle;

  • Sign In to post your comments