Render PDF Reports in .Net Web or Winforms

One thing to note – render is not the same as draw. This article assumes you are using purely .Net for reporting purposes. If you are using C1, Telerik, Crystal Reports or some others, these examples are not included, although I can at least tell you it definitely possible to do the same in all of these.

Web:
(converted from VB asp.net blog reference below to C#)

//this would return the .rdl report to a pdf and prompt the user for a download in their browser
Warning[] warnings = null;
string[] streamids = null;
string mimeType = null;
string encoding = null;
string extension = null;
byte[] bytes = null;
bytes = ReportViewer1.ServerReport.Render("PDF", null, mimeType, encoding, extension, streamids, warnings);
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);
Response.ContentType = "Application/pdf";
Response.BinaryWrite(ms.ToArray());
Response.End();

In Winforms your syntax would be similar, except instead of writing to buffer and prompting user for download, you would probably prompt for a path to save first and then write to this location using File IO.

References
MSDN (ReportViewer WinForms), http://msdn.microsoft.com/en-us/library/aa337089.aspx
ASP.net (blogs), http://weblogs.asp.net/kyleholder/archive/2008/03/05/render-the-contents-of-a-reportviewer-control-directly-to-pdf.aspx

About Ronnie Diaz

Ronnie Diaz is a software engineer and tech consultant. Ronnie started his career in front-end and back-end development for companies in ecommerce, service industries and remote education. This work transitioned from traditional desktop client-server applications through early cloud development. Software included human resource management and service technician workflows, online retail e-commerce and electronic ordering and fulfillment, IVR customer relational systems, and video streaming remote learning SCORM web applications. Hands on server experience and software performance optimization led to creation of a startup business focused on collocated data center services and continued experience with video streaming hardware and software. This led to a career in Amazon Prime Video where Ronnie is currently employed, building software and systems which stream live sports and events for millions of viewers around the world.

Posted on May 17, 2011, in Programming & Development and tagged , , , , . Bookmark the permalink. 1 Comment.

  1. Nice site, nice and easy on the eyes and great content too.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: