Sunday, February 14, 2010

Interaction between the Report and the Hosting application, Email Rendering Extension and more

I was pretty busy during past week and did not get the good ideas for the new post. Therefore I am going to write about two fairly new features of Data Dynamics Reports. They are Action event of Windows Forms Viewer and WebViewer and Email Rendering Extension. They are not well-documented yet, therefore this post will be probably helpful for someone.

Action event
Data Dynamics Reports has the great interactivity capabilities. It supports the hyperlinks, drill-through links, drill-down links, interactive sorting, bookmarks and the document maps. However, sometimes developer needs a custom type of interactivity. For instance - the report shows the list of employees, if a user clicks on the employee name in the report, then e-mail of employee is added in the internal list in the hosting application. A user selects a bunch of employees, closes the report and clicks "Send" button in the hosting application. The e-mail with the subject "You are fired" is sent to each employee from the list. Some time ago, it was not possible to implement such scenario using Data Dynamics Reports. But the fabulous product team went ahead and implemented the new feature that is called "Action event". Windows Forms Report Viewer and Web Report Viewer now have Action event. It is raised when a user clicks on the hyperlink, bookmark link or drill-through link in the report that is displayed in the viewer. In the handler of Action event developer can perform the custom steps and cancel the further action processing. Here is the sample of code:

public partial class _Default : System.Web.UI.Page
   internal List<string> _emailsList = new List<string>();

   protected void Page_Load(object sender, EventArgs e)
      WebReportViewer1.Action += WebReportViewer1_Action;

   void WebReportViewer1_Action(object sender, ActionEventArgs e)
      if(e.Action.ActionType == ActionType.HyperLink)
         e.Cancel = true;
       e.Cancel = false;

ActionEventArgs instance that is passed in Action event handler contains all the needed information: the type of Action and the value of the hyperlink, drill-through link or bookmark link. The most amazing fact about Action event is the uniform way it is implemented. It works for Windows Forms Viewer, Web Report Viewer-HTML mode and Web Report Viewer-PDF mode. In all cases developer just needs to add the handler for Action event.

Email Rendering Extension
Email Rendering Extension is the new rendering engine that allows to generate the report output which is compatible with the most popular e-mail clients. The clients with which we tested Email Rendering Extensions are:

  • Outlook 2007

  • Outlook 2003

  • Gmail

  • Yahoo

  • Hotmail

  • Thunderbird

Email Rendering Extension supports two modes - it can write the output to the file or memory stream and it can build the instance of System.Net.Mail.MailMessage which is available via the pubic property. The MailMessage instance that is generated by Email Rendering Extension contains the html view of the message and all the attachments, i.e. images, if any. Here is the sample of the code which sends the report output to the list of recipients, it continues the code that is shown above:

public partial class _Default : System.Web.UI.Page
   internal List<string> _emailsList = new List<string>();

   protected void btnSend_Click(object sender, EventArgs e)
      var re = new EmailRenderingExtension();
      var def = new ReportDefinition(new FileInfo(Server.MapPath("~/Email.rdlx")));
      var runtime = new ReportRuntime(def);
      runtime.Render(re, null);
      var emailMessage = re.MailMessage;
      emailMessage.Subject = "You are fired";
      foreach(string email in _emailsList)
      var fromAddress = new MailAddress("","Big Boss");
      emailMessage.Sender = fromAddress;
      emailMessage.From = fromAddress;
      var smtp = new SmtpClient("", 25);

In this code Email Rendering Extension does not render the report to the file or memory stream. MailMessage property is used instead.

Sunday, February 7, 2010

A few thoughts about no-touch printing in ActiveReports6

I am not sure what is the proper term for the functionality that allows to print a document with no user interaction. In this post I am using "no-touch printing" term. For instance: Every morning Jon Smith turns on his computer at the office and runs his favorite internet browser. The browser opens the start page which is the company web-site. The start page of the company web-site indicates that Jon Smith has logged in and prints the daily schedule on the default printer that is set up on Jon's computer. Let's see how we can implement such scenario using ActiveReports6.

ActiveX Viewer And Flash Viewer.
ActiveX Viewer was introduced in the COM version of the product. ActiveReports was growing up, .NET framework became the product environment, but ActiveX Viewer never was dead in the water, and with good reason! ActiveX Viewer provides developer with the rich capabilities of displaying and printing the reports on the web. For example it's pretty easy to implement no-touch printing on the web with ActiveX Viewer. The code should load the report in the viewer, then wait until the loading is finished and then call PrintReport method. The knowledge base of ActiveReports has several articles that show the sample of such code.
Of course the main disadvantage of ActiveX Viewer is it's ActiveX component. ActiveX components officially operate only with IE and Windows. In our days the usage share of IE is only ~60%. Developers need more uniform technology that allows displaying and printing reports on the web. In ActiveReports6 ActiveX viewer is not supported but Flash Viewer is introduced instead.
Flash Viewer operates with any browser and any OS where the flash player is installed.
The bad news are Flash Viewer does not support no-touch printing.
So, it seems that there are no ways to implement no-touch printing on the web with ActiveReports6? Not exactly!

Turn around.
To display PDF documents in browser Adobe Acrobat Reader includes the Acrobat ActiveX control for Internet Explorer and the Adobe PDF Plug-In For Firefox and Netscape. ActiveX control or FF plug-in can be hosted by the HTML page and a PDF document can be loaded and printed using Javascript or Visual Basic code of that page. I am confident that no-touch printing is possible to implement with Acrobat ActiveX control and I believe that it's possible with Adobe FF plug-in as well. In other hand ActiveReports6 includes the RPX handler which can return the report is saved in PDF format...No more words! Here is sample of code which performs no-touch printing of the report using Acrobat ActiveX control:

<body onload="window.setTimeout('em.PrintAll();', 5000);">
<div id="PdfContainer">
<span style="font-size: large;">Please wait for a few seconds and the printing will be starting.</span>
<embed id="em" width="0" height="0" src="HelloWorld.rpx?OutputFormat=PDF" />

The code uses EMBED tag to host the PDF document which is returned by RPX handler. PrintAll method prints the entire document without displaying a user dialog box. There are two issues with that code. First of all, it uses the timeout approach to wait until the document is loaded in PDF viewer. I think it's possible to intercept the event which is raised by PDF control when the document loading is completed, I just don't know the exact way to do that. The second issue is the warning which appears before printing starts:

Once "Don't show this message again" checkbox is unchecked, the code provides the no-touch printing, really.

Great Silver Hope
Eventually we will add the Silverlight-based Reports Viewer for ActiveReports. At the moment the most recent version of Silverlight is v.4 Beta 2 and it's new features list includes:
Comprehensive printing support enabling hardcopy reports and documents as well as a virtual print view, independent of screen content.
Unfortunately, that "comprehensive printing support" does not include no-touch printing. The great review of Silverligh 4 printing is available here.
Perhaps the final version of Silverlight 4 or 5 will support no-touch printing. Then we will add the Silverlight-based Reports Viewer and no-touch printing will be fully supported in ActiveReports. That's what I called "Great Silver Hope":)