/// <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 打印的更多相关文章

  1. winform使用PrintDocument控件打印以及oledb驱动

    代码,需要加入的控件:PrintDocument.PageSetupDialog.PrintDialog.PrintPreviewDialog.BackgroundWorker,控件的Document ...

  2. Winform中使用printDocument控件打印pictureBox中的二维码照片

    场景 Winform中使用zxing和Graphics实现自定义绘制二维码布局: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1 ...

  3. winform 打印控件

    (1)PageSetupDialog1    打印设置窗口  (2)PrintDocument     向打印机输送的对象 事件:PrintPage   对于打印的每一页都执行一次 (3)PrintP ...

  4. winform 对话框、打印框

    winform 对话框控件 1.打开文件对话框(OpenFileDialog) 2.保存文件对话框(SaveFileDialog) 3.字体对话框(FontDialog) 4.颜色对话框(ColorD ...

  5. Windows 打印控件

    Windows窗体的PrintDocument组件用于设置一些属性,这些属性说明,在基于Windows的应用程序中要打印说明内容以及打印文档的能力,可将它与PrintDialog组件一起使用来控制文档 ...

  6. C# 字符流打印类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  7. C# 打印小票 POS

    C# 打印小票 POS 最近在写一个餐饮的收银系统,以前从来没有碰过打印机这玩意.感觉有些无从下手,在前面做报表时,总想找第三方的控件来用用,结果始终不行没搞定.没研究透,催得急没办法还是的动手自己写 ...

  8. c# windowsForm打印

    在windows应用程序中文档的打印是一项非常重要的功能,在以前一直是一个非常复杂的工作,Microsoft .net Framework的打 印功能都以组件的方式提供,为程序员提供了很大的方便,但是 ...

  9. C#实现打印与打印预览功能

    C#实现打印与打印预览功能的思路及代码. 在windows应用程序中文档的打印是一项非常重要的功能,在以前一直是一个非常复杂的工作,Microsoft .Net Framework的打印功能都以组件的 ...

随机推荐

  1. 【openCV学习笔记】【3】高斯模糊一张图片(_cvSmooth相关编译错误)

    代码如下: #include <iostream> #include <opencv/highgui.h> #include <opencv/cv.h> void ...

  2. TableView刷新 局部刷新等

    1.对整个页面刷新 [ tableView reloadData]; 2.对某一个section刷新 NSIndexSet *indexSet=[[NSIndexSet alloc]initWithI ...

  3. ORACLE常识

    1. ORACLE中查看表中的外键来源于哪些表 select cl.table_name from user_cons_columns cl left join user_constraints c ...

  4. 【转】Jmeter应用评估

    Jmeter应用评估 发布时间: 2008-9-03 16:17    作者: 未知    来源: 网络转载 字体:  小  中  大  | 上一篇 下一篇 | 打印  | 我要投稿  | 推荐标签: ...

  5. oracle自动创建表分区

    创建一个table,记录哪些表需要创建表分区 create table STAT_TABLE ( tablename VARCHAR2(), pre_partition_name VARCHAR2() ...

  6. python's eighth day for me

    f : 变量,f_obj, file, f_handler,...文件句柄. open : windows 的系统功能. windows 默认编码方式:gbk.   Linux 默认编码方式:utf ...

  7. Node.js实用知识点

    本文介绍如何使用nodejs 简单的HttpServer 调试nodejs 基础路由 nodejs配置开发和生产环境 nodejs核心模块一览 express用法 文件I/O nodejs模块 nod ...

  8. VS2017自动添加头部注释

    让VS自动生成类的头部注释,只需修改两个文集即可,一下两个路径下个有一个 Class.cs文件 D:\Program Files (x86)\Microsoft Visual Studio\2017\ ...

  9. Deep Learning 学习笔记(3):Linear Regression 数据的预处理

    为了获得良好的收敛,在进行梯度下降前,我们可以对数据进行预处理. 目标是使得数据大小在同一个数据数量级上,均值为零. 一般将数据放缩到(-1,1)区间, 我们可以对数据进行如下操作: 其中u1是数据的 ...

  10. HTTP协议头域详解

    HTTP协议头域详解 Requests部分 Accept 指定客户端能够接收的内容类型 Accept: text/plain, text/html Accept-Charset 浏览器可以接受的字符编 ...