Używam Telerik Report Designer (autonomiczny) do projektowania raportu (.trdx), jak eksportować go do pliku pdf programowo w kodzie C #?
Odpowiedzi:
1 dla odpowiedzi № 1Użyłem poniżej fragmentu kodu. Deserializuje plik .trdx, a następnie tworzy z niego instancję Report (Telerik.Reporting.Report). To wystąpienie raportu można następnie przekonwertować na plik pdf.
System.Xml.XmlReaderSettings settings = new System.Xml.XmlReaderSettings();
settings.IgnoreWhitespace = true;
//read the .trdx file contents
using (System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(path_to your_trdx_file, settings))
{
Telerik.Reporting.XmlSerialization.ReportXmlSerializer xmlSerializer =
new Telerik.Reporting.XmlSerialization.ReportXmlSerializer();
//deserialize the .trdx report XML contents
Telerik.Reporting.Report report = (Telerik.Reporting.Report)
xmlSerializer.Deserialize(xmlReader);
string mimType = string.Empty;
string extension = string.Empty;
Encoding encoding = null;
// call Render() and retrieve raw array of bytes
// write the pdf file
byte[] buffer = Telerik.Reporting.Processing.ReportProcessor.Render(
"PDF", report, null, out mimType, out extension, out encoding);
// create a new file on disk and write the byte array to the file
FileStream fs = new FileStream(Path_you_need_to_save_the_pdf_file, FileMode.Create);
fs.Write(buffer, 0, buffer.Length);
fs.Flush();
fs.Close();
}
0 dla odpowiedzi nr 2
Jeśli użytkownik Telerik raportuje 2012 lub nowszy, powyższy kod należy zmienić na ten
enter code here
Ustawienia XmlReaderSettings = nowe XmlReaderSettings (); settings.IgnoreWhitespace = true;
//read the .trdx file contents
using (
XmlReader xmlReader =
XmlReader.Create(you trdx file path,
settings))
{
ReportXmlSerializer xmlSerializer =
new ReportXmlSerializer();
//deserialize the .trdx report XML contents
Report report = (Report)xmlSerializer.Deserialize(xmlReader);
Telerik.Reporting.InstanceReportSource instanceReportSource = new Telerik.Reporting.InstanceReportSource
{
ReportDocument = report
};
string mimType = string.Empty;
string extension = string.Empty;
//Encoding encoding = null;
// call Render() and retrieve raw array of bytes
// write the pdf file
ReportProcessor reportProcessor = new ReportProcessor();
RenderingResult renderingResult = reportProcessor.RenderReport("DOCX", instanceReportSource, null);
// create a new file on disk and write the byte array to the file
FileStream fs = new FileStream(@"D:testDashboard.DOCX", FileMode.Create);
fs.Write(renderingResult.DocumentBytes, 0, renderingResult.DocumentBytes.Length);
fs.Flush();
fs.Close();
}