July 21, 2024

SamTech 365

PowerPlatform, Power Apps, Power Automate, PVA, SharePoint, C#, .Net, SQL, Azure News, Tips ….etc

Generate a PDF from the Report Viewer (Web Form), in ASP.net & C#

In this article, I will share with you a simple way of generating a pdf from the report viewer. Assuming you have created your .RDLC and used it in an ASP.net page, users could click on the export form from the report viewer which will allow them to expert the rendered report to an Excel, PDF or Word Document. However, if you want to generate the pdf and save it locally in the code behind using C#, you can use this simple snippet:

Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string filenameExtension;
byte[] bytes = ReportViewer1.LocalReport.Render("PDF", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);
string FilePath = Server.MapPath("/data/");
FilePath +=  "ReportName.pdf";
using (FileStream fs = new FileStream(FilePath, FileMode.Create))
  fs.Write(bytes, 0, bytes.Length);