c#调用打印机(针式打印机),实现打印

方法1:【不推荐】

使用wps打印,c# 生成word,然后打印word(走弯路了)

只能通过空格实现文字的间距就很low

注意:这里使用此方法前提是电脑必须安装wps...

 public string Print(string printName, string Zhmc, string Bxr, string Dyh, string Lxdh, string Yysj, string Jlr, string Gz, string Fwnr,string repair_id)
        {
            try
            {
                string url = GetFilePath() + @"file\";
                var printList = GetLocalPrinters();
                bool isFind = false;
                foreach (var item in printList)
                {
                    if (item == printName)
                    {
                        isFind = true;
                        Externs.SetDefaultPrinter(printName);
                    }
                }
                if (!isFind)
                {
                    throw new Exception("未查找到打印机【" + printName + "】!");
                }
                if (!System.IO.Directory.Exists(url))
                {
                    System.IO.Directory.CreateDirectory(url);
                }
                XWPFDocument doc = new XWPFDocument();
                XWPFParagraph p1 = doc.CreateParagraph();
                XWPFParagraph p2 = doc.CreateParagraph();
                XWPFParagraph p3 = doc.CreateParagraph();
                XWPFParagraph p4 = doc.CreateParagraph();
                XWPFParagraph p5 = doc.CreateParagraph();
                XWPFParagraph p6 = doc.CreateParagraph();
                XWPFRun r1 = p1.CreateRun();
                XWPFRun r2 = p2.CreateRun();
                XWPFRun r3 = p3.CreateRun();
                XWPFRun r4 = p4.CreateRun();
                XWPFRun r5 = p5.CreateRun();
                XWPFRun r6 = p6.CreateRun();
                r1.SetText("                                                                                                                " + repair_id);
                r2.SetText("   " + Zhmc + "                                                                        " + Bxr);
                r3.SetText("   " + Dyh + "                                                                         " + Lxdh + "           " + Yysj);
                r4.SetText("       ");
                r5.SetText("                                                                    " + Jlr + "                            " + Gz + "");
                r6.SetText(Fwnr + "");
                //FileStream sw = File.Create(url + "print.docx");
                FileStream sw = File.OpenWrite(url + "print.docx");
                doc.Write(sw);
                sw.Close();
                FileInfo file = new FileInfo(url + "print.docx");
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                process.StartInfo.CreateNoWindow = false;
                process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                process.StartInfo.UseShellExecute = true;
                process.StartInfo.FileName = file.FullName;
                process.StartInfo.Verb = "Print";
                process.Start();
                Thread.Sleep(2000);
                //File.Delete(url + "print.docx");
                return "success";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

方法2:

直接打印:使用PrintDocument

 

 #region 测试打印
   //这里是模拟的参数,定义的全局
       public void emptydefult()
        {
            printName = "Jolimark 24-pin printer";
            Zhmc = "Zhmc";
            Bxr = "Bxr";
            Dyh = "Dyh";
            Lxdh = "Lxdh";
            Yysj = "Yysj";
            Jlr = "Jlr";
            Gz = "Gz";
            Fwnr = "Fwnr";
            repair_id = "repair_id";
        }
        private void Myprinter()
        {
            emptydefult();
            PrintDocument pd = new PrintDocument();
            pd.PrintPage += new PrintPageEventHandler(printDocument_PrintA4Page);
            pd.DefaultPageSettings.PaperSize=new PaperSize("Custom", 800, 550);
            pd.DefaultPageSettings.PrinterSettings.PrinterName = printName;       //打印机名称
                                                                                           //pd.DefaultPageSettings.Landscape = true;  //设置横向打印,不设置默认是纵向的
            pd.PrintController = new System.Drawing.Printing.StandardPrintController();
            pd.Print();
        }
        private void printDocument_PrintA4Page(object sender, PrintPageEventArgs e)
        {
            Font fntTxt1 = new Font("宋体", 12, System.Drawing.FontStyle.Bold);//正文文字     
            Font fntTxt = new Font("宋体", 10, 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(repair_id, fntTxt1, brush, new System.Drawing.Point(450, 30));
                e.Graphics.DrawString(Zhmc, fntTxt, brush, new System.Drawing.Point(120, 60));
                e.Graphics.DrawString(Bxr, fntTxt, brush, new System.Drawing.Point(400, 60));
                e.Graphics.DrawString(Dyh, fntTxt, brush, new System.Drawing.Point(120, 95));
                e.Graphics.DrawString(Lxdh, fntTxt, brush, new System.Drawing.Point(400, 95));
                e.Graphics.DrawString(Yysj, fntTxt, brush, new System.Drawing.Point(550, 95));
                e.Graphics.DrawString(Jlr, fntTxt, brush, new System.Drawing.Point(330, 130));
                e.Graphics.DrawString(Gz, fntTxt, brush, new System.Drawing.Point(450, 130));
                e.Graphics.DrawString(Fwnr, fntTxt, brush, new System.Drawing.Point(80, 160)); 
            }
            catch (Exception ee)
            {
                 
            }
        }
        #endregion

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值