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();
        }
        /// 
        /// 初始化打印机
        /// 
        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();
        }

        /// 
        /// 设置打印的内容
        /// 
        /// 
        /// 
        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对象

        }

        /// 
        /// 打印
        /// 
        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();
        }
    }
}

打印效果如下:

C#打印的方式总结_第1张图片

 

 

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

 

 

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

你可能感兴趣的:(C#编程,C#,打印,PrintDocument)