网页WEB打印控件制作

Posted SDP开发平台

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了网页WEB打印控件制作相关的知识,希望对你有一定的参考价值。

    在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             set { _marginRight = value; }
 434         }
 435 
 436         /// <summary>
 437         /// 获取--设

以上是关于网页WEB打印控件制作的主要内容,如果未能解决你的问题,请参考以下文章

网页打印控件Lodop好不好

WEB 打印

web网页打印的方法

Web打印控件

web打印控件 LODOP的详细api

web打印控件 LODOP的详细api