0
votes

The following code resides inside a method and it uses the report viewer to render reports:

            objReportViewer.LocalReport.ReportPath = "TestReport.rdlc";

            objReportViewer.LocalReport.DataSources.Clear();

            objReportViewer.LocalReport.DisplayName = "TestReport_" +  DateTime.Now.ToString("yyyyMMdd");

            object objReportDataSource = TestReportDataSource(Id);

            objReportViewer.LocalReport.DataSources.Add(new ReportDataSource("TestReportDataSource", objReportDataSource));

            objReportViewer.ZoomMode = ZoomMode.PageWidth;


            ReportParameter[] arrReportParameters = new ReportParameter[3];

            // First Name
            arrReportParameters[0] = new ReportParameter("FirstName",person.FirstName);

            // LastName
            arrReportParameters[1] = new ReportParameter("LastName", person.LastName);

 //DOB
   arrReportParameters[2] = new ReportParameter("Age",person.DOB);


            objReportViewer.ShowParameterPrompts = false;
            objReportViewer.DocumentMapCollapsed = true;
            objReportViewer.ShowDocumentMapButton = false;


            objReportViewer.LocalReport.SetParameters(arrReportParameters);

   objReportViewer.LocalReport.ExecuteReportInSandboxAppDomain();

            objReportViewer.LocalReport.Refresh();

So far no issues when trying to generate the reports, if there is a lot of information then the report will have two or more pages. If I want to print or export to .pdf I can do so from the rendered report. My questions are two:

1- How can I make the report viewer export to .pdf programmatically.

2- How can I make the report viewer show only 1 page ignoring the rest of the data, or in case of exporting it to .pdf programmatically how can I export only the first page ignoring the rest.

For example if a report would normally render 2 pages I want it to render only the first one.

Is it possible to accomplish using the report viewer?

Thank you.

1

1 Answers

1
votes

I'm not sure how to limit the pdf to one page. But here is how you export to pdf:

 Microsoft.Reporting.WebForms.LocalReport oLocalReport = objReportViewer.LocalReport;


byte[] renderedBytes = null;
string reportType = "PDF";
string mimeType = "application/pdf";
string encoding = null;
Microsoft.Reporting.WebForms.Warning[] warnings = null;
string[] streams = null;
string deviceInfo = "<DeviceInfo><OutputFormat>PDF</OutputFormat><PageWidth>8.5in</PageWidth><PageHeight>11in</PageHeight></DeviceInfo>";


//Render the report
renderedBytes = oLocalReport.Render(reportType, deviceInfo, mimeType, encoding, "PDF", streams, warnings);

System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ContentType = mimeType;

System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + _reportName + ".PDF");
System.Web.HttpContext.Current.Response.BinaryWrite(renderedBytes);
System.Web.HttpContext.Current.Response.End();