C#打印的方式总结

打印在医疗软件中经常碰到,如打印标签、发药单等

 

本文力求抽出最简单的模型,至于要打印什么内容,可自由发挥,什么安全校验、异常捕捉都不考虑,避免增加代码复杂性

 

方式1:利用PrintDocument

 

步骤:

1   新建控制台项目,名为‘PrintDocument打印’,然后为项目添加System.Drawing.dll引用

 

2   新建类,名为PrinterHelper,并编辑如下:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrintDocument打印
{
    public class PrinterHelper
    {
        PrintDocument _printDocument;
        float _leftMargin = 0;
        float _topMargin = 0;
        Brush brushHeaderFont = new SolidBrush(Color.Black);
        Pen LinePen = new Pen(Color.Black, 1);
        public PrinterHelper()
        {
            InitPrint();
        }
        /// <summary>
        /// 初始化打印机
        /// </summary>
        private void InitPrint()
        {
            _printDocument = new PrintDocument();
            //设置打印机的名称,没有设就使用默认可以使用的打印机
            //_printDocument.PrinterSettings.PrinterName = "Microsoft XPS Document Writer";
            _printDocument.DefaultPageSettings.Landscape = true;
            _printDocument.DefaultPageSettings.PaperSize = new PaperSize("CustomerSize", (Int32)472, (Int32)333);
            _printDocument.PrintPage += new PrintPageEventHandler(this.PrintLabel);
            _printDocument.PrintController = new StandardPrintController();
        }

        /// <summary>
        /// 设置打印的内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PrintLabel(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Font Cellfont = new Font("宋体", 15, System.Drawing.FontStyle.Bold);
            e.Graphics.DrawString("角膜病区", Cellfont, brushHeaderFont, _leftMargin, _topMargin);
            e.Graphics.DrawString("25号床", Cellfont, brushHeaderFont, _leftMargin + 185, _topMargin);

            Cellfont = new Font("宋体", 15, System.Drawing.FontStyle.Bold);
            e.Graphics.DrawString("张祥裕", Cellfont, brushHeaderFont, _leftMargin + 52, _topMargin + 42);
            Cellfont = new Font("宋体", 11, System.Drawing.FontStyle.Bold);
            e.Graphics.DrawString("211119039", Cellfont, brushHeaderFont, _leftMargin + 52, _topMargin + 65);

            Cellfont = new Font("宋体", 12, System.Drawing.FontStyle.Bold);
            e.Graphics.DrawString("08:00", Cellfont, brushHeaderFont, _leftMargin + 200, _topMargin + 42);
            e.Graphics.DrawString("2020-09-24", Cellfont, brushHeaderFont, _leftMargin + 200, _topMargin + 65);

            //当然也可以打印二维码或者一维码
            //使用e.Graphics.DrawImage,把二维码信息转成base64字符串,再把base64字符串转成Image对象

        }

        /// <summary>
        /// 打印
        /// </summary>
        public void Print()
        {
            if (_printDocument != null)
            {
                _printDocument.Print();
            }
        }
    }
}

3   在主程序测试如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrintDocument打印
{
    class Program
    {
        static void Main(string[] args)
        {
            PrinterHelper printer = new PrinterHelper();
            printer.Print();
            Console.WriteLine("打印完毕");
            Console.Read();
        }
    }
}

打印效果如下:

 

 

方式2:利用报表(如rdlc报表或者devexpress报表),待补充

 

 

方式3:window底层API(winspool.drv),待补充

  • 4
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zxy2847225301

测试使用

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值