原創|使用教程|編輯:郝浩|2013-08-05 11:32:32.000|閱讀 742 次
概述:ActiveReports 7中包括了一個Silverlight報表查看器,但我們今天討論的重點在于很多Silverlight報表開發者最關心的老大難問題——打印。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
報表開發工具ActiveReports 7中包括了一個Silverlight報表查看器,雖然Silverlight近兩年并沒有太流行了,但我們今天討論的重點在于很多Silverlight報表開發者最關心的老大難問題——打印。設計和創建報表的最終目的肯定不局限于報表查看體驗,最終用戶無一例外的還需要有效的報表打印。
在ActiveReports的Silverlight報表查看器得工具欄上就提供了“打印”按鈕來滿足這一需求。但是對于內容較多的復雜報表的打印,我們一般推薦使用PDF打印選項來完成。雖然ActiveReports支持原生的Silverlight API打印,但是為了減小后臺打印的大小,保證打印的穩定性,保證報表內條碼打印的質量,還是建議使用PDF打印來代替默認的Silverlight打印。
使用PDF打印的方法一般是在Silverlight項目,直接將報表轉換為PDF格式然后再打印。在ActiveReports中設置好PDF打印后,單擊Silverlight報表查看器得工具欄上的“打印”按鈕就會出現如下對話框:
設置PdfPrint的方法很簡單,在Silverlight報表查看器的Web.config文件中添加以下XML代碼就行了:
<httpHandlers> <add verb="*" path="*.ar7" type="GrapeCity.ActiveReports.Web.Handlers.ReportBinariesStreamer, GrapeCity.ActiveReports.Web.v7, Version=7.0.xxxx.0, Culture=neutral, PublicKeyToken=cc4967777c49a3ff" /> <add verb="*" path="*.ar7Web" type="GrapeCity.ActiveReports.Web.Handlers.WebCacheAccessHandler, GrapeCity.ActiveReports.Web.v7, Version=7.0.xxxx.0, Culture=neutral, PublicKeyToken=cc4967777c49a3ff" /> <add verb="*" path="*.ActiveReport" type="GrapeCity.ActiveReports.Web.Handlers.CompiledReportHandler, GrapeCity.ActiveReports.Web.v7, Version=7.0.xxxx.0, Culture=neutral, PublicKeyToken=cc4967777c49a3ff" /> <add verb="*" path="*.rpx" type="GrapeCity.ActiveReports.Web.Handlers.RpxHandler, GrapeCity.ActiveReports.Web.v7, Version=7.0.xxxx.0, Culture=neutral, PublicKeyToken=cc4967777c49a3ff" /> <add verb="*" path="*.rdl,*.rdlx" type="GrapeCity.ActiveReports.Web.Handlers.RdlxHandler, GrapeCity.ActiveReports.Web.v7, Version=7.0.xxxx.0, Culture=neutral, PublicKeyToken=cc4967777c49a3ff" /> <remove verb="*" path="*.asmx" /> <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> </httpHandlers>
但是最近我們收到不少反饋說,在調用LoadDocument方法將報表加載到Silverlight報表查看器后,PdfPrint功能不起作用。
眾所周知,加載報表最常用的方法是LoadFromService和LoadDocument。經過我們測試,LoadFromService由于帶有一個報表文件路徑的字符串,所以PdfPrint完全正常工作,所以問題明顯出在LoadDocument上,由于使用LoadDocument加載的報表不帶文件路徑,讓Silverlight報表查看器不知道報表在服務器端的具體位置,導致了PDF打印無法完成。我們最后想到一個變通的方法來解決這一問題。
1、為運行報表和生成ID添加一個Service
2、添加ReportCache和IHttpHandler接口(Report.ashx)。這個主要用來緩存報表和獲取rdf和準備打印的pdf的。
public class Report : IHttpHandler { public void ProcessRequest(HttpContext context) { string id = context.Request.Params["id"]; if (string.IsNullOrEmpty(id)) { ThrowException(context.Response); return; } SectionDocument sd = ReportCache.GetSectionDocument(id); if (sd == null) { ThrowException(context.Response); return; } string type = context.Request.Params["type"]; if (string.IsNullOrEmpty(type)) { type = "rdf"; } type = type.ToLower(); switch (type) { case "pdf": PdfExport pdfExport = new PdfExport(); string print = context.Request.Params["print"]; if (print == null) { print = "false"; } print = print.ToLower(); if (print != "false") { pdfExport.Options.OnlyForPrint = true; } using (var outStream = new MemoryStream()) { // Put output into the stream pdfExport.Export(sd, outStream); // send the bits context.Response.Clear(); context.Response.ContentType = "application/pdf"; context.Response.AddHeader("content-disposition", "inline; filename=ActiveReports.PDF"); context.Response.BinaryWrite(outStream.ToArray()); } break; case "rdf": default: using (MemoryStream outStream = new MemoryStream()) { // Put output into the stream sd.Save(outStream); // send the bits context.Response.Clear(); context.Response.BinaryWrite(outStream.ToArray()); } break; } } private void ThrowException(HttpResponse response) { response.ContentType = "text/html"; response.Write("The specified report was not found on the server"); response.Flush(); } public bool IsReusable { get { return false; } } }
3、自定義Silverlight報表查看器
然后調用LoadDocument方法加載的報表就能正常的使用PDF打印功能了。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件網