C# 自定义打印

C# 自定义打印类,可以实现打印文本、线条、图片等,以下仅仅是举例代码:

public class PrintInfo
    {
        public string PortName { get; set; }
        public string DepartureTime { get; set; }
        public string AMPM { get; set; }

        PrintDocument document = new PrintDocument(); 
        private int pageWidth = 302;
        private int pageHeight = 530;

        public PrintInfo()
        {        
       PortName=“Test Port”;
       DepartureTime="11:30";
       AMPM="上午";       document.BeginPrint
+= new PrintEventHandler(document_BeginPrint); document.PrintPage += new PrintPageEventHandler(document_PrintPage); document.EndPrint += new PrintEventHandler(document_EndPrint); } private void document_EndPrint(object sender, PrintEventArgs e) { } private void document_BeginPrint(object sender, PrintEventArgs e) { } private void document_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { if (DepartureTime != null) { //Logo Bitmap imageLogo = new Bitmap(System.AppDomain.CurrentDomain.BaseDirectory + @"\Resource\logo.bmp"); e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; e.Graphics.SmoothingMode = SmoothingMode.HighQuality; e.Graphics.CompositingQuality = CompositingQuality.HighQuality; e.Graphics.DrawImage(imageLogo, 5, 10, 292, 131); //Line e.Graphics.DrawLine(new Pen(Brushes.Black, 3), 12, 166, 291, 166); //Port Name Font strFont = new Font("宋体", 18); e.Graphics.DrawString(PortName, strFont, Brushes.Black, (float)pageWidth / 2 - e.Graphics.MeasureString(PortName, strFont).Width / 2 + 1, 198); //Departure Time strFont = new Font("宋体", 50, FontStyle.Bold); e.Graphics.DrawString(DepartureTime, strFont, Brushes.Black, (float)pageWidth / 2 - e.Graphics.MeasureString(DepartureTime, strFont).Width / 2, 248); //AMPM e.Graphics.DrawString(AMPM, strFont, Brushes.Black, (float)pageWidth / 2 - e.Graphics.MeasureString(AMPM, strFont).Width / 2, 320); //Line e.Graphics.DrawLine(new Pen(Brushes.Black, 3), 12, 419, 291, 419); //Thankyou and print time strFont = new Font("宋体", 13, FontStyle.Bold); printTxt = "Thank You"; e.Graphics.DrawString(printTxt, strFont, Brushes.Black, 12, 481); strFont = new Font("宋体", 8); printTxt = DateTime.Now.ToString("MM/dd/yyyy hh:mmt\\M"); e.Graphics.DrawString(printTxt, strFont, Brushes.Black, 184, 487); } } public void PrintDocument() { document.DefaultPageSettings.PaperSize = new PaperSize("Custum", pageWidth, pageHeight); PrintController printController = new StandardPrintController(); document.PrintController = printController; //System.Windows.Forms.PrintPreviewDialog printPreview = new System.Windows.Forms.PrintPreviewDialog(); //printPreview.Document = document; //printPreview.ShowDialog(); document.Print(); }

 

一、应用场景与出发点 同一个系统中,为了解决不同的客户可能需要设计不同的单据打印模板,实现此方法可能是: 1、设计不同的自带RDLC报表文件,根据当前客户加载不同的报表并打印 2、GDI+绘图 和 打印组件 ,不同的客户创建不同的绘图XML格式的模板内容 3、其他第三方组件 主要对比一下前两种方法,第一种方法不好之处在于,不灵活,开发者必须地每个客户制订一个报表,不推荐采用。第二种方法,修改对应的模板内容就可以了, 模板内容可以是Xml文件,也可以是存放在数据库中的Xml格式字符串。推荐采用这种方法。然后这种方法的也有一个棘手问题 :如何让用户快速、方便地设计打印模板,本示例就是为了解决这个问题。 二、实现思路与原理 功能概要:设计一个界面,支持用户自由添加 要打印的项,文本,直线,图片 等,并且可以方便改变打印项的 字体、颜色、粗细、位置,设计时支持效果预览。 技术要点:GDI+绘图、拖动控件、XML解析、自定义控件 三、相关类介绍 绘图工具类:DrawHelper 实现 xml格式模板 与 打印项 之间进行互相转换,在目标画板中绘制 拖动工具类:WinHelper 实现控件的鼠标拖动,键盘移动 自定义控件:用于显示文字的文本框 TextBoxExt、用于显示直线的标签 LabelExt 主窗体代码:用于用户操作,添加,删除,编辑,打印项 详细介绍请参照我的博文:http://de.cel.blog.163.com/blog/static/51451236201472215450939/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值