Adhikar Patil

Adhikar Patil

  • NA
  • 481
  • 128.3k

I want to change report title dynamically using local report

Mar 8 2020 12:56 PM
Hi,
 
I export a LocalReport to a PDF file, I use the LocalReport.Render(...) method, but the title of PDF is the
file name of the LocalReport.
 
If the LocalReport file is "NBISIAReport.rdlc", the title of PDF will be "NBISIAReport".
 
I want to define the title dynamically. Thanks an advance...
 
Here is My Code
  1. [HttpPost]  
  2. public ActionResult GenerateMultipleNBISIAReport(List<NBISIAReportViewModel> structures)  
  3. {  
  4. try  
  5. {  
  6. //var dirName = User.Identity.Name.Replace(" ", "");  
  7. DateTime currDate = DateTime.Now;  
  8. string dirName = "NBI-SIA_Reports" + "_" + currDate.ToString("MM") + "_" + currDate.ToString("dd") + "_" + currDate.ToString("yy") + "_" + currDate.ToString("HH") + "_" + currDate.ToString("mm");  
  9. var reportFolderPath = Request.MapPath(Request.ApplicationPath) + "Downloads/" + dirName;  
  10. const string fileExtension = "PDF";  
  11. var zipFolderPath = reportFolderPath + "/" + dirName + ".zip";  
  12. var exists = Directory.Exists(reportFolderPath);  
  13. if (!exists)  
  14. {  
  15. Directory.CreateDirectory(reportFolderPath);  
  16. }  
  17. else  
  18. {  
  19. Directory.Delete(reportFolderPath, true);  
  20. Directory.CreateDirectory(reportFolderPath);  
  21. }  
  22. foreach (var structure in structures)  
  23. {  
  24. if (structure.Id == nullcontinue;  
  25. string reportName;  
  26. string reportPath;  
  27. string reportTitle;  
  28. switch (structure.StrKindCode)  
  29. {  
  30. case "1":  
  31. reportPath = Request.MapPath(Request.ApplicationPath) + @"Reports\NBISIAReport.rdlc";  
  32. reportName = structure.StrNumber + "-Bridge.pdf";  
  33. break;  
  34. case "2":  
  35. reportPath = Request.MapPath(Request.ApplicationPath) + @"Reports\NBISIAReport.rdlc";  
  36. reportName = structure.StrNumber + "-Culvert.pdf";  
  37. break;  
  38. case "3":  
  39. reportPath = Request.MapPath(Request.ApplicationPath) + @"Reports\NBISIAReport.rdlc";  
  40. reportName = structure.StrNumber + "-Tunnel.pdf";  
  41. break;  
  42. case "4":  
  43. reportPath = Request.MapPath(Request.ApplicationPath) + @"Reports\NBISIAReport.rdlc";  
  44. reportName = structure.StrNumber + "-Trail.pdf";  
  45. break;  
  46. default:  
  47. reportPath = Request.MapPath(Request.ApplicationPath) + @"Reports\NBISIAReport.rdlc";  
  48. reportName = structure.StrNumber + "-Other.pdf";  
  49. break;  
  50. }  
  51. var localReport = new LocalReport  
  52. {  
  53. ReportPath = reportPath  
  54. };  
  55. var obj = new InspectionReportData();  
  56. var ds = obj.GetNBISIAReportData(Convert.ToInt32(structure.Id), AgencyCode);  
  57. localReport.DataSources.Add(new ReportDataSource("NBISIAReportDataSet", ds.Tables[0]));  
  58. localReport.DataSources.Add(new ReportDataSource("NBISIABIPReportDataSet", ds.Tables[1]));  
  59. localReport.EnableExternalImages = true;  
  60. localReport.DisplayName='Bridge NBI SIA Report';  
  61. var renderByte = localReport.Render(fileExtension, "");  
  62. using (var fileStream = new FileStream(reportFolderPath + "/" + reportName, FileMode.Create))  
  63. {  
  64. fileStream.Write(renderByte, 0, renderByte.Length);  
  65. }  
  66. }  
  67. using (var zip = new ZipFile())  
  68. {  
  69. zip.AddDirectory(reportFolderPath);  
  70. zip.Save(zipFolderPath);  
  71. }  
  72. dirName = string.Empty;  
  73. return Json(new { success = true, filePath = zipFolderPath }, JsonRequestBehavior.AllowGet);  
  74. }  
  75. catch (Exception ex)  
  76. {  
  77. return Json(new { success = false, message = ex.Message }, JsonRequestBehavior.AllowGet);  
  78. }  
  79. }  

Answers (9)