之前写过一个“C#WinForm程序中选择打印机打印”的文章,但在使用过程中,尤其是生成Word文档时,会感觉系统响应较慢。如果不需要留存打印文档的电子版,可以使用下面的类直接打印。相比之前的方法,这种方法更简单,可以绘制文字、线段、图片。
打印响应速度很快,比之前的方法更好用。
需要引用:
using System.Drawing;
using System.Drawing.Printing;
public class PrnBill
{
private Font titleFont;//标题字体;
private Font font;//正文字体
private Font smallFont; //小字体;
private PrintDocument printDoc;
// 使用的打印机名称
private string printerName;
public string PrinterName
{
get { return printerName; }
set { printerName = value; }
}
// 打印份数
private short copies;
public short Copies
{
get { return copies; }
set { copies = value; }
}
//标题内容
private string title;
public string Title
{
get{return title;}
set{title = value;}
}
//姓名
private string cusName;
public string CusName;
{
get{return cusName;}
set{cusName = value;}
}
//正文内容
private string contents;
public string Contents
{
get { return contents; }
set { contents = value; }
}
//标志图片
private Image logoImg;
public Image LogoImg
{
get { return logoImg; }
set { logoImg = value; }
}
//构造函数
public PrnBill(string printer, short prnCopies, string _title, string _contents,Image _logoImg)
{
printDoc = new PrintDocument();
printDoc.PrintPage += new PrintPageEventHandler(pd_PrintBill);
titleFont = new System.Drawing.Font("宋体", 25f);//标题字体
font = new System.Drawing.Font("宋体", 14f);//字体
smallFont = new System.Drawing.Font("宋体", 12f);//字体
printerName = printer;
copies = prnCopies;
logoImg = _logoImg;
contents = _contents;
}
// 打印的方法
public void Print()
{
printDoc.PrinterSettings.PrinterName = printerName;
printDoc.PrinterSettings.Copies = copies;
printDoc.Print(); //调用pd_PrintBill
}
///
/// 打印主过程
///
private void pd_PrintBill(object sender, PrintPageEventArgs e)
{
int topMargin = 24; //上边距
int leftMargin = 50; //左边距
int rowHeight = 30; //行高
int lineStartY = 117; //画线起始位置
int currentPosY = lineStartY - rowHeight; //当前绘制位置
int currentLineY = lineStartY; //当前画线位置
int padding = 2; //调整位置使用
float fdxsLogo = 0.1f; //图片缩放系数
Pen penLine; //画笔
//因需要绘制表格,故把表格第一行的四个格子宽度放到了变量
int w_HA = 148;
int w_HB = 226;
int w_HC = 152;
int w_HD = 175;
//开始画文字
DrawString(e.Graphics, "业务受理单", titleFont, SystemBrushes.WindowText, new Rectangle(leftMargin, topMargin, w_HA + w_HB + w_HC + w_HD + 15, 70), StringAlignment.Center, StringAlignment.Center);
DrawString(e.Graphics, "姓名:" + cusName, font, SystemBrushes.WindowText, new Rectangle(leftMargin, currentPosY, w_HA + w_HB, rowHeight), StringAlignment.Near, StringAlignment.Center);
currentPosY += rowHeight;
DrawString(e.Graphics, "事项内容:"+contents, font, SystemBrushes.WindowText, new Rectangle(leftMargin, currentPosY, w_HA+w_HB, rowHeight), StringAlignment.Near, StringAlignment.Center);
//这里可以按照自己需要,继续添加内容……
//在合适的位置绘制图片
if (logoImg != null)
{
e.Graphics.DrawImage(logoImg, new RectangleF((leftMargin * 2 + w_HA + w_HB + w_HC + w_HD - logoImg.Width * fdxsLogo) / 2, currentPosY, logoImg.Width * fdxsLogo, logoImg.Height * fdxsLogo));
}
//开始画横线
penLine = new Pen(Color.Black, 1f);
penLine.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
e.Graphics.DrawLine(penLine, leftMargin, currentLineY, w_HA + w_HB + w_HC + w_HD + leftMargin, currentLineY);
}
/**/
///
/// 绘制文字的具体方法
///
/// 定义画布
/// 需要绘制的文字
/// 字体
/// 笔刷
/// 绘制区域
/// 水平对齐方式
/// 垂直对齐方式
private void DrawString(Graphics g, string s, Font font, Brush brush, Rectangle rect, StringAlignment alignment, StringAlignment lineAlignment)
{
StringFormat sf = new StringFormat();
sf.Alignment = alignment;
sf.LineAlignment = lineAlignment;
g.DrawString(s, font, brush, rect, sf);
}
}