PrintDocument or PrintPreviewDialog 打印
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPreview_Click(object sender, EventArgs e)
{
PackTypeReport report = null;
_printTable = GetPrintTable();
report.PrintData = _printTable;
report.Preview();
} /// <summary>
/// 打印
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPrint_Click(object sender, EventArgs e)
{
PackTypeReport report = null;
_printTable = GetPrintTable();
report.PrintData = _printTable;
report.Print();
}
/// <summary>
/// 电子条码打印
/// </summary>
public class PackTypeReport
{ /// <summary>
/// 打印文档
/// </summary>
PrintDocument _printDoc;
/// <summary>
/// 打印预览窗体
/// </summary>
PrintPreviewDialog _printPreview; /// <summary>
/// A4纸宽度
/// </summary>
const float PageWidth = 840f;//210mm
/// <summary>
/// A4纸高度
/// </summary>
const float PageHeight = 1188f;//297mm const float PaddingVertical = 20f;//上下边距
const float PaddingHorizontal = 40f;//左右边距
const float BarCodeHeight = ;//条码高度
const float ItemPadding = ;//项目内部项上下间隔
private float BarCodeTop = PaddingVertical;
private float _barcodeLeft = PaddingHorizontal;
private float _barcodeWidth = ;//每个条码的间隔量
private float _ItemHeight = ;//每个项目间的上下间隔
private float CodeNameTHeight = 0f;//条码(包括下边距)和包名总高度,窗体加载中设置
private int PageIndex = ;//打印当前页
private string Title = "器械包编码";//打印显示的标题;
private DataRow[] printRowsForC = null;//字母检索数据行集合
private Font TitleFont = new Font("Arital", 16f, FontStyle.Bold);//标题字体
private Font CodeFont = new Font("Arital", 10f);//条码字体
private Font NameFont = new Font("Arital", 12f);//包名字体
private char letter = '';//非字母标实
private int rowIndex = ;//行索引
private List<DataRow> printRowsForD = null;//非字母检索数据行集合 private DataTable _printTable = null;
/// <summary>
/// 打印数据
/// </summary>
public DataTable PrintData
{
set
{
_printTable = value;
}
}
/// <summary>
///
/// </summary>
public PackTypeReport(PrintDocument pd, PrintPreviewDialog prd)
{
_printDoc = pd;
_printPreview = prd;
CodeNameTHeight = GetCodeAndNameHeight(); _printPreview.Width = ;
_printPreview.Height = ;
_printDoc.DefaultPageSettings.Margins = new System.Drawing.Printing.Margins(, , , );
_printDoc.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("A4", (int)PageWidth, (int)PageHeight);
_printDoc.PrintPage += new PrintPageEventHandler(DrawPage);
} /// <summary>
/// 打印
/// </summary>
public void Print()
{
_printDoc.Print();
} /// <summary>
/// 预览
/// </summary>
public void Preview()
{
_printPreview.ShowDialog();
} //条码(包括下边距)和包名总高度
private float GetCodeAndNameHeight()
{
return TitleFont.GetHeight() + ItemPadding + BarCodeHeight;
} /// <summary>
/// 开始绘制
/// </summary>
private void DrawPage(object sender,System.Drawing.Printing.PrintPageEventArgs e)
{
//定位到页头
BarCodeTop = PaddingVertical;
//临时的下一个定位点,用于绘制包名条码
float LSTop = 0f;
//第一页需要绘制标题
if (PageIndex == )
{
DrawTitle(e.Graphics, Title, ref BarCodeTop);
}
float y = ;
while (BarCodeTop < PageHeight - PaddingVertical)
{
if (letter == '')
{
//检索非字母优化设置
if (printRowsForD == null)
{
printRowsForD = new List<DataRow>();
foreach (DataRow dr in _printTable.Select("", "name asc"))
{
if (dr["Spell"].ToString() == "" || !char.IsLetter(dr["Spell"].ToString()[]))
{
printRowsForD.Add(dr);
}
}
//0个非字母数据时,将转入字母A开始的数据,并初始化行索引rowIndex
if (printRowsForD.Count == )
{
letter = 'A';
rowIndex = ;
}
}
else
{
//行索引在指定行内
if (rowIndex < printRowsForD.Count)
{
string packName = printRowsForD[rowIndex]["Name"].ToString();
string barcode = printRowsForD[rowIndex]["Bar"].ToString();
if (rowIndex % == )
{
//判断包名和条码组合高度是否超出指定高度
_barcodeLeft = PaddingHorizontal;
BarCodeTop += y + _ItemHeight;//加上Y偏移
if (BarCodeTop + CodeNameTHeight >= PageHeight - PaddingVertical)
{
break;
}
else
{
y = DrawCodeAndName(e.Graphics, packName, barcode,letter, _barcodeLeft, BarCodeTop);
LSTop = y + BarCodeTop + ;
}
}
else
{
y = DrawCodeAndName(e.Graphics, packName, barcode, '',_barcodeLeft, BarCodeTop);
}
_barcodeLeft += _barcodeWidth;
rowIndex++;
}
else
{
//数据全部加载完设置
printRowsForD = null;
letter = 'A'; //奇偶条码终点定位设置,奇数需要跳转到下一个定位点高度
if ((rowIndex - ) % == )
{
BarCodeTop = LSTop;
y = ;//Y偏移量已经加在LSTOP上
} rowIndex = ;
//类型名与条码包名的组合高度超过指定高度,需要跳转到下一页,用于分类名显示不会显示在页尾的情况
if (BarCodeTop + CodeNameTHeight >= PageHeight - PaddingVertical)
{
break;
}
}
}
}
else
{
//字母检索优化设置
if (printRowsForC == null || printRowsForC.Length == )
{
//判断当前定位点是否能够包含类型名绘制点
if (BarCodeTop >= PageHeight - PaddingVertical)
{
break;
}
else
{
printRowsForC = _printTable.Select(string.Format("Spell like '{0}%'", letter), "Name asc");
//判断该分类数据是否存在,存在时显示其分类名
if (printRowsForC.Length == )
{//如果该分类不存在数据,且不是Z字母时,初始化行索引和进入下一个字母,若为Z,初始化后退出循环
if (letter < 'Z')
{
rowIndex = ;
letter = (char)(letter + );
}
else
{
rowIndex = ;
letter = (char)(letter + );
printRowsForC = null;
break;
}
}
}
}
else
{
//行索引在指定行内
if (rowIndex < printRowsForC.Length)
{
string packName = printRowsForC[rowIndex]["Name"].ToString();
string barcode = printRowsForC[rowIndex]["Bar"].ToString();
if (rowIndex % == )
{
BarCodeTop += y + _ItemHeight;
_barcodeLeft = PaddingHorizontal;
//判断包名和条码组合高度是否超出指定高度
if (BarCodeTop + CodeNameTHeight >= PageHeight - PaddingVertical)
{
break;
}
else
{
y = DrawCodeAndName(e.Graphics, packName, barcode, letter, _barcodeLeft, BarCodeTop);
LSTop = y + BarCodeTop + ;
} }
else
{
y = DrawCodeAndName(e.Graphics, packName, barcode, '', _barcodeLeft, BarCodeTop);
}
_barcodeLeft += _barcodeWidth;
rowIndex++;
}
else
{
//加载该字母分类数据完毕时,需要判断该字母是否为字母Z。若不是,进入奇偶条码的下一个定位点判断,并初始化。若是,则设置字符为Z的下一个,并退出循环
if (letter >= 'Z')
{
letter = (char)(letter + );
break;
}
else if ((rowIndex - ) % == )
{
BarCodeTop = LSTop;
y = ;
}
rowIndex = ;
printRowsForC = null;
letter = (char)(letter + );
//类型名与条码包名的组合高度超过指定高度,需要跳转到下一页,用于分类名显示不会显示在页尾的情况
if (BarCodeTop + CodeNameTHeight >= PageHeight - PaddingVertical)
{
break;
}
}
}
}
} PageIndex++;
//当退出循环时,使用字符判断是否为Z的下一个字符,来确定当前页是否是最后一页
if (letter <= 'Z')
{
e.HasMorePages = true;
}
else
{
PageIndex = ;
letter = '';
rowIndex = ;
printRowsForD = null;
printRowsForC = null;
e.HasMorePages = false;
}
} /// <summary>
/// 绘制标题
/// </summary>
private void DrawTitle(Graphics g, string strTitle, ref float y)
{
float width = g.MeasureString(strTitle, TitleFont).Width;
float left = (PageWidth - width) / ;
g.DrawString(strTitle, TitleFont, Brushes.Black, left, PaddingHorizontal); y += TitleFont.GetHeight() + ItemPadding;
} /// <summary>
/// 绘制条码和包名,返回下边纵坐标
/// </summary>
/// <returns>float</returns>
private float DrawCodeAndName(Graphics g, string name, string code,char letter, float x, float y)
{
float top = ;
float y1 = ;
string name1 = string.Empty;
string name2 = string.Empty; if (name.Length > )
{
name1 = name.Substring(, );
name2 = name.Substring();
}
else
{
name2 = " ";
name1 = name;
}
if (letter != '')
{
g.DrawString(Convert.ToString(letter), NameFont, Brushes.Black, x, y);
y1 = NameFont.GetHeight() + ItemPadding;
top = y1;
y += y1;
}
else
{
g.DrawString("", NameFont, Brushes.Black, x, y);
y1 = NameFont.GetHeight() + ItemPadding;
top = y1;
y += y1;
}
g.DrawString(name1, NameFont, Brushes.Black, x, y);
y1 = NameFont.GetHeight() + ItemPadding;
top = y1;
y += y1; g.DrawString(name2, NameFont, Brushes.Black, x, y);
top += y1;
y += y1; float y2 = ;
g.DrawString(code, CodeFont, Brushes.Black, x, y);
y2 = CodeFont.GetHeight() + ItemPadding; top += y2;
y += y2;
Image codeImg = BLL.Report.Print.IReport.GetBarcodeImg(code);
g.DrawImage(codeImg, x, y); top += codeImg.Height + ItemPadding; return top;
} }
PrintDocument or PrintPreviewDialog 打印的更多相关文章
- winform使用PrintDocument控件打印以及oledb驱动
代码,需要加入的控件:PrintDocument.PageSetupDialog.PrintDialog.PrintPreviewDialog.BackgroundWorker,控件的Document ...
- Winform中使用printDocument控件打印pictureBox中的二维码照片
场景 Winform中使用zxing和Graphics实现自定义绘制二维码布局: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1 ...
- winform 打印控件
(1)PageSetupDialog1 打印设置窗口 (2)PrintDocument 向打印机输送的对象 事件:PrintPage 对于打印的每一页都执行一次 (3)PrintP ...
- winform 对话框、打印框
winform 对话框控件 1.打开文件对话框(OpenFileDialog) 2.保存文件对话框(SaveFileDialog) 3.字体对话框(FontDialog) 4.颜色对话框(ColorD ...
- Windows 打印控件
Windows窗体的PrintDocument组件用于设置一些属性,这些属性说明,在基于Windows的应用程序中要打印说明内容以及打印文档的能力,可将它与PrintDialog组件一起使用来控制文档 ...
- C# 字符流打印类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...
- C# 打印小票 POS
C# 打印小票 POS 最近在写一个餐饮的收银系统,以前从来没有碰过打印机这玩意.感觉有些无从下手,在前面做报表时,总想找第三方的控件来用用,结果始终不行没搞定.没研究透,催得急没办法还是的动手自己写 ...
- c# windowsForm打印
在windows应用程序中文档的打印是一项非常重要的功能,在以前一直是一个非常复杂的工作,Microsoft .net Framework的打 印功能都以组件的方式提供,为程序员提供了很大的方便,但是 ...
- C#实现打印与打印预览功能
C#实现打印与打印预览功能的思路及代码. 在windows应用程序中文档的打印是一项非常重要的功能,在以前一直是一个非常复杂的工作,Microsoft .Net Framework的打印功能都以组件的 ...
随机推荐
- 学习HTML5之路
Web 技术大致的时间轴 1991 HTML 1994 HTML 2 1996 CSS 1+JavaScript 1997HTML 4 1998 CSS2 2000 XHTML 1 2002 使用DI ...
- OPCDAAuto.dll的C#使用方法浅析(转载)
上次研究了.Net版本的OPC API dll,这次我采用OPCDAAuto.dll来介绍使用方法.以下为我的源代码,有详细的注释无需我多言.编译平台:VS2008SP1.WINXP.KEPServe ...
- 严谨的程序案例Api
文档 功能 同步推荐关系 接口方法 syncRelation 参数描述 OriginalUsername 查询的用户用户名 RecommandUsername 推荐人用户名 返回值 status 1成 ...
- 微信小程序之if操作
.wxss控制样式 .price-agent{ font-size: 25rpx; color:#ababab; float: left; position: absolute; bottom: 0; ...
- Cassandra学习六 一些知识点
http://www.flyml.net/2016/09/08/cassandra-tutorial-java-api-example/ Cassandra对查询的支持很弱,只支持主键列及索引列的查询 ...
- java面试(6)
1 六大原则 详情参考:设计模式六大原则(转载). 2 UML类之间关系有几种?聚合和组合区别? 类之间可能存在以下几种关系:关联(association).依赖(dependency).聚合(A ...
- WP8.1通过StreamSocket连接C++服务器
注:当服务端和手机模拟器运行在一台机器时,会有奇怪错误.将服务端放在其它机器上更改客户端连接地址,运行正常.或者直接用本机modern调试也可以. 实例化一个对象 StreamSocket clien ...
- Storm集群详细部署
1.安装zookeeper 3.1下载zookeeper安装包, 建议下载3.4.5及以上的版本 http://www.apache.org/dyn/closer.cgi/zookeeper/ 3.2 ...
- http://www.jb51.net/list/list_233_2.htm(导航: 首页 >> 软件编程 >> Android)
日期:2015-04-24理解Android中Activity的方法回调 日期:2015-04-24Android获取手机通讯录.sim卡联系人及调用拨号界面方法 日期:2015-04-24And ...
- JAVA的对称加密算法AES——加密和解密
出自: http://blog.csdn.net/hongtashan11/article/details/6599645 http://www.cnblogs.com/liunanjava/p/42 ...