Use RSClientPrint control in a custom application

On www.codeproject.com, there is an article that in depth describes how to use RSClientPrint control in a custom application. Here I don't want to give those details again. Please visit : http://www.codeproject.com/KB/reporting-services/RsClientPrint.aspx

 

My purpose to write this article is that I want to share how to use RSClientPrint control to print a report which is rendered by Reporting Services WebService.

 

1. Add RSClientPrint.cab as a reference in your application.

You can find out it under the folder where Reporting Services is installed.

 

2. Embed RSClientPrint control on your form. See folloing HTML scripts:
<object id="RSClientPrint" classid="CLSID:5554DCB0-700B-498D-9B58-4E40E5814405" codebase="/cab/RSClientPrint-x86.cab#version=2007,100,1600,22" style="display:none;"></object>

 

3. Add a button on your form.
Html codes:
<img align="absmiddle" src="/images/printer.gif" title="Print" style="cursor:pointer" οnclick="js_printReport(event)" />
Javascript codes:
function js_printReport(evt) {
    var frmElem2 = document.getElementById("ReportRunFrm");
    var printElem = document.getElementById("RSClientPrint");
    printElem.Authenticate = false;
    printElem.MarginLeft = 10;
    printElem.MarginTop = 10;
    printElem.MarginRight = 10;
    printElem.MarginBottom = 10;
    printElem.PageHeight = 297;
    printElem.PageWidth = 210;
    printElem.Culture = 1033;
    printElem.UICulture = 9;
    printElem.UseEmfPlus = true;
    printElem.Print("PrintReport.aspx", "txtExecutionID=" + frmElem2.contentWindow.rptExecutionID, "");
}

 

note:
Used for Reporting Services: RSClientPrint.Print(ServerPath, ReportPathParameters, ReportName);
Used for Custom Applications: RSClientPrint.Print(FilePath, Parameters, ReportName);

how to get the rptExecutionID, please refer to my previous article named "利用ReportExecutionService.FindString实现查询功能"

4. Desigin PrintReport.aspx file

How does RSClientPrint work?
When rendering the reports using the EMF format, the RsClientPrint instruct the Reporting Service to persist the rendered report stream using the URL command rs:PresistStream=true. This command will force Reporting Service to render the first page and return the result stream to the caller directly. To get the next page, RsClientPrint will replace the rs:PresistStream command with rs:GetNextStream=true. The Reporting Service will render the next page and return its stream to the caller. When there are no more pages, Reporting Service will return an empty stream.


RsClientPrint will read the result EMF streams and send it to the selected client printer or prepare it for preview.

 

Back-end codes of PrintReport.aspx:
public partial class PrintReport : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        String strExecutionID = (Request["txtExecutionID"] == null ? "" : Request["txtExecutionID"]);
        Boolean rsGetNextStream = (Request.QueryString["rs:GetNextStream"] == null ? false : Convert.ToBoolean(Request.QueryString["rs:GetNextStream"].ToString()));
        String strHost = ConfigurationManager.AppSettings["AdHocReportServerUrl"];
        String strRSUrl = ConfigurationManager.AppSettings["RogersRS.ReportService2005"];
        String strRESUrl = ConfigurationManager.AppSettings["RogersRS.ReportExecution2005"];
        String strFormat = "IMAGE";

        //retrieve webservices
        ReportExecutionService res = new ReportExecutionService();
        res.Credentials = System.Net.CredentialCache.DefaultCredentials;
        res.Url = strRESUrl;
        res.Timeout = System.Threading.Timeout.Infinite;
        String strDevInfo = "";
        String strExtension;
        String strEncoding;
        String strMimeType;
        Warning[] warnings = null;
        String[] strStreamIDs = null;
        ExecutionInfo execInfo = new ExecutionInfo();

        //flush any pending response
        Response.Clear();
        Response.ClearHeaders();
        Response.ClearContent();
        Response.BufferOutput = true;

        try
        {
            ExecutionHeader execHeader = new ExecutionHeader();
            execHeader.ExecutionID = strExecutionID;
            res.ExecutionHeaderValue = execHeader;

            Byte[] rptRefined = null;
            int nPageIndex = 1;

            //get the page number of current report circularly
            //if rsGetNextStream is false, get the first page
            //if rsGetNextStream is true, get next page

            //when nPageIndex exceeds the total page of current report, it will throw an exception and return an empty stream to the caller.

            if (Session[strExecutionID]!=null && rsGetNextStream)
            {
                nPageIndex = (int)Session[strExecutionID];
            }
            else
            {
                nPageIndex = 1;
            }            

            Session[strExecutionID] = (nPageIndex ++);

            strDevInfo = "<DeviceInfo>" +
                 "<StartPage>" + nPageIndex.ToString() + "</StartPage>" +
                 "<OutputFormat>EMF</OutputFormat>" +
                 "</DeviceInfo>";

            //render the report
            rptRefined = res.Render(strFormat, strDevInfo, out strExtension, out strMimeType, out strEncoding, out warnings, out strStreamIDs);

            Response.ContentType = strMimeType;
            Response.AddHeader("content-disposition", "attachment; filename=Ad-Hoc-Report." + strExtension);

            Response.OutputStream.Write(rptRefined, 0, rptRefined.Length);
        }
        catch (Exception ex)
        {
            endPrintProcess();
        }
       
        Response.Flush();
        Response.End();
    }

    private void endPrintProcess()
    {
       
        Response.ContentType = "image/EMF";
        Response.AddHeader("content-disposition", "attachment; filename=Ad-Hoc-Report_Err.emf");
       
    }
}

 

Note:
If you application disable Session (for example, Reporting Service website does it), the following scripts is invalid.
            if (Session[strExecutionID]!=null && rsGetNextStream)
            {
                nPageIndex = (int)Session[strExecutionID];
            }
            else
            {
                nPageIndex = 1;
            }            

            Session[strExecutionID] = (nPageIndex ++);
To achieve the same functionality, you can create a table in your database with ExecutionID and PageNumber fields. This table stores a increased page number by ExecutionID. You just need to change the apprach to retrieve the PageNumber from Session to a database.

 

5. RSClientPrint's expansibility
In conclusion, RSClientPrint is not limited to Reporting Services and .Net environment. If the Browser supports ActiveX control and your back-end codes can output a stream in EMF, you then can use this control.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值