在WEB系統中,列印的確是比較煩人的問題,如果我們能製作一個屬於自己的自定義的列印插件,那麼我們在後續自定義列印的時候能隨心所欲的控制列印,這樣的效果對於程式員來說是非常開心的一件事件,本文將自己開發編寫的C# 製作的HTML列印插件分享出來,讓有同樣需求的朋友提供一個參考;此插件是基於Micros ...
在WEB系統中,列印的確是比較煩人的問題,如果我們能製作一個屬於自己的自定義的列印插件,那麼我們在後續自定義列印的時候能隨心所欲的控制列印,這樣的效果對於程式員來說是非常開心的一件事件,本文將自己開發編寫的C# 製作的HTML列印插件分享出來,讓有同樣需求的朋友提供一個參考;此插件是基於Microsoft .NET Framework 2.0 開發的,缺點是每台客戶端在安裝插件時,必須要安裝Microsoft .NET Framework 2.0 ;本插件能實現 頁眉、頁腳、表頭、標題、表尾的分頁列印;支持紙張類型、自動補充空行等功能;由於技術有限,肯定有很多不足的地方,請批評指正!
由於本列印插件是基於我們開發平臺的報表基礎來開發設計的,所以列印控制項的原理:通過JS將頁面表格數據生成固定格式的XML字元串(圖片通過64base圖片格式)傳送給列印插件,有列印插件自主繪圖生成列印頁面。E_Print插件可以在WEB或WinForm中使用:
列印插件完整源碼:E_Print.rar (包含插件源碼、打包程式、winform調試DEMO)
下麵貼出源碼:(在源碼中有詳細的註釋說明)
1、PrintControl 列印插件類
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Drawing; 5 using System.Data; 6 using System.Text; 7 using System.Windows.Forms; 8 using System.Runtime.InteropServices; 9 using System.Drawing.Printing; 10 using System.Xml; 11 using System.Security; 12 using System.Drawing.Drawing2D; 13 using System.Drawing.Text; 14 using System.Text.RegularExpressions; 15 16 namespace E_Print 17 { 18 /// <summary> 19 /// 列印控制項 20 /// 實現IObjectSafety介面 21 /// 網頁上所有所使用到的GUID 通過Guid工具生成的唯一編碼 22 /// 74D1ED1D-B1A7-4039-A060-6F544FBE99EC 編碼以後不允許修改 23 /// </summary> 24 [Guid("74D1ED1D-B1A7-4039-A060-6F544FBE99EC"), ProgId("EReportPrint"), ComVisible(true)] 25 public partial class PrintControl : UserControl, IObjectSafety 26 { 27 #region 私有變數 28 29 #region 通用參數 30 31 /// <summary> 32 /// 縮放比例 33 /// </summary> 34 private float Zoom = 1; 35 36 /// <summary> 37 /// 網頁高度 像素 px 38 /// </summary> 39 private float HtmlHeight = 0; 40 41 /// <summary> 42 /// 網頁寬度 像素 px 43 /// </summary> 44 private float HtmlWidth = 0; 45 46 /// <summary> 47 /// 報表區域矩形 48 /// </summary> 49 private RectangleF TableRect = new RectangleF(); 50 51 /// <summary> 52 /// 報表繪製實例 53 /// </summary> 54 private ReportDraw RptDraw = new ReportDraw(); 55 56 #endregion 57 58 #region 頁邊距 59 60 /// <summary> 61 /// 左邊距 62 /// 毫米 mm(一位小數) 63 /// </summary> 64 private float _marginLeft = 9.9f; 65 66 /// <summary> 67 /// 右邊距 68 /// </summary> 69 private float _marginRight = 9.9f; 70 71 /// <summary> 72 /// 上邊距 73 /// </summary> 74 private float _marginTop = 9.9f; 75 76 /// <summary> 77 /// 下邊距 78 /// </summary> 79 private float _marginBottom = 9.9f; 80 81 #endregion 82 83 #region 版型方向 84 85 /// <summary> 86 /// 版型方向 Landscape: true 橫向;false 縱向 87 /// </summary> 88 private bool _landscape = false; 89 90 #endregion 91 92 #region 紙型大小 93 94 /// <summary> 95 /// 紙張類型 96 /// </summary> 97 private string _paperName = "A4"; 98 99 /// <summary> 100 /// 紙張寬度 101 /// </summary> 102 private int _paperWidth = 210; // 毫米 103 104 /// <summary> 105 /// 紙張高度 106 /// </summary> 107 private int _paperHeight = 297; // 毫米 108 109 #endregion 110 111 #region 列印參數 112 113 /// <summary> 114 /// 自適應紙張大小方法 115 /// null: 無 116 /// row: 橫向 117 /// col: 縱向 118 /// </summary> 119 private string _zoomType = "null"; 120 121 /// <summary> 122 /// 是否每頁列印標題 123 /// </summary> 124 private bool _isTblTitleAllPage = false; 125 126 /// <summary> 127 /// 是否每頁列印表頭 128 /// </summary> 129 private bool _isTblHeadAllPage = false; 130 131 /// <summary> 132 /// 是否每頁列印表尾 133 /// </summary> 134 private bool _isTblFootAllPage = false; 135 136 /// <summary> 137 /// 最後一頁自動補行 138 /// </summary> 139 private bool _isAutoFillRow = false; 140 141 /// <summary> 142 /// 字元溢出是否換行縮小處理方式 143 /// </summary> 144 private bool _isOverFlow = false; 145 146 /// <summary> 147 /// 列印數據 148 /// </summary> 149 private string _dataXml = ""; 150 151 #endregion 152 153 #region 頁眉參數 154 155 /// <summary> 156 /// 頁眉--繪製頁眉 157 /// </summary> 158 private bool _headDraw = false; 159 160 /// <summary> 161 /// 頁眉--高度 毫米 162 /// 預設 10 剛好 163 /// </summary> 164 private float _headHeight = 10.0f; 165 166 /// <summary> 167 /// 頁眉--左側文字 168 /// </summary> 169 private string _headLeft = ""; 170 171 /// <summary> 172 /// 頁眉--中間文字 173 /// </summary> 174 private string _headCenter = ""; 175 176 /// <summary> 177 /// 頁眉--右側文字 178 /// </summary> 179 private string _headRight = ""; 180 181 /// <summary> 182 /// 頁眉--字體名稱 183 /// </summary> 184 private string _headFontName = "宋體"; 185 186 /// <summary> 187 /// 頁眉--字體大小 188 /// </summary> 189 private string _headFontSize = "9pt"; 190 191 /// <summary> 192 /// 頁眉--字體顏色 193 /// </summary> 194 private string _headFontColor = "Black"; 195 196 /// <summary> 197 /// 頁眉--字體--粗體 198 /// </summary> 199 private bool _headFontBold = false; 200 201 /// <summary> 202 /// 頁眉--字體--斜體 203 /// </summary> 204 private bool _headFontItalic = false; 205 206 /// <summary> 207 /// 頁眉--字體--刪除線 208 /// </summary> 209 private bool _headFontStrikeout = false; 210 211 /// <summary> 212 /// 頁眉--字體--下劃線 213 /// </summary> 214 private bool _headFontUnderline = false; 215 216 /// <summary> 217 /// 頁眉--繪製分隔線 218 /// </summary> 219 private bool _headLineDraw = false; 220 221 /// <summary> 222 /// 頁眉--分隔線寬度 223 /// </summary> 224 private float _headLineWidth = 1.0f; 225 226 /// <summary> 227 /// 頁眉--分隔線線型 228 /// </summary> 229 private string _headLineDash = "solid"; 230 231 /// <summary> 232 /// 頁眉--分隔線顏色 233 /// </summary> 234 private string _headLineColor = "Black"; 235 236 #endregion 237 238 #region 頁腳參數 239 240 /// <summary> 241 /// 頁腳--繪製頁腳 242 /// </summary> 243 private bool _footDraw = false; 244 245 /// <summary> 246 /// 頁腳--高度 毫米 247 /// </summary> 248 private float _footHeight = 10.0f; 249 250 /// <summary> 251 /// 頁腳--左側文字 252 /// </summary> 253 private string _footLeft = ""; 254 255 /// <summary> 256 /// 頁腳--中間文字 257 /// </summary> 258 private string _footCenter = ""; 259 260 /// <summary> 261 /// 頁腳--右側文字 262 /// </summary> 263 private string _footRight = ""; 264 265 /// <summary> 266 /// 頁腳--字體名稱 267 /// </summary> 268 private string _footFontName = "宋體"; 269 270 /// <summary> 271 /// 頁腳--字體大小 272 /// </summary> 273 private string _footFontSize = "9pt"; 274 275 /// <summary> 276 /// 頁腳--字體顏色 277 /// </summary> 278 private string _footFontColor = "Black"; 279 280 /// <summary> 281 /// 頁腳--字體--粗體 282 /// </summary> 283 private bool _footFontBold = false; 284 285 /// <summary> 286 /// 頁腳--字體--斜體 287 /// </summary> 288 private bool _footFontItalic = false; 289 290 /// <summary> 291 /// 頁腳--字體--刪除線 292 /// </summary> 293 private bool _footFontStrikeout = false; 294 295 /// <summary> 296 /// 頁腳--字體--下劃線 297 /// </summary> 298 private bool _footFontUnderline = false; 299 300 /// <summary> 301 /// 頁腳--繪製分隔線 302 /// </summary> 303 private bool _footLineDraw = false; 304 305 /// <summary> 306 /// 頁腳--分隔線寬度 307 /// </summary> 308 private float _footLineWidth = 1.0f; 309 310 /// <summary> 311 /// 頁腳--分隔線線型 312 /// </summary> 313 private string _footLineDash = "solid"; 314 315 /// <summary> 316 /// 頁腳--分隔線顏色 317 /// </summary> 318 private string _footLineColor = "Black"; 319 320 #endregion 321 322 #endregion 323 324 #region 構造方法 325 326 /// <summary> 327 /// 列印控制項構造函數 328 /// </summary> 329 public PrintControl() 330 { 331 InitializeComponent(); 332 Init_PageSetting(); 333 } 334 335 #endregion 336 337 #region 介面實現 338 339 private const string _IID_IDispatch = "{00020400-0000-0000-C000-000000000046}"; 340 private const string _IID_IDispatchEx = "{a6ef9860-c720-11d0-9337-00a0c90dcaa9}"; 341 private const string _IID_IPersistStorage = "{0000010A-0000-0000-C000-000000000046}"; 342 private const string _IID_IPersistStream = "{00000109-0000-0000-C000-000000000046}"; 343 private const string _IID_IPersistPropertyBag = "{37D84F60-42CB-11CE-8135-00AA004BB851}"; 344 345 private const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001; 346 private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002; 347 private const int S_OK = 0; 348 private const int E_FAIL = unchecked((int)0x80004005); 349 private const int E_NOINTERFACE = unchecked((int)0x80004002); 350 351 private bool _fSafeForScripting = true; 352 private bool _fSafeForInitializing = true; 353 354 public int GetInterfaceSafetyOptions(ref Guid riid, ref int pdwSupportedOptions, ref int pdwEnabledOptions) 355 { 356 int Rslt = E_FAIL; 357 358 string strGUID = riid.ToString("B"); 359 pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA; 360 switch (strGUID) 361 { 362 case _IID_IDispatch: 363 case _IID_IDispatchEx: 364 Rslt = S_OK; 365 pdwEnabledOptions = 0; 366 if (_fSafeForScripting == true) 367 pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER; 368 break; 369 case _IID_IPersistStorage: 370 case _IID_IPersistStream: 371 case _IID_IPersistPropertyBag: 372 Rslt = S_OK; 373 pdwEnabledOptions = 0; 374 if (_fSafeForInitializing == true) 375 pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA; 376 break; 377 default: 378 Rslt = E_NOINTERFACE; 379 break; 380 } 381 382 return Rslt; 383 } 384 385 public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions) 386 { 387 int Rslt = E_FAIL; 388 string strGUID = riid.ToString("B"); 389 switch (strGUID) 390 { 391 case _IID_IDispatch: 392 case _IID_IDispatchEx: 393 if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_CALLER) && (_fSafeForScripting == true)) 394 Rslt = S_OK; 395 break; 396 case _IID_IPersistStorage: 397 case _IID_IPersistStream: 398 case _IID_IPersistPropertyBag: 399 if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_DATA) && (_fSafeForInitializing == true)) 400 Rslt = S_OK; 401 break; 402 default: 403 Rslt = E_NOINTERFACE; 404 break; 405 } 406 407 return Rslt; 408 } 409 410 #endregion 411 412 #region 屬性方法 413 414 #region 頁邊距 415 416 /// <summary> 417 /// 獲取--設置--左邊距 418 /// 計量單位 毫米(mm) 419 /// </summary> 420 public float MARGINLEFT 421 { 422 get { return _marginLeft; } 423 set { _marginLeft = value; } 424 } 425 426 /// <summary> 427 /// 獲取--設置--右邊距 428 /// 計量單位 毫米(mm) 429 /// </summary> 430 public float MARGINRIGHT 431 { 432 get { return _marginRight; } 433