How to make report viewer in asp net open in pdf direct

Introduction

ASP.NET is a popular language used for developing web applications. One common requirement in web applications is to generate and reports. In this article, we will explore how to make a report in ASP.NET open in PDF format directly.

Using the ReportViewer Control

The ReportViewer control is a powerful tool provided by ASP.NET for displaying reports. By default, the ReportViewer control renders the report in HTML format. However, we can configure it to open the report directly in PDF format.

To achieve this, we need to set the Response.ContentType property to “/pdf” and the Response.AddHeader method to specify the content disposition as “attachment”. This will force the to the report as a PDF file.


protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ReportViewer1.LocalReport.Refresh();
        byte[] reportBytes = ReportViewer1.LocalReport.Render("PDF");
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=report.pdf");
        Response.BinaryWrite(reportBytes);
        Response.End();
    }
}

In the above code, we first refresh the ReportViewer control to that the latest data is displayed in the report. Then, we render the report in PDF format using the Render method of the LocalReport object. The rendered report is stored in a byte array.

Next, we set the ContentType property of the Response object to “application/pdf” to indicate that the response is a PDF file. We also use the AddHeader method to specify the content disposition as “attachment” and provide a filename for the downloaded file.

Finally, we write the byte array to the response stream using the BinaryWrite method and end the response using the End method.

Using the ReportViewer WebForms Control

If you are using the ReportViewer control in a WebForms application, you can achieve the same result by handling the AsyncRendering event of the control.


protected void ReportViewer1_AsyncRendering(object sender, EventArgs e)
{
    ReportViewer1.LocalReport.Refresh();
    byte[] reportBytes = ReportViewer1.LocalReport.Render("PDF");
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=report.pdf");
    Response.BinaryWrite(reportBytes);
    Response.End();
}

In the above code, we handle the AsyncRendering event of the ReportViewer control. the event handler, we follow the same steps as mentioned earlier to render and download the report in PDF format.

Conclusion

In this article, we have explored how to make a report viewer in ASP.NET open in PDF format directly. By setting the appropriate response and content type, we can force the browser to download the report as a PDF file. This can be achieved using the ReportViewer control or by handling the AsyncRendering event in a WebForms application.

By following steps, you can enhance the user experience and provide a seamless way for users to view and download reports in PDF format.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents