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中选择

 

转载于:https://www.cnblogs.com/dannyqiu/articles/2424444.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值