This is a migrated thread and some comments may be shown as answers.

Exporting a PDF Programmatically to a File from the WinForms Report Viewer

1 Answer 766 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
msulis
Top achievements
Rank 1
msulis asked on 30 Nov 2009, 02:41 AM

There's a lot of examples centered around the WebForms reportviewer, and which use the ReportProcessor independently. But I've got some complex reports that I've extended which require programmatic manipulation to process, and it seems that there's no examples available for how to generate those through the ReportProcessor. And as I found, calling ExportReport() simply pops up the export dialog in the UI.

So here's a nice concept to have in your toolbox - it's simple, but it eluded me for a bit so I thought I'd share it.

Assuming you have a ReportViewer in a windows form (or a RadForm), and you've already rendered the report there, you can simply grab the report object from the report viewer and pass it to the ReportProcessor like this:

    private void SaveReport(string fileLocation) { 
      ReportProcessor reportProcessor = new ReportProcessor(); 
      //pass the reportViewer1's "Report" object to the RenderReport method 
      //populate the HashTable if you need to specify DeviceInfo properties, otherwise just pass an empty one like I've done here 
      RenderingResult renderingResult = reportProcessor.RenderReport("PDF", reportViewer1.Report, new Hashtable()); 
      //now just create a filestream and write the DocumentBytes array to it - flush and close, and your file is ready. 
      using (FileStream destinationFileStream = new FileStream(fileLocation, FileMode.Create, FileAccess.Write)) { 
        destinationFileStream.Write(renderingResult.DocumentBytes, 0, renderingResult.DocumentBytes.GetLength(0)); 
        destinationFileStream.Flush(); 
        destinationFileStream.Close(); 
      } 
    } 

There's a sample project attached, but that little routine is the basis of it.

1 Answer, 1 is accepted

Sort by
0
Steve
Telerik team
answered on 30 Nov 2009, 08:01 AM
Hi msulis,

Thank you for your submission to our code library base. However the sample you provided shows basic usage of the report API that has already been covered in the API documentation and even a standalone help topic: Exporting a Report Programmatically. Thus we consider that it does not match the criteria to be part of the product code library, as its main purpose is to provide a way for our clients to share their interesting solutions, discuss coding techniques with fellow Telerik users, share experience on the Telerik suite of controls.
Nevertheless we have transferred your submission to a forum thread, so that other community members can still benefit from the provided sample.

All the best,
Steve
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
General Discussions
Asked by
msulis
Top achievements
Rank 1
Answers by
Steve
Telerik team
Share this question
or