I have a program that is installed in C:\Program Files (x86)\Qiagen\PGxUtils and I am using the code below to render reports by passing the full path of the report template to the function. This works fine when it is a single standalone report but when I try to use a report with subreports I get the following:
Could not find file 'C:\Program Files (x86)\Qiagen\PGxUtils\SubFinalReportV1_PatientDemographics.trdp'.,Could not find file 'C:\Program Files (x86)\Qiagen\PGxUtils\SubFinalReportV1_Header.trdp'.,
Could not find file 'C:\Program Files (x86)\Qiagen\PGxUtils\SubFinalReportV1_PrescribingInsights.trdp'.,
Could not find file 'C:\Program Files (x86)\Qiagen\PGxUtils\SubFinalReportV1_RiskManagement.trdp'.,
Could not find file 'C:\Program Files (x86)\Qiagen\PGxUtils\SubFinalReportV1_Medication.trdp'.,
Could not find file 'C:\Program Files (x86)\Qiagen\PGxUtils\SubFinalReportV1_TestResult.trdp'.
Reading the online documentation this seems to be by design but I need to know how to work around it in order to use subreports.
using Telerik.Reporting;using System.Text.Json.Nodes;
using System.Text.Json;
namespace PdfReportBuilder
{
public static class TelerikReportBuilder
{
public static ReportSource GetReportSource(string reportPath, JsonNode jsonNode)
{
var reportPackager = new ReportPackager();
using (var sourceStream = System.IO.File.OpenRead(reportPath))
{
Report report = (Report)reportPackager.UnpackageDocument(sourceStream);
var jsonDataSources = report.GetDataSources().OfType<JsonDataSource>();
foreach (var jsonDataSource in jsonDataSources)
{
jsonDataSource.Source = jsonNode.ToJsonString(new System.Text.Json.JsonSerializerOptions { WriteIndented = true });
// This proves that Source can be assigned a json string, assigning a JsonNode doesn't work.
// JsonNode? tmp = JsonNode.Parse((jsonDataSource.Source as string)!);
// jsonDataSource.Source = tmp.ToJsonString(new System.Text.Json.JsonSerializerOptions { WriteIndented = true });
}
return new InstanceReportSource
{
ReportDocument = report
};
}
}
public static byte[] BuildReport(string reportPath, JsonNode jsonNode)
{
var reportProcessor = new Telerik.Reporting.Processing.ReportProcessor();
// set any deviceInfo settings if necessary
var deviceInfo = new System.Collections.Hashtable();
ReportSource reportSource = GetReportSource(reportPath, jsonNode);
Telerik.Reporting.Processing.RenderingResult result = reportProcessor.RenderReport("PDF", reportSource, deviceInfo);
if (!result.HasErrors)
{
return result.DocumentBytes;
}
else
{
throw new Exception(string.Join(", ", result.Errors.Select(ex => ex.Message)));
}
}
public static byte[] BuildReport(string templatePath, JsonElement root)
{
JsonNode? jsonNode = JsonNode.Parse(root.GetRawText());
if (jsonNode == null)
{
throw new ArgumentNullException(nameof(jsonNode), "Parsed JsonNode cannot be null.");
}
return BuildReport(templatePath, jsonNode);
}
}
}