/// <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. laravel查看sql语句

    我自己是用第一种方法来调试的,第三种不行 不知道为啥 laravel查看sql语句 方法一: 我们有时候想测试一段代码生产的 SQL 语句,比如: 我们想看 App\User::all(); 产生的 ...

  2. fineUI表格控件各属性说明

    最近在新的公司后台用fineUI,所以也稍微总结一下表格的一些属性,希望对刚入手的人有些帮助: AllowPaging 允许分页,为true时表示允许分页,表格下方会出现分页工具栏: PageSize ...

  3. Navicat设定mysql定时任务步骤示例

    怎样在Navicat中设置,是数据库按照记录中的日期更新状态字段 其实这个很常用,比如你网站里的某条记录的日期——比如说数据库中某条活动记录的审核日期字段已经过期,亦即当前时间已经超过审核日期,那么定 ...

  4. 【转】Apache JMeter web性能测试实例

    Apache JMeter是可以对利用HTTP或FTP服务器的应用程序进行测试的工具.它是基于Java的,通过所提供的API它还具有高度可扩展性.典型的JMeter测试包括创建循环和线程组.循环使用预 ...

  5. 模块的分类以及time与date time 模块 radom模块

    1.标准库,或者内置模块,python解释器自带的,比如sys,os模块 2.开源模块,或者叫第三方模块,python就强大在这里. 3.自定义模块. 标准库: 1.时间模块time与datetime ...

  6. PHP中GD库的使用

    1.基本步骤 <?php /** * Created by PhpStorm. * User: jiqing * Date: 18-4-9 * Time: 上午9:34 * 熟悉步骤 */ // ...

  7. 转:oracle几组重要的常见视图-v$latch,v$latch_children,v$lock,v$locked_object

    v$latch Oracle Rdbms应用了各种不同类型的锁定机制,latch即是其中的一种.Latch是用于保护SGA区中共享数据结构的一种串行化锁定机制.Latch的实现是与操作系统相关的, 尤 ...

  8. Linux机器工作环境安装

    安装gcc编译器: yum -y install gcc 安装wget: yum -y install wget 安装python-setuptools: wget http://peak.telec ...

  9. 爬了个爬(三)Scrapy框架

    参考博客:武Sir Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了页面抓取 (更确切来说, 网络抓取 ...

  10. div+css 左右两列自适应高度 ,以及父级div也跟着自适应子级的高度(兼容各大浏览器)

    <style type="text/css" media="screen"> <!-- #main {width:500px;_height: ...