Search code examples
winformspdfvisual-studio-2012reportviewerfilestream

Diplay Converted reportview Report


I am trying to display a report that is converted to a PDF. I have found a bit of code that will display the PDF but it needs to be stored on disk. Is there a way to store the PDF locally temporally so it can be called by a reader?

Here is the code that currently is for the print button.

namespace Dispatch311.Views

/// <summary>
/// Interaction logic for PrintDialog.xaml
/// </summary>
public partial class PrintDialog : Window
{
    public PrintDialog()
    {
        InitializeComponent();                       
    }
    public void DisplayReport(int eventID)
    {
        Warning[] warnings;
        string[] streamids;
        string mimeType;
        string encoding;
        string filenameExtention;

        reportViewer.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Remote;
        ServerReport serverReport = reportViewer.ServerReport;
        reportViewer.ShowParameterPrompts = false;
        serverReport.ReportServerUrl = new Uri("http://sql2008test/reportserver");
        serverReport.ReportPath = "/311Reports/311SingleEvent";
        ReportParameter ID = new ReportParameter();
        ID.Name = "ID";
        ID.Values.Add(eventID.ToString());
        reportViewer.ServerReport.SetParameters(
            new ReportParameter[] { ID });

        byte[] bytes = reportViewer.ServerReport.Render(
            "PDF", null, out mimeType, out encoding, out filenameExtention,
            out streamids, out warnings);

        using (FileStream fs = new FileStream("EventPDF.pdf", FileMode.Create))
        {
            fs.Write(bytes, 0, bytes.Length);
        }

        reportViewer.RefreshReport();
    }


}

Solution

  • Fixed the end of my code so it looks like this:

    byte[] bytes = reportViewer.ServerReport.Render("PDF",
                null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);
            using (FileStream fs = new FileStream("311Event.pdf", FileMode.Create))
            {
                fs.Write(bytes, 0, bytes.Length);
            }
            string fileName = "311Event.pdf";
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.FileName = fileName;
            process.Start();
            process.WaitForExit();
    

    This makes it so Adobe reader pops up instead of reportviewer and returns to the application when the user exits the reader.