Manage Report Printing and Exporting

This example shows how to perform the required actions before printing or exporting the report in the viewer.

First you need to add the StiMvcViewer component to the view page. Also you need to pass the StiMvcViewerOptions object to the constructor. In the options you should set the next actions: GetReport, PrintReport, ExportReport and ViewerEvent. The last two actions will be called accordingly at report printing and exporting.

@using Stimulsoft.Report.Mvc;

...

@Html.Stimulsoft().StiMvcViewer(new StiMvcViewerOptions()
{
Actions =
{
GetReport = "GetReport",
PrintReport = "PrintReport",
ExportReport = "ExportReport",
ViewerEvent = "ViewerEvent"
}
})


In the options above we define several actions, and we need to add it in the controller.

The GetReport action loads the report and returns the answer to the client part of the viewer using the GetReportResult() static method. In the parameters of this method, the report object should be passed.

public ActionResult GetReport()
{
// Create the report object
StiReport report = new StiReport();
report.Load(Server.MapPath("~/Content/Reports/TwoSimpleLists.mrt"));

// Load data from XML file for report template
DataSet data = new DataSet("Demo");
data.ReadXml(Server.MapPath("~/Content/Data/Demo.xml"));

report.RegData(data);

return StiMvcViewer.GetReportResult(report);
}


The ViewerEvent action handles all the viewer events (switching pages, zooming, etc.) and returns the answer to the client using the ViewerEventResult() static method.

public ActionResult ViewerEvent()
{
return StiMvcViewer.ViewerEventResult();
}


The PrintReport action will be invoked when you print the report through menu of the viewer. In this action, you can get the report object and perform any action, for example connect to data. To prepare the answer for the client you should use the PrintReportResult() static method.

public ActionResult PrintReport()
{
StiReport report = StiMvcViewer.GetReportObject();

// Some actions with report when printing

return StiMvcViewer.PrintReportResult(report);
}


The ExportEvent action will be invoked when you export the report in any format through menu of the viewer. You can get the report object and perform any action. Also you can get the action parameters of the viewer and, for example, perform some action at the PDF report export. To prepare the answer for the client, you should use the ExportReportResult() static method.

public ActionResult ExportReport()
{
StiReport report = StiMvcViewer.GetReportObject();
StiRequestParams parameters = StiMvcViewer.GetRequestParams();

if (parameters.ExportFormat == StiExportFormat.Pdf)
{
// Some actions with report when exporting to PDF
}

return StiMvcViewer.ExportReportResult(report);
}


In the screenshot below you can see the result of the sample code.

Manage Report Printing and Exporting