Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display a PDF from Reporting Services

I would like to display a PDF generated from Reporting Services from my WinForms app.

I tried the following:

Uri uri = new Uri("http://myReportServer?MyReport&rs%3aCommand=Render&rs:Format=pdf");
System.Diagnostics.Process.Start(uri.ToString());

Which launches a browser, which then in turn prompts me to open or save this file.

Ideally I would like to display only the file, either in the browser or in a PDF viewer. Problem is I have to open both the browser and then PDF viewer, which the users doesn't want.

Is there a simple way to do this using just the URL?

My other alternative is to just write some C# code which seems straight forward. There are some examples here:

http://geekswithblogs.net/bsherwin/archive/2007/04/29/112094.aspx

and here:

http://www.codeproject.com/KB/reporting-services/PDFUsingSQLRepServices.aspx

like image 479
openshac Avatar asked Oct 12 '22 09:10

openshac


1 Answers

You can download PDF to disk and then use Process.Start to show it.

Take a look at this example:

        Uri uriDownload = new Uri("http://myReportServer?MyReport&rs%3aCommand=Render&rs:Format=pdf");
        string strSavePath = @"C:\test\test123.pdf";

        System.Net.WebClient wcli = new System.Net.WebClient();
        wcli.DownloadFile(uriDownload, strSavePath);
        System.Diagnostics.Process.Start(strSavePath);

UPDATE:

If that does not work by default, try to add this before wcli.DownloadFile():

        wcli.Credentials = new NetworkCredential("username", "password", "domain");
        wcli.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
like image 69
HABJAN Avatar answered Oct 15 '22 09:10

HABJAN