C# 打印機打印

1.安裝打印機驅動

2.代碼實現:

關鍵在於使用  e.Graphics.DrawString() 實現打印

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;
using System.Drawing.Drawing2D;
using SMT_LabellingMachine.Interface;
using System.Windows.Forms;

using Gma.QrCodeNet.Encoding;
using Gma.QrCodeNet.Encoding.Windows.Render;
using System.IO;




namespace SMT_LabellingMachine.PrintModel
{
    public class LabelPrinter
    {
        /// <summary>
        /// 打印內容
        /// </summary>
        public string PrintContent { get; private set; }

        PrintDocument _printDocument;

        const string str = "160-12345678-8888,AbcdefgHijklmnOpqRst,2018/05/21";
        List<Bitmap> imgLst = new List<Bitmap>();

        public LabelPrinter()
        {
            _printDocument = new PrintDocument();
            _printDocument.DefaultPageSettings.PrinterSettings.PrinterName = "Intermec PM43c (300 dpi) #2";
            // _printDocument.DefaultPageSettings.PaperSize = new PaperSize("Custum", 90, 30);
            _printDocument.PrintPage += new PrintPageEventHandler(this.PrintDocument_PrintPage);
        }




        private void PrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            //設置打印內容及其字體,顏色和位置
            //e.Graphics.DrawString(PrintContent, new Font(new FontFamily("Arial"), 7), System.Drawing.Brushes.Black, 8, 1);
            //可以設置打印圖片
            //e.Graphics.DrawImage(Image.FromFile(imageStr), 5, 5);


            Font titleFont = new Font("黑體", 8, System.Drawing.FontStyle.Bold);//標題字體           
            Font fntTxt = new Font("宋體", 8, System.Drawing.FontStyle.Regular);//正文文字         
            Font fntTxt1 = new Font("宋體", 6, System.Drawing.FontStyle.Regular);//正文文字           
            System.Drawing.Brush brush = new SolidBrush(System.Drawing.Color.Black);//畫刷           
            //System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black);           //線條顏色         

            //try
            //{
            //    e.Graphics.DrawString("標題name", titleFont, brush, new System.Drawing.Point(20, 10));

            //    Point[] points111 = { new Point(20, 28), new Point(90, 28) };
            //    e.Graphics.DrawLines(pen, points111);

            //e.Graphics.DrawString("資產編號:", fntTxt, brush, new System.Drawing.Point(80, ));
            //e.Graphics.DrawString("123456789123465", fntTxt, brush, new System.Drawing.Point(80, 31));
            e.Graphics.DrawString(PrintContent, fntTxt, brush, new System.Drawing.Point(30, 46));
            //e.Graphics.DrawString("123456789131321", fntTxt, brush, new System.Drawing.Point(85, 46));

            //    e.Graphics.DrawString("底部name", fntTxt1, brush, new System.Drawing.Point(100, 62));


            //e.Graphics.DrawImage(bmpAndtext, new System.Drawing.Point(30, 35));

            //}
            //catch (Exception e)
            //{
            //    throw;
            //}

        }

        public void print(string[] contents)
        {
            string temp = "";
            foreach (string str in contents)
            {
                temp += str + '\n';
            }
            this.PrintContent = temp;
            _printDocument.Print();
        }

        public void print(string str)
        {
            this.PrintContent = str;
            _printDocument.Print();
        }

        public bool print(SendReciveReelData sendReciveReelData)
        {
            try
            {
                string[] tempArray = sendReciveReelData.reelInf.ID.Split('!');
                string pn = tempArray[0];
                string vendor = tempArray[1];
                string dc = tempArray[2];

                string temp = "";
                temp += "P/N: " + pn;
                temp += '\n' + "VENDOR: " + vendor;
                temp += '\n' + "QTY: " + sendReciveReelData.reelInf.QTY;
                temp += '\n' + "D/C: " + dc;
                temp += '\n' + "工單: " + sendReciveReelData.reelInf.OutInf.ERPWO;
                temp += '\n' + "線別: " + sendReciveReelData.reelInf.OutInf.ERPWO;
                temp += '\n' + "站位: " + "";

                this.PrintContent = temp;
                _printDocument.Print();
                return true;
            }
            catch
            {
                return false;
            }








        }

        public void print(LabelPrinterData data)
        {
            string temp = "";
            temp += "工單: " + data.WorkNo;
            temp += '\n' + "料號: " + data.PartNo;
            temp += '\n' + "出口: " + data.StationNo;
            temp += '\n' + "日期: " + data.Date.ToShortDateString();
            temp += '\n' + "Time: " + data.Date.ToShortTimeString();

            this.PrintContent = temp;
            _printDocument.Print();
        }


        string imageStr;
        public void printImage(string Path)
        {
            imageStr = Path;
            _printDocument.Print();
        }  
       
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章