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

Advertisements

About Ronnie Diaz

Ronnie Diaz is an enterprise software engineer responsible for front-end and back-end development for companies in many industries. Heavily involved in cloud development, online retail, e-commerce and electronic ordering, fulfillment and customer relational systems.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: