如何将使用Telerik Reporting(.trdx)创建的报告导出为pdf

本文关键字:报告 创建 pdf trdx Telerik Reporting | 更新日期: 2023-09-27 18:01:08

我使用Telerik Report Designer(Standalone(设计报告(.trdx(,如何在C#代码中以编程方式将其导出到pdf文件?

如何将使用Telerik Reporting(.trdx)创建的报告导出为pdf

我使用了下面的代码片段。它反序列化.trdx文件,然后从中创建一个Report(Telerik.Reporting.Report(实例。然后可以将该报表实例转换为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();
            }

如果您使用Telerik reporting 2012或更新版本,则需要将上述代码更改为该

enter code here

XmlReaderSettings设置=新的XmlReaderSettings((;设置。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:'test'Dashboard.DOCX", FileMode.Create);
            fs.Write(renderingResult.DocumentBytes, 0, renderingResult.DocumentBytes.Length);
            fs.Flush();
            fs.Close();
        }