Silent Print C#

1 Answer 23 Views
Getting started with ASP.NET
Telesharp
Top achievements
Rank 1
Telesharp asked on 04 Apr 2022, 01:46 PM | edited on 04 Apr 2022, 07:26 PM

I'm new to the development area and I'm doing some tests with telerik.
my problem is as follows
at the moment my application already generates the CSV file and I already have the report layout ready, I would like some path in C# to load this

my pdf is generated without the data source , how do I solve this ?

 



{

            var csvDataSource = new CsvDataSource();
            csvDataSource.Source = new Uri(@"C:\Users\victord.lima\Desktop\Exemplos\31teste.csv");
            csvDataSource.FieldSeparators = new[] { ',' };
            csvDataSource.RecordSeparators = new[] { '\r', '\n' };
            csvDataSource.HasHeaders = true;
            csvDataSource.EscapeFormat = CsvEscapeFormat.Quotes;
            csvDataSource.Quote = '"';



            Telerik.Reporting.Report report = new Telerik.Reporting.Report();
            report.DataSource = csvDataSource;

            System.Collections.Hashtable deviceInfo = new System.Collections.Hashtable();
            UriReportSource reportSource = new UriReportSource();
            reportSource.Uri = @"C:\Users\victord.lima\Desktop\Exemplos\TermoDeliberacaoDeCargaDeRecebimento.trdx";
            Telerik.Reporting.Processing.ReportProcessor reportProcessor = new Telerik.Reporting.Processing.ReportProcessor();
            Telerik.Reporting.Processing.RenderingResult result = reportProcessor.RenderReport("PDF", reportSource, deviceInfo);

            string fileName = result.DocumentName + "b." + result.Extension;
            string path = System.IO.Path.GetTempPath();
            string filePath = System.IO.Path.Combine(path, fileName);

            using (System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
            {
                fs.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);
            }

                           

1 Answer, 1 is accepted

Sort by
0
Accepted
Dimitar
Telerik team
answered on 07 Apr 2022, 09:14 AM

Hello Victor,

Thank you for the provided code snippet!

The problem is that the csvDataSource is set to the DataSource of a new report and not the data source of the TermoDeliberacaoDeCargaDeRecebimento report. The reportProcessor renders the empty report and that is why the rendered PDF document has no data in it:

            UriReportSource reportSource = new UriReportSource();
            reportSource.Uri = @"C:\Users\victord.lima\Desktop\Exemplos\TermoDeliberacaoDeCargaDeRecebimento.trdx";
            Telerik.Reporting.Processing.ReportProcessor reportProcessor = new Telerik.Reporting.Processing.ReportProcessor();
            Telerik.Reporting.Processing.RenderingResult result = reportProcessor.RenderReport("PDF", reportSource, deviceInfo);

In order for the CSV data to be assigned to the real report, that report should first be deserialized into a Report object, then you can set the csvDataSource as the DataSource of the deserialized report. Finally, the modified report object should be wrapped in an InstanceReportSource and rendered through the report processor. For example:

      

var csvDataSource = new CsvDataSource(); csvDataSource.Source = new Uri(@"C:\Users\victord.lima\Desktop\Exemplos\31teste.csv"); csvDataSource.FieldSeparators = new[] { ',' }; csvDataSource.RecordSeparators = new[] { '\r', '\n' }; csvDataSource.HasHeaders = true; csvDataSource.EscapeFormat = CsvEscapeFormat.Quotes; csvDataSource.Quote = '"'; var uri = @"C:\Users\victord.lima\Desktop\Exemplos\TermoDeliberacaoDeCargaDeRecebimento.trdx"; Telerik.Reporting.Report report; System.Xml.XmlReaderSettings settings = new System.Xml.XmlReaderSettings(); settings.IgnoreWhitespace = true;

// deserialize

using (System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(uri, settings)) { Telerik.Reporting.XmlSerialization.ReportXmlSerializer xmlSerializer = new Telerik.Reporting.XmlSerialization.ReportXmlSerializer(); report = (Telerik.Reporting.Report) xmlSerializer.Deserialize(xmlReader); } // update data source report.DataSource = csvDataSource;

// create new instance report source var irs = new InstanceReportSource() { ReportDocument = report }; System.Collections.Hashtable deviceInfo = new System.Collections.Hashtable(); Telerik.Reporting.Processing.ReportProcessor reportProcessor = new Telerik.Reporting.Processing.ReportProcessor();

//use the instance report source for the render Telerik.Reporting.Processing.RenderingResult result = reportProcessor.RenderReport("PDF", irs, deviceInfo);

Please test this approach and let me know if you run into any other issues.

Regards,
Dimitar
Progress Telerik

Brand new Telerik Reporting course in Virtual Classroom - the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products. Check it out at https://learn.telerik.com/.
Telesharp
Top achievements
Rank 1
commented on 07 Apr 2022, 11:24 AM

Thank you very much, it helped me a lot to understand the process!!
Tags
Getting started with ASP.NET
Asked by
Telesharp
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or