C#調用小票打印機


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Drawing.Printing;
namespace BNCheckItemsClient.FormC.Specimen
{
    publicclass PrintSpecimenLabel
    {
        PrintDocument printDocument;
        privateint _PrintPage =0;//當前打印頁
         privateint _TotalPage =1;//總頁數 
         publicstring _PrinterName =string.Empty;// 打印機名稱
        publicvoid DoPrint()
        {
            try
            {
                //准備數據
                PrepareData();
               
                if (_TotalPage <=0)
                    return;
                //設置打印機
                PrinterSetup();
                if (!string.IsNullOrEmpty(_PrinterName))
                {
                    printDocument.PrinterSettings.PrinterName = _PrinterName;
                    if (!printDocument.PrinterSettings.IsValid)
                    {
                        thrownew Exception("The printer is not Valid");
                    }
                }
                printDocument.Print();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //throw;
            }
        }
        privatevoid PrinterSetup()
        {
            //設置打印機屬性
            printDocument.PrinterSettings.PrinterName ="ZDesigner 888-TT";//設置打印機
            printDocument.DefaultPageSettings.PaperSize =new System.Drawing.Printing.PaperSize("SpecimenLabel",110, 180);//頁面大小
            printDocument.DefaultPageSettings.Landscape =true;//橫向打印
            printDocument.PrintPage +=new PrintPageEventHandler(printDocument_PrintPage);
         }
        //在這里寫打印的內容
        void printDocument_PrintPage(object sender, PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;
            float leftMargin = 5f; //左邊距
              SolidBrush myBrush =new SolidBrush(Color.Black);//刷子
              float yPosition = 5f;//行定位
            Font printFont =new Font("宋體", 20f, FontStyle.Bold);//設置字體
            g.DrawString("這是要打印的第一行內容",printFont, myBrush, leftMargin + 140f, 7f, new StringFormat());
            yPosition += printFont.GetHeight(g);//另起一行
              printFont =new Font("宋體", 10f, FontStyle.Bold);//改變字體
            g.DrawString("這是要打印的第二行內容", printFont, myBrush, leftMargin, yPosition, new StringFormat());
            //如果要同時打印多個標簽
              _PrintPage++;//頁號
            if (_PrintPage < _TotalPage)
            { 
                e.HasMorePages =true;
            }
            else
            {
                e.HasMorePages =false;
            }
        }
    }
}

到打印機和傳真文件夾-->右鍵-->服務器屬性
添加了自己定義的紙類型 名稱949W300H 寬9.49in,高3.00in

所以改了程序為

foreach(PaperSize ps in printDoc.PrinterSettings.PaperSizes)
{
 if(ps.PaperName=="949W300H")
 {
  printDoc.PrinterSettings.DefaultPageSettings.PaperSize=ps;
  printDoc.DefaultPageSettings.PaperSize=ps;
 }
}

 


就可以了 似乎紙張只能從printDoc.PrinterSettings.PaperSizes中選擇


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM