网页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)
下面贴出源码:(在源码中有详细的注释说明)
usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Drawing;namespaceE_Print{///<summary>///分页计算///</summary>publicclassPagingCalc{#region私有变量///<summary>///表格区域///</summary>privateRectangleF_tableRect;///<summary>///报表行集///</summary>privateList<Row>_rowsList;///<summary>///是否每页打印标题///</summary>privatebool_isAllPrintTitle;///<summary>///是否每页打印表头///</summary>privatebool_isAllPrintHead;///<summary>///是否每页打印表尾///</summary>privatebool_isAllPrintFoot;///<summary>///标题行集///</summary>privateList<Row>TitleList;///<summary>///表头前行集///</summary>privateList<Row>HForeList;///<summary>///表头行集///</summary>privateList<Row>HeadList;///<summary>///数据行集///</summary>privateList<Row>DataList;///<summary>///表尾行集///</summary>privateList<Row>FootList;///<summary>///每页打印标题+表头高度///</summary>privatefloat_myHeadPix;///<summary>///每页打印表尾高度///</summary>privatefloat_myFootPix;#endregion#region构造方法///<summary>///构造函数///</summary>publicPagingCalc(){_tableRect=newRectangleF();_rowsList=newList<Row>();_isAllPrintTitle=false;_isAllPrintHead=false;_isAllPrintFoot=false;TitleList=newList<Row>();HForeList=newList<Row>();HeadList=newList<Row>();DataList=newList<Row>();FootList=newList<Row>();_myHeadPix=0;_myFootPix=0;}#endregion#region属性方法///<summary>///获取--设置--表格区域///</summary>publicRectangleFTableRect{get{return_tableRect;}set{_tableRect=value;}}///<summary>///获取--设置--表格行集///</summary>publicList<Row>RowsList{get{return_rowsList;}set{_rowsList=value;}}///<summary>///获取--设置--是否每页打印标题///</summary>publicboolIsAllPrintTitle{get{return_isAllPrintTitle;}set{_isAllPrintTitle=value;}}///<summary>///获取--设置--是否每页打印表头///</summary>publicboolIsAllPrintHead{get{return_isAllPrintHead;}set{_isAllPrintHead=value;}}///<summary>///获取--设置--是否每页打印表尾///</summary>publicboolIsAllPrintFoot{get{return_isAllPrintFoot;}set{_isAllPrintFoot=value;}}///<summary>///获取--设置--每页打印标题+表头高度///</summary>publicfloatMyHeadPix{get{return_myHeadPix;}set{_myHeadPix=value;}}///<summary>///获取--设置--每页打印表尾巴高度///</summary>publicfloatMyFootPix{get{return_myFootPix;}set{_myFootPix=value;}}#endregion#region计算方法///<summary>///分页计算///</summary>///<returns></returns>publicList<PagingItem>CalcPages(){List<PagingItem>retPages=newList<PagingItem>();//无需分页if(Get_TableAllHeight()<=TableRect.Height){PagingItemtmItem0=newPagingItem();tmItem0.PageNum=1;for(inty=0;y<RowsList.Count;y++){tmItem0.IndexList.Add(y);}retPages.Add(tmItem0);}else//需要分页{//有设置了每页打印标题、表头、表位其中的任意一个if(Get_IsCusSet_THDF())//则执行每页相对分页{Paging_Relative(0,refretPages);//计算每页打印头尾高度MyHeadPix=0;if(IsAllPrintTitle){MyHeadPix+=Get_TableTileHeight();}if(IsAllPrintHead){MyHeadPix+=Get_TableHeadHeight();}if(IsAllPrintFoot){MyFootPix=Get_TableFootHeight();}}else//执行直接数据分页{Paging_Direct(0,refretPages);}}returnretPages;}///<summary>///直接分页///</summary>///<paramname="startR">开始行号</param>///<paramname="pages">页面数组</param>privatevoidPaging_Direct(intstartR,refList<PagingItem>pages){floatp_Height=TableRect.Height;PagingItemp_Item=newPagingItem();p_Item.PageNum=pages.Count+1;for(intt=startR;t<RowsList.Count;t++){//检查行内单元格是否不允许分页两种情况:条形码,图片if(Paging_CheckCell(RowsList[t],p_Height)){startR=t;pages.Add(p_Item);Paging_Direct(startR,refpages);break;}else{p_Height-=RowsList[t].RowHeight;if(p_Height<=0){startR=t;pages.Add(p_Item);Paging_Direct(startR,refpages);break;}else{p_Item.IndexList.Add(t);if(t==RowsList.Count-1){pages.Add(p_Item);}}}}}///<summary>///相对分页///</summary>///<paramname="startR">开始序号</param>///<paramname="pages">页面数组</param>privatevoidPaging_Relative(intstartR,refList<PagingItem>pages){SplitReportArea();//拆分表行floatp_Height=TableRect.Height;//页面总高PagingItemp_Item=newPagingItem();//分页页面p_Item.PageNum=pages.Count+1;//分页页码boolrunNext=false;//继续分页#region每页打印标题//每页打印标题if(IsAllPrintTitle){p_Height-=Get_TableTileHeight();foreach(Rowp_RowinTitleList)p_Item.IndexList.Add(p_Row.RowIndex);}else{if(p_Item.PageNum==1)//第一页特殊处理{p_Height-=Get_TableTileHeight();foreach(Rowp_RowinTitleList)p_Item.IndexList.Add(p_Row.RowIndex);}}#endregion#region每页打印表头//每页打印表头if(IsAllPrintHead){if(p_Item.PageNum==1)//第一页特殊处理{//计算表头前的行高p_Height-=Get_TableHForHeight();foreach(Rowp_RowinHForeList)p_Item.IndexList.Add(p_Row.RowIndex);}//计算表头行的高度p_Height-=Get_TableHeadHeight();foreach(Rowp_RowinHeadList)p_Item.IndexList.Add(p_Row.RowIndex);}else{if(p_Item.PageNum==1)//第一页特殊处理{//计算表头前的行高p_Height-=Get_TableHForHeight();foreach(Rowp_RowinHForeList)p_Item.IndexList.Add(p_Row.RowIndex);//计算表头行的高度p_Height-=Get_TableHeadHeight();foreach(Rowp_RowinHeadList)p_Item.IndexList.Add(p_Row.RowIndex);}}#endregion#region每页数据区域//每页数据划分if(IsAllPrintFoot){p_Height-=Get_TableFootHeight();//表格高度先减去表尾的高度}for(intt=startR;t<DataList.Count;t++){//检查行内单元格是否不允许分页两种情况:条形码,图片if(Paging_CheckCell(DataList[t],p_Height))//此情况下,单元格不能分割,并且高度超过页面剩余高度,所以要启动新的一页{startR=t;runNext=true;break;}else{p_Height-=DataList[t].RowHeight;if(p_Height<=0){startR=t;runNext=true;break;}else{p_Item.IndexList.Add(DataList[t].RowIndex);}}}#endregion#region每页打印表尾//每页打印表尾if(IsAllPrintFoot){foreach(Rowp_RowinFootList)p_Item.IndexList.Add(p_Row.RowIndex);}#endregion#region添加分页页面pages.Add(p_Item);if(runNext){Paging_Relative(startR,refpages);}#endregion}///<summary>///检查行内单元格如果是图片///并且合并行数大于1///</summary>///<paramname="cRow"></param>///<paramname="cHeight"></param>///<returns></returns>privateboolPaging_CheckCell(RowcRow,floatcHeight){foreach(CellcCellincRow.RowCells){if(cCell.IsImage==true){if(cCell.RectH>cHeight)returntrue;}}returnfalse;}#endregion#region辅助方法///<summary>///获取--报表全部高度///</summary>///<returns></returns>privatefloatGet_TableAllHeight(){floatretHight=0;for(intk=0;k<RowsList.Count;k++){Rowt_Row=RowsList[k];retHight+=t_Row.RowHeight;}returnretHight;}///<summary>///获取是否设置了标题、表头、表尾中的任意一个///</summary>///<returns></returns>privateboolGet_IsCusSet_THDF(){stringtmType="";foreach(RowcusRowinthis.RowsList){tmType=cusRow.RowType.ToLower().Trim();if(tmType=="t"||tmType=="h"||tmType=="f")returntrue;}returnfalse;}///<summary>///获取--报表标题高度///</summary>///<returns></returns>privatefloatGet_TableTileHeight(){floatretHight=0;for(intk=0;k<TitleList.Count;k++)retHight+=TitleList[k].RowHeight;returnretHight;}///<summary>///获取--报表表头前高度///</summary>///<returns></returns>privatefloatGet_TableHForHeight(){floatretHight=0;for(intk=0;k<HForeList.Count;k++)retHight+=HForeList[k].RowHeight;returnretHight;}///<summary>///获取--报表表头高度///</summary>///<returns></returns>privatefloatGet_TableHeadHeight(){floatretHight=0;for(intk=0;k<HeadList.Count;k++)retHight+=HeadList[k].RowHeight;returnretHight;}///<summary>///获取--报表表尾高度///</summary>///<returns></returns>privatefloatGet_TableFootHeight(){floatretHight=0;for(intk=0;k<FootList.Count;k++)retHight+=FootList[k].RowHeight;returnretHight;}///<summary>///拆分报表区域///</summary>publicvoidSplitReportArea(){TitleList=newList<Row>();HForeList=newList<Row>();HeadList=newList<Row>();DataList=newList<Row>();FootList=newList<Row>();for(intm=0;m<RowsList.Count;m++){RowmmRow=RowsList[m];switch(mmRow.RowType.ToLower()){case"t"://标题TitleList.Add(mmRow);break;case"h"://表头HeadList.Add(mmRow);break;case"f"://表尾FootList.Add(mmRow);break;case"d"://数据default:DataList.Add(mmRow);break;}}//设置表头前行集if(TitleList.Count==0&&HeadList.Count>0){List<Row>tmpList=newList<Row>();for(intn=0;n<DataList.Count;n++){if(DataList[n].RowIndex<HeadList[0].RowIndex){HForeList.Add(DataList[n]);tmpList.Add(DataList[n]);}}for(intn=0;n<tmpList.Count;n++){DataList.Remove(tmpList[n]);}tmpList.Clear();}//重设表尾不是每页打印表尾情况下,那么表位就去掉if(!IsAllPrintFoot){foreach(RowtRowinFootList)DataList.Add(tRow);FootList.Clear();}}#endregion}}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。