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的打印功能都以组件的 ...
随机推荐
- 蓝桥杯 算法训练 ALGO-34 纪念品分组
算法训练 纪念品分组 时间限制:1.0s 内存限制:256.0MB 问题描述 元旦快到了,校学生会让乐乐负责新年晚会的纪念品发放工作.为使得参加晚会的同学所获得的纪念品价值 相对均衡,他要把购 ...
- AppCan上下拉列表刷新
function initBounce(funcTop, funcBottom){ uexWindow.setBounce("1"); if (!funcTop && ...
- 常用FTP命令 1. 连接ftp服务器
1. 连接ftp服务器 格式:ftp [hostname| ip-address]a)在linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码,分别输入用户名和相应密码 ...
- 2015 浙江省赛B Team Formation (技巧,动归)
Team Formation For an upcoming programming contest, Edward, the headmaster of Marjar University, is ...
- Altium Designer之多图纸设计
Altium Designer的多图纸功能感觉比较方便:今天翻了下徐老师<Altium Designer 快速入门>里面关于多图纸设计的介绍,再参考了altium 网站的一些资料,算是摸熟 ...
- gcc及其选项详解
1.简介: gcc是gnu旗舰产品,目前基本上就是和unix捆绑在一起分发的.这个东西功能强大,但是有多达上千个选项,其用户手册也有将近一万行.虽然其中的多数选项平时很少用到.但是不管装软件还是写程序 ...
- Flask 数据库多对多关系
数据库使用关系建立记录之间的联系.其中,一对多关系是最常用的关系类型,它把一个记录和一组相关的记录联系在一起.实现这种关系时,要在“多”这一侧加入一个外键,指向“一”这一侧联接的记录.大部分的其他关系 ...
- JAXB 专题二(BSP接口实战)
BSP下单接口 1.xml格式如下 <?xml version="1.0" encoding="utf-8"?> <Request servi ...
- Maven 国内源
maven的仓库好慢的说,还是配置一个国内的源吧.推荐aliyun 在maven/conf/settings.xml 文件里配置mirrors的子节点,添加如下mirror <mirror> ...
- java StirngStringbufferStringbuild的区别
java StirngStringbufferStringbuild的区别 String 1,Stirng是对象不是基本数据类型 2,String是final类,不能被继承.是不可变对象,一旦创建, ...