Skip to main content Skip to footer

Exporting Wijmo GridView to PDF

Wijmo Gridview does not have any built-in methods for exporting its data to PDF or an Excel file. If you want to export to excel, then we already have a blog on this. This blog illustrates a simple approach by which you can save the content of Wijmo Gridview in a PDF file. The trick is to save the grid data into a html string using StringWriter and HtmlTextWriter classes. And then, generate a PDF file using another control i.e. C1PdfDocument which is a part of C1Pdf dll. When you create an instance of the C1PdfDocument class, you get a PDF document with a single blank page. You can add content to the page using methods like DrawStringHtml(), DrawString(), etc.. Here is the custom method which you can use :


<pre>public void ExportToPDF(C1GridView grid)  
 {  
    C1PdfDocument pdfdoc = new C1PdfDocument();  
    RectangleF rect = pdfdoc.PageRectangle;  
    Font font = new Font("Times New Roman", 12);  

    StringWriter sw = new StringWriter();  
    HtmlTextWriter htmlwriter = new HtmlTextWriter(sw);  

    System.Web.UI.HtmlControls.HtmlForm form1 = new System.Web.UI.HtmlControls.HtmlForm();  
    grid.AllowPaging = false;  
    grid.DataBind();  
    form1.Controls.Add(grid);  
    form1.Controls[0].RenderControl(htmlwriter);  
    StringReader streader = new StringReader(sw.ToString());  
    string str = streader.ReadToEnd();  
    string savepath = Server.MapPath("TestData.pdf");  

    for (int start = 0; ; )  
     {  
        start = pdfdoc.DrawStringHtml(str, font, Brushes.Black, rect, start);  
        // done?  
        if (start >= int.MaxValue)  
         {  
           break;  
          }  
        // skip to next page  
       pdfdoc.NewPage();  
     }  

   pdfdoc.Save(savepath);  
   streader.Close();  
   lblUpdate.Text = "Data has been exported to PDF";  
   System.Diagnostics.Process.Start(savepath);  
 }  

You may refer to the samples below for complete implementation. Download Sample - C# Download Sample - VB

MESCIUS inc.

comments powered by Disqus