SystemSetting_PrinterTest.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using Seagull.BarTender.Print;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Drawing.Printing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Windows.Forms;
  11. namespace UAS_MES_NEW.SystemSetting
  12. {
  13. public partial class SystemSetting_PrinterTest : Form
  14. {
  15. Engine engine = new Engine();
  16. LabelFormatDocument format;
  17. public SystemSetting_PrinterTest()
  18. {
  19. InitializeComponent();
  20. }
  21. private void PrintTest_Click(object sender, EventArgs e)
  22. {
  23. PrintDocument print = new PrintDocument();
  24. print.PrinterSettings.PrinterName = PrinterList.Text;
  25. print.PrintPage += Print_PrintPage;
  26. print.Print();
  27. }
  28. private void Print_PrintPage(object sender, PrintPageEventArgs e)
  29. {
  30. Graphics g = e.Graphics;
  31. float leftMargin = 10f; //左边距
  32. SolidBrush myBrush = new SolidBrush(Color.Black);//刷子
  33. float yPosition = 5f;//行定位
  34. Font printFont = new Font("微软雅黑", 20f, FontStyle.Bold);//设置字体
  35. yPosition += printFont.GetHeight(g);//另起一行
  36. g.DrawString("成功连接此打印机", printFont, myBrush, leftMargin, yPosition, new StringFormat());
  37. }
  38. private void Setting_Click(object sender, EventArgs e)
  39. {
  40. PrintDialog printd = new PrintDialog();
  41. printd.PrinterSettings.PrinterName = PrinterList.Text;
  42. printd.ShowDialog();
  43. }
  44. private void Export_Click(object sender, EventArgs e)
  45. {
  46. for (int i = int.Parse(Rn.Text); i < (int.Parse(Num.Text)+ int.Parse(Rn.Text)); i = i + 4)
  47. {
  48. int temp = i;
  49. for (int j = 0; j < format.SubStrings.Count; j++)
  50. {
  51. switch (format.SubStrings[j].Name)
  52. {
  53. case "RN1":
  54. format.SubStrings[j].Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp).ToString()));
  55. break;
  56. case "RN2":
  57. format.SubStrings[j].Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 1).ToString()));
  58. break;
  59. case "RN3":
  60. format.SubStrings[j].Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 2).ToString()));
  61. break;
  62. case "RN4":
  63. format.SubStrings[j].Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 3).ToString()));
  64. break;
  65. default:
  66. break;
  67. }
  68. }
  69. format.PrintSetup.PrinterName = PrinterList.Text;
  70. format.PrintSetup.IdenticalCopiesOfLabel = 1;
  71. format.Print();
  72. }
  73. }
  74. private static string lpad(int length, string number)
  75. {
  76. while (number.Length < length)
  77. {
  78. number = "0" + number;
  79. }
  80. number = number.Substring(number.Length - length, length);
  81. return number;
  82. }
  83. private void SystemSetting_PrinterTest_Load(object sender, EventArgs e)
  84. {
  85. engine.Start();
  86. format = engine.Documents.Open(Application.StartupPath + "/Label.btw");
  87. }
  88. }
  89. }