• 斑馬打印機打印中文


    创建项目

    首先說一下,本文章是借鉴了其他大佬的文章,然后我记录一下的文章。

    首先创建好一个.net framework的winform项目。

    这里面主要用到两个库文件:
    Fnthex32.dll、LabelPrint.dll

    Fnthex32这个有8位参数和9位参数的,我这里用9位参数。
    如果下载资源要付费,请留下评论。

    这只是一个demo,展示一下斑马打印机的一个打印中文的方法。

    连接打印机方式多样,请按需调整。

    开始

    [DllImport("Fnthex32.dll")]
    private static extern int GETFONTHEX(
                        string text,//文本
                        string fontName,//字体
                        string tempName,//临时变量
                        int orient,//方向
                        int height,//字体高度
                        int width,//字体宽度
                        int isBold,//是否加粗0,1
                        int isItalic,//是否斜体0,1
                        StringBuilder cmd);//内容
    private static string printName = string.Empty;
    
    
    public Form1()
    {
        InitializeComponent();
    }
    
    private void Form1_Load(object sender, EventArgs e)
    {
        // 获取打印机名称
        GetGetPrintersList();
    }
    public static void GetGetPrintersList()
    {
        string pringZDPrintName = string.Empty;
        foreach (var item in PrinterSettings.InstalledPrinters)
        {
            if (item.ToString().Contains("ZD"))
            {
                pringZDPrintName = item.ToString();
            }
        }
        printName = pringZDPrintName;
    }
    private void button1_Click(object sender, EventArgs e)
    {
        //Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
    
        if (printName == null)
        {
            throw new ArgumentNullException("printName", "打印机名称不能为空。");
        }
        
        string sBarCodeCMD = "";            //条码打印命令
        //多行汉字打印
        string sTemp = GetFontHex("txt0", "Microsoft YaHei", "temp0");
        
        string sTemp1 = GetFontHex("txt1", "Microsoft YaHei", "temp1");
        string sTemp2 = GetFontHex("txt2", "Microsoft YaHei", "temp2");
    
        string sTemp3 = GetFontHex("txt3", "Microsoft YaHei", "temp3");
        string sTemp4 = GetFontHex("txt4", "Arial", "temp4");
        
    
        string sTemp5 = GetFontHex("txt5", "Microsoft YaHei", "temp5");
        string sTemp6 = GetFontHex("txt6", "Arial", "temp6");
    
    
        sBarCodeCMD = sTemp.ToString()+ sTemp1.ToString() + sTemp2.ToString()
            + sTemp3.ToString() 
            //+ sTemp4.ToString() 
            + sTemp5.ToString() + sTemp6.ToString() + @"
                        ^XA
                        ^JMA^LL800^PW1200^MD10^PR2^PON^LRN^LH0,0
    
                        ^FO50,100^GB750,100,1^FS
                        ^FO250,200^GB1,300,1^FS
    
                        ^FO250,130^ADN,30,30^XGtemp0,1,1^FS
    
                        ^FO50,200^GB750,100,1^FS
                        ^FO100,230^ADN,30,30^XGtemp1,1,1^FS
                        ^FO450,230^ADN,30,30^XGtemp2,1,1^FS
    
                        ^FO50,300^GB750,100,1^FS
                        ^FO100,330^ADN,30,30^XGtemp3,1,1^FS
                        ^FO450,330^ADN,30,30^XGtemp4,1,1^FS
    
                        ^FO50,400^GB750,100,1^FS
                        ^FO100,430^ADN,30,30^XGtemp5,1,1^FS
                        ^FO450,430^ADN,30,30^XGtemp6,1,1^FS
    
                        ^XZ";
       
       //	发送打印LabelPrint.dll
       LabelPrint.LabelPrint.SendStringToPrinter(printName, sBarCodeCMD);
    }
    
    
    /// 
    /// 获取文本编码
    /// 
    /// 文本
    /// 字体
    /// 临时变量
    /// 字体高度
    /// 字体宽度
    /// 是否加粗
    /// 是否斜体
    /// 
    public static string GetFontHex(string text, string fontName, string tempName,
        int height = 30, int width = 15, bool isBlod = false, bool isItalic = false)
    {
        int nIsBlod = isBlod ? 1 : 0;
        int nIsItalic = isItalic ? 1 : 0;
    
        StringBuilder context = new StringBuilder(text.Length * 1024);
        int count = GETFONTHEX(text, fontName, tempName, 1, height, width, nIsBlod, nIsItalic, context);
        string temp = " " + context.ToString();
        temp = temp.Substring(0, count);
        return temp;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114

    最后的结果是:在这里插入图片描述

  • 相关阅读:
    5-RabbitMQ工作模式-Publish/Subscribe发布与订阅模式
    【C语言精髓 之 指针】指针*、取地址&、解引用*、引用&
    IntelliJ IDEA 常用的插件
    学信息系统项目管理师第4版系列29_信息系统治理
    MATLAB算法实战应用案例精讲-【图像处理】机器视觉(基础篇)(四)
    通过shell或java操作HDFS
    LeetCode 141. 环形链表 和 142. 环形链表 II
    一、MFC介绍
    【待解决】Not a Prefab scene
    ShowDoc远程下载导致文件上传漏洞复现 CVE-2021-36440&CVE-2022-1034
  • 原文地址:https://blog.csdn.net/weixin_44556271/article/details/133712403