Save RDLC Report as PDF at Run Time in C#


In my previous article I described how to create a RDLC report.

Now I show how to save a RDLC report as a PDF at run time.

I am creating a method for that named "SAVEPDF" with the two arguments:

  • Path (Location) as string type where we want to save that PDF file.

    Example: C:\MyFile\
  • Another is ReportViewer that is the instance of the ReportViewer Class

    On which we will perform the action to convert the report to a PDF; but not just PDF, we can save the file in another format, such as Word or Excel.

Here is the sample code:

  1. public void SavePDF(ReportViewer viewer, string savePath)  
  3. {  
  4.      byte[] Bytes = viewer.LocalReport.Render(format:"PDF",deviceInfo:"");  
  6.      using (FileStream stream = new FileStream(savePath, FileMode.Create))  
  7.      {  
  8.               stream.Write(Bytes, 0, Bytes.Length);  
  9.     }  
  10. }  

We can save the RDLC report in another format like Word or Excel; not just in PDF.