| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- using FastReport;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Windows.Forms;
- using System.Drawing.Printing;
- namespace UAS_PRINT
- {
- class PrintHandler
- {
- //图片打印中间变量
- static int imagecount = 0;
- static int index = 0;
- //打印图片集合
- static List<Bitmap> bitmaps = new List<Bitmap>();
- static DataTable dt = new DataTable("VENDORBARCODE_VIEW");
- public static void zplprint(string str,string PrinterName)
- {
- Dictionary<string, object> data = PrintHelper.ToDictionary(str);
- //获取所有的打印格式数据
- if (data.ContainsKey("data"))
- {
- string PrintCode = data["data"].ToString().Replace(" ", "").Replace("\"", "");
- List<string> PrintList = new List<string>();
- int PrintTime = Regex.Matches(PrintCode, "XA").Count;
- for (int i = 0; i < PrintTime; i++)
- {
- PrintList.Add(PrintCode.Substring(0, PrintCode.IndexOf("XZ") + 2));
- PrintCode = PrintCode.Substring(PrintCode.IndexOf("XZ") + 2);
- }
- for (int i = 0; i < PrintList.Count; i++)
- {
- PrintHelper.SendStringToPrinter(PrinterName, PrintList[i]);
- }
- data.Clear();
- }
- else if (data.ContainsKey("exceptionInfo"))
- {
- //string PrintCode = data["exceptionInfo"].ToString();
- //MessageBox.Show(PrintCode);
- }
- }
- public static void zplprint(string str)
- {
- Dictionary<string, object> data = PrintHelper.ToDictionary(str);
- //获取所有的打印格式数据
- if (data.ContainsKey("data"))
- {
- string PrintCode = data["data"].ToString().Replace(" ", "").Replace("\"", "").Replace("\\n","");
- string PrinterName = data["uid"].ToString().Replace("\"", "");
- if (!PrintCode.StartsWith("^XA"))
- {
- return;
- }
- PrintHelper.SendStringToPrinter(PrinterName, PrintCode);
- PrintDocument pd = new PrintDocument();
- pd.PrinterSettings.PrinterName = PrinterName;
- data.Clear();
- }
- else if (data.ContainsKey("exceptionInfo"))
- {
- //string PrintCode = data["exceptionInfo"].ToString();
- //MessageBox.Show(PrintCode);
- }
- }
- public static void vendorZplPrint( PrinterList print, string PrinterName,string str) {
- Report Report = new Report();
- RadioButton PRFR = (RadioButton)print.Controls["PRFR"];
- RadioButton PRPIC = (RadioButton)print.Controls["PRPIC"];
- if (PRFR.Checked)
- {
- try
- {
- Report.Load(System.AppDomain.CurrentDomain.BaseDirectory + "GD.frx");
- }
- catch
- {
- MessageBox.Show("未找到可使用的标签文件");
- return;
- }
- }
- bitmaps.Clear();
- Dictionary<string, object> data = PrintHelper.ToDictionary(str);
- List<Dictionary<string, object>> parameter = new List<Dictionary<string, object>>();
- List<Dictionary<string, object>> barcode = new List<Dictionary<string, object>>();
- parameter = (List<Dictionary<string, object>>)data["parameter"];
- barcode = (List<Dictionary<string, object>>)data["barcode"];
- int la_width = int.Parse(parameter.First()["LA_WIDTH"].ToString());
- int la_height = int.Parse(parameter.First()["LA_HEIGHT"].ToString());
- foreach (var item in barcode)
- {
- Bitmap bit = new Bitmap(la_width * 10, la_height * 4);
- using (Graphics g = Graphics.FromImage(bit))
- {
- //g.CompositingQuality = CompositingQuality.HighQuality;
- //g.SmoothingMode = SmoothingMode.HighQuality;
- //g.InterpolationMode = InterpolationMode.HighQualityBicubic;
- ////定义一枝画笔
- //Pen pen = new Pen(Color.Black);
- ////用这个画笔在左上角画一个矩形
- //g.DrawRectangle(pen, new Rectangle(10, 10, 50, 20));
- foreach (var itemc in item)
- {
- Dictionary<string, object> Sidc = parameter.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper());
- if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "text")
- {
- string itemstr = "";
- if (itemc.Value != null)
- {
- itemstr = itemc.Value.ToString();
- while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n"))
- {
- itemstr = itemstr.Remove(0, 2);
- }
- if (itemstr.StartsWith("\""))
- {
- itemstr = itemstr.Remove(0, 1);
- }
- while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n"))
- {
- itemstr = itemstr.Remove(itemstr.Length - 2);
- }
- if (itemstr.EndsWith("\""))
- {
- itemstr = itemstr.Remove(itemstr.Length - 1);
- }
- if (itemstr.Contains("\\\""))
- {
- itemstr = itemstr.Replace("\\\"", "\"");
- }
- }
- if (PRFR.Checked)
- {
- DataColumn DC = new DataColumn(itemc.Key, Type.GetType("System.String"));
- if (!dt.Columns.Contains(itemc.Key))
- dt.Columns.Add(DC);
- }
- if (PRPIC.Checked)
- {
- PrintHelper.Printstring(itemc.Value != null ? itemstr : " ", int.Parse(Sidc["LP_SIZE"].ToString()), g, int.Parse(Sidc["LP_LEFTRATE"].ToString()), int.Parse(Sidc["LP_TOPRATE"].ToString()));
- }
- }
- else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode")
- {
- if (PRFR.Checked)
- {
- DataColumn DC = new DataColumn(itemc.Key, Type.GetType("System.String"));
- if (!dt.Columns.Contains(itemc.Key))
- dt.Columns.Add(DC);
- }
- if (PRPIC.Checked)
- {
- PrintHelper.DrawBarcode(itemc.Value != null ? itemc.Value.ToString().Replace("\"", "") : " ", ref bit, double.Parse(Sidc["LP_LEFTRATE"].ToString()), double.Parse(Sidc["LP_TOPRATE"].ToString()), double.Parse(Sidc["LP_WIDTH"].ToString()), double.Parse(Sidc["LP_HEIGHT"].ToString()) - 2);
- PrintHelper.Printstring(itemc.Value != null ? itemc.Value.ToString().Replace("\"", "") : " ", int.Parse(Sidc["LP_SIZE"].ToString()), g, int.Parse(Sidc["LP_LEFTRATE"].ToString()), int.Parse(Sidc["LP_TOPRATE"].ToString()) + double.Parse(Sidc["LP_HEIGHT"].ToString()) - 2);
- }
- }
- }
- }
- bitmaps.Add(bit);
- }
- if (PRFR.Checked)
- {
- foreach (var item in barcode)
- {
- DataRow dr = dt.NewRow();
- //g.CompositingQuality = CompositingQuality.HighQuality;
- //g.SmoothingMode = SmoothingMode.HighQuality;
- //g.InterpolationMode = InterpolationMode.HighQualityBicubic;
- ////定义一枝画笔
- //Pen pen = new Pen(Color.Black);
- ////用这个画笔在左上角画一个矩形
- //g.DrawRectangle(pen, new Rectangle(10, 10, 50, 20));
- foreach (var itemc in item)
- {
- Dictionary<string, object> Sidc = parameter.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper());
- if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "text")
- {
- string itemstr = "";
- if (itemc.Value != null)
- {
- itemstr = itemc.Value.ToString();
- while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n"))
- {
- itemstr = itemstr.Remove(0, 2);
- }
- if (itemstr.StartsWith("\""))
- {
- itemstr = itemstr.Remove(0, 1);
- }
- while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n"))
- {
- itemstr = itemstr.Remove(itemstr.Length - 2);
- }
- if (itemstr.EndsWith("\""))
- {
- itemstr = itemstr.Remove(itemstr.Length - 1);
- }
- if (itemstr.Contains("\\\""))
- {
- itemstr = itemstr.Replace("\\\"", "\"");
- Console.WriteLine(itemstr);
- }
- }
- dr[itemc.Key] = itemstr;
- }
- else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode")
- {
- dr[itemc.Key] = itemc.Value.ToString().Replace("\"", "");
- }
- }
- dt.Rows.Add(dr);
- }
- }
- if (PRPIC.Checked)
- {
- imagecount = bitmaps.Count;
- ////设置打印机名称
- print.PrintDoc.PrintPage += PrintPage;
- print.PrintDoc.Print();
- }
- if (PRFR.Checked)
- {
- Report.RegisterData(dt, "VENDORBARCODE_VIEW");
- Report.GetDataSource("VENDORBARCODE_VIEW").Enabled = true;
- Report.PrintSettings.ShowDialog = false;
- Report.PrintSettings.Printer = PrinterName;
- Report.Print();
- dt.Clear();
- }
- }
- private static void PrintPage(object sender, PrintPageEventArgs e)
- {
- Image i = bitmaps[index];
- Graphics g = e.Graphics;
- g.DrawImage(i, 4, 10, i.Width, i.Height);
- if (index < imagecount - 1)
- {
- e.HasMorePages = true;
- index++;
- }
- else
- {
- index = 0;
- e.HasMorePages = false;
- }
- }
- }
- }
|