1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > c#调用打印机(针式打印机) 实现打印

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

时间:2024-04-20 07:05:45

相关推荐

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。