123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936 |
- using CefSharp;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Linq;
- using System.Text.RegularExpressions;
- using System.Windows.Forms;
- using System.Drawing.Printing;
- using FastReport;
- using System.Data;
- using System.Threading;
- using System.Net;
- using System.Text;
- using System.Web;
- using System.Diagnostics;
- using UAS_Web.Properties;
- namespace UAS_Web.tool
- {
- class RequestHandler : IRequestHandler
- {
- public event Action<byte[]> NotifyMsg;
- //打印图片集合
- List<Bitmap> bitmaps = new List<Bitmap>();
- Report BarcodeReport = new Report();
- Dictionary<string, object> itemlast;
- Dictionary<string, object> itemBox;
- //图片打印中间变量
- int imagecount = 0;
- int index = 0;
- string PrinterName = "";
- string PrintType = "";
- PrinterList print;
- string LABELNAME = "";
- string labelview = "";
- Dictionary<string, object> DataCollection = null;
- //打印全部标签所需变量
- string AllLabelname = "";
- string AllBoxname = "";
- //打印类型,箱号条码统一打印或者是单一打印
- string PType = "";
- /// <summary>
- /// 拦截指定请求
- /// </summary>
- /// <param name="browserControl"></param>
- /// <param name="browser"></param>
- /// <param name="frame"></param>
- /// <param name="request"></param>
- /// <param name="response"></param>
- /// <returns></returns>
- IResponseFilter IRequestHandler.GetResourceResponseFilter(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
- {
- MessageFilter filter;
- switch (request.Url.Substring(request.Url.LastIndexOf("/") + 1))
- {
- case "zplPrinter":
- filter = FilterManager.CreateFilter(request.Identifier.ToString()) as MessageFilter;
- return filter;
- case "zplPrint.action":
- filter = FilterManager.CreateFilter(request.Identifier.ToString()) as MessageFilter;
- return filter;
- case "getPrintType.action":
- filter = FilterManager.CreateFilter(request.Identifier.ToString()) as MessageFilter;
- return filter;
- case "vendorZplPrint.action":
- filter = FilterManager.CreateFilter(request.Identifier.ToString()) as MessageFilter;
- return filter;
- default:
- break;
- }
- return null;
- }
- /// <summary>
- /// 完成响应后获取Js返回的数据
- /// </summary>
- /// <param name="browserControl"></param>
- /// <param name="browser"></param>
- /// <param name="frame"></param>
- /// <param name="request"></param>
- /// <param name="response"></param>
- /// <param name="status"></param>
- /// <param name="receivedContentLength"></param>
- void IRequestHandler.OnResourceLoadComplete(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
- {
- MessageFilter filter;
- string str;
- Dictionary<string, object> data;
- //打印条码时总页数与当前页变量
- switch (request.Url.Substring(request.Url.LastIndexOf("/") + 1))
- {
- case "zplPrinter":
- var m = request.Method;
- if (request.Method == "POST")
- {
- using (var postData = request.PostData)
- {
- if (postData != null)
- {
- var elements = postData.Elements;
- var charSet = request.GetCharSet();
- foreach (var element in elements)
- {
- if (element.Type == PostDataElementType.Bytes)
- {
- string body = HttpUtility.UrlDecode(element.GetBody(charSet),Encoding.UTF8);
- LABELNAME = body.Substring(body.LastIndexOf('=')+1);
- }
- }
- }
- }
- }
- PType = "normal";
- //弹出打印机选择列表
- print = new PrinterList(browser) { StartPosition = FormStartPosition.CenterScreen,TopMost=true };
-
- print.Controls["Confirm"].Click += RequestHandler_Click;
- print.PrintDoc.PrintPage += PrintPage;
- print.ShowDialog();
- break;
- case "zplPrinterAll":
- if (request.Method == "POST")
- {
- using (var postData = request.PostData)
- {
- if (postData != null)
- {
- var elements = postData.Elements;
- var charSet = request.GetCharSet();
- foreach (var element in elements)
- {
- if (element.Type == PostDataElementType.Bytes)
- {
- string body = HttpUtility.UrlDecode(element.GetBody(charSet), Encoding.UTF8);
- //LABELNAME = body.Substring(body.LastIndexOf('=') + 1);
- AllLabelname = body.Substring(body.IndexOf("la_name=") + 8, body.IndexOf("&la_idBox")- (body.IndexOf("la_name=") + 8));
- AllBoxname = body.Substring(body.LastIndexOf('=') + 1);
- }
- }
- }
- }
- }
- PType = "all";
- //弹出打印机选择列表
- print = new PrinterList(browser) { StartPosition = FormStartPosition.CenterScreen, TopMost = true };
- print.Controls["Confirm"].Click += RequestHandler_Click;
- print.PrintDoc.PrintPage += PrintPage;
- print.ShowDialog();
- break;
- case "zplPrint.action":
- filter = FilterManager.GetFileter(request.Identifier.ToString()) as MessageFilter;
- str = System.Text.Encoding.UTF8.GetString(filter.dataAll.ToArray());
- data = 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);
- }
-
- break;
- case "getPrintType.action":
- filter = FilterManager.GetFileter(request.Identifier.ToString()) as MessageFilter;
- str = System.Text.Encoding.UTF8.GetString(filter.dataAll.ToArray());
- data = ToDictionary(str);
- PrintType = data["data"].ToString().Replace("\"", "");
- break;
- case "vendorZplPrint.action":
- try
- {
- filter = FilterManager.GetFileter(request.Identifier.ToString()) as MessageFilter;
- str = System.Text.Encoding.UTF8.GetString(filter.dataAll.ToArray());
- if (str.Contains("不允许打印")||(str.Contains("exceptionInfo")))
- return;
- data = ToDictionary(str);
- if (DataCollection == null)
- {
- DataCollection = new Dictionary<string, object>();
- DataCollection.Add(((string)data["currentPage"]).Replace("\"", ""), data);
- }
- if (DataCollection.Count < int.Parse(((string)data["totalPage"]).Replace("\"", "")))
- {
- if(!DataCollection.ContainsKey(((string)data["currentPage"]).Replace("\"", "")))
- DataCollection.Add(((string)data["currentPage"]).Replace("\"", ""), data);
- }
- if (DataCollection.Count == int.Parse(((string)data["totalPage"]).Replace("\"", "")))
- {
- if (PType == "normal")
- {
- for (int i = 0; i < DataCollection.Count; i++)
- {
- vendorZplPrint(DataCollection[(i + 1).ToString()]);
- }
- }
- else
- {
- Console.WriteLine("打印ALL");
- for (int i = 0; i < DataCollection.Count; i++)
- {
- vendorZplPrintAll(DataCollection[(i + 1).ToString()]);
- }
- }
- DataCollection = null;
- }
- //vendorZplPrint(data);
- //vendorZplPrintAll(data);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message + "请重新进行打印");
- DataCollection = null;
- return;
- }
- break;
- default:
- break;
- }
- }
- public void vendorZplPrint(Object data)
- {
- DataTable dt = new DataTable();
- RadioButton PRFR = (RadioButton)print.Controls["PRFR"];
- RadioButton PRPIC = (RadioButton)print.Controls["PRPIC"];
- string type;
- bitmaps.Clear();
- List<Dictionary<string, object>> parameter = new List<Dictionary<string, object>>();
- List<Dictionary<string, object>> barcode = new List<Dictionary<string, object>>();
- try
- {
- parameter = (List<Dictionary<string, object>>)((Dictionary<string, object>)data)["parameter"];
- barcode = (List<Dictionary<string, object>>)((Dictionary<string, object>)data)["barcode"];
- }
- catch {
- MessageBox.Show("打印数据异常,请联系管理员");
- return;
- }
- try
- {
- type = ((String)((Dictionary<string, object>)data)["type"]).Replace("\"", "");
- }
- catch
- {
- type = "no";
- }
- try
- {
- labelview = ((String)((Dictionary<string, object>)data)["label_type"]).Replace("\"", "");
- }
- catch
- {
- labelview = "";
- }
- if (PRFR.Checked)
- {
- Thread.Sleep(1000);
- int i = 0;
- while (DownLoadFile.downloading && i < 2)
- {
- Thread.Sleep(1000);
- i++;
- }
- if (DownLoadFile.downloading)
- MessageBox.Show("标签更新有误");
- try
- {
- BarcodeReport.Load(System.AppDomain.CurrentDomain.BaseDirectory + LABELNAME + ".frx");
- }
- catch
- {
- MessageBox.Show("未找到可使用的标签文件,请重试");
- return;
- }
- }
- int la_width = 1;
- int la_height = 1;
- foreach (var item in barcode)
- {
- Bitmap bit = new Bitmap(la_width * 10, la_height * 4);
- using (Graphics g = Graphics.FromImage(bit))
- {
- foreach (var itemc in item)
- {
- Dictionary<string, object> Sidc = parameter.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper());
- if (Sidc != null)
- {
- 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)
- {
- for (int i = 0; i < Settings.Default.Printnum; i++)
- {
- 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 != null)
- {
- 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("\\\"", "\"");
- }
- }
- 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)
- {
- if (bitmaps.Count > 0)
- {
- imagecount = bitmaps.Count;
- print.PrintDoc.Print();
- }
- }
- if (PRFR.Checked)
- {
- try
- {
- if (labelview == "")
- {
- if (type == "box")
- {
- BarcodeReport.RegisterData(dt, "VENDORBARCODE_BOX_VIEW");
- BarcodeReport.GetDataSource("VENDORBARCODE_BOX_VIEW").Enabled = true;
- BarcodeReport.PrintSettings.ShowDialog = false;
- BarcodeReport.PrintSettings.Printer = PrinterName;
- BarcodeReport.Print();
- }
- else
- {
- BarcodeReport.RegisterData(dt, "VENDORBARCODE_VIEW");
- BarcodeReport.GetDataSource("VENDORBARCODE_VIEW").Enabled = true;
- BarcodeReport.PrintSettings.ShowDialog = false;
- BarcodeReport.PrintSettings.Printer = PrinterName;
- BarcodeReport.Print();
- }
- }
- else
- {
- BarcodeReport.RegisterData(dt, labelview);
- BarcodeReport.GetDataSource(labelview).Enabled = true;
- BarcodeReport.PrintSettings.ShowDialog = false;
- BarcodeReport.PrintSettings.Printer = PrinterName;
- BarcodeReport.Print();
- }
- }
- catch(Exception ex)
- {
- MessageBox.Show(ex.Message+"打印出现错误,请检查标签文件选择是否正确");
- }
- dt.Clear();
-
- }
- }
- string nowboxcode = "";
- public void vendorZplPrintAll(Object data)
- {
- //条码标签结构datatable
- DataTable dt = new DataTable();
- //箱号标签结构datatable
- DataTable box = new DataTable();
- //记录最新打印出来的箱号
- string type;
- //箱号对应视图名
- string box_type;
- bitmaps.Clear();
- List<Dictionary<string, object>> parameter = new List<Dictionary<string, object>>();
- List<Dictionary<string, object>> BoxParameter = new List<Dictionary<string, object>>();
- //List<Dictionary<string, object>> parameter2 = new List<Dictionary<string, object>>();
- List<Dictionary<string, object>> barcode = new List<Dictionary<string, object>>();
- List<Dictionary<string, object>> BoxBarcode = new List<Dictionary<string, object>>();
- try
- {
- parameter = (List<Dictionary<string, object>>)((Dictionary<string, object>)data)["parameter"];
- BoxParameter = (List<Dictionary<string, object>>)((Dictionary<string, object>)data)["BoxParameter"];
- barcode = (List<Dictionary<string, object>>)((Dictionary<string, object>)data)["barcode"];
- BoxBarcode = (List<Dictionary<string, object>>)((Dictionary<string, object>)data)["BoxBarcode"];
- }
- catch(Exception ex)
- {
- MessageBox.Show(ex.Message+"打印数据异常,请联系管理员");
- return;
- }
- try
- {
- type = ((String)((Dictionary<string, object>)data)["type"]).Replace("\"", "");
- }
- catch
- {
- type = "no";
- }
- try
- {
- labelview = ((String)((Dictionary<string, object>)data)["label_type"]).Replace("\"", "");
- }
- catch
- {
- labelview = "";
- }
- try
- {
- box_type = ((String)((Dictionary<string, object>)data)["BoxLabel_type"]).Replace("\"", "");
- }
- catch
- {
- box_type = "";
- }
- Thread.Sleep(1000);
- int k = 0;
- while (DownLoadFile.downloading && k < 2)
- {
- Thread.Sleep(1000);
- k++;
- }
- if (DownLoadFile.downloading)
- MessageBox.Show("标签更新有误");
-
- //barcode条码datable格式
- foreach (var item in parameter)
- {
- foreach (var itemc in item)
- {
- string itemstr = "";
- if (itemc.Value != null && itemc.Key.ToString().ToUpper() == "LP_NAME")
- {
- 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("\\\"", "\"");
- }
- DataColumn DC = new DataColumn(itemstr, Type.GetType("System.String"));
- if (!dt.Columns.Contains(itemc.Key))
- dt.Columns.Add(DC);
- }
- }
- }
- //box箱号datatable格式
- foreach (var item in BoxParameter)
- {
- foreach (var itemc in item)
- {
- string itemstr = "";
- if (itemc.Value != null && itemc.Key.ToString().ToUpper() == "LP_NAME")
- {
- 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("\\\"", "\"");
- }
- DataColumn DC = new DataColumn(itemstr, Type.GetType("System.String"));
- if (!box.Columns.Contains(itemc.Key))
- box.Columns.Add(DC);
- }
- }
- }
-
- for (int i = 0; i < barcode.Count; i++)
- {
- Dictionary<string, object> item = barcode[i];
- if (i == 0 && item["BAN_OUTBOXCODE"].ToString() != nowboxcode && nowboxcode != "")
- {
- try
- {
- itemBox = BoxBarcode.Find(p => p["BAN_OUTBOXCODE"].ToString().Replace(" ", "").Replace("\"", "") == itemlast["BAN_OUTBOXCODE"].ToString().Replace(" ", "").Replace("\"", ""));
- FillPrintData(box, itemBox, BoxParameter);
- PrintLabel(box, box_type, AllBoxname);
- Console.WriteLine("一箱" + itemlast["BAN_OUTBOXCODE"].ToString());
- }
- catch
- {
- nowboxcode = "";
- }
- }
- if (i > 0 && item["BAN_OUTBOXCODE"].ToString() != barcode[i - 1]["BAN_OUTBOXCODE"].ToString())
- {
- PrintLabel(dt,labelview, AllLabelname);
- itemlast = barcode[i - 1];
- try
- {
- itemBox = BoxBarcode.Find(p => p["BAN_OUTBOXCODE"].ToString().Replace(" ", "").Replace("\"", "") == itemlast["BAN_OUTBOXCODE"].ToString().Replace(" ", "").Replace("\"", ""));
- FillPrintData(box, itemBox, BoxParameter);
- PrintLabel(box, box_type, AllBoxname);
- Console.WriteLine("一箱" + itemlast["BAN_OUTBOXCODE"].ToString());
- }
- catch {}
- }
- FillPrintData(dt, item, parameter);
- }
- PrintLabel(dt, labelview,AllLabelname);
- itemlast = barcode[barcode.Count-1];
- if (((String)((Dictionary<string, object>)data)["currentPage"]).Replace("\"", "") == ((String)((Dictionary<string, object>)data)["totalPage"]).Replace("\"", ""))
- {
- try
- {
- itemBox = BoxBarcode.Find(p => p["BAN_OUTBOXCODE"].ToString().Replace(" ", "").Replace("\"", "") == itemlast["BAN_OUTBOXCODE"].ToString().Replace(" ", "").Replace("\"", ""));
- FillPrintData(box, itemBox, BoxParameter);
- PrintLabel(box, box_type, AllBoxname);
- Console.WriteLine("一箱" + itemBox["BAN_OUTBOXCODE"].ToString());
- }
- catch { }
- nowboxcode = "";
- }
- else {
- nowboxcode = itemlast["BAN_OUTBOXCODE"].ToString();
- }
- dt.Clear();
- }
- //给datable添加行(添加打印具体数据)
- private void FillPrintData(DataTable dt, Dictionary<string, object> item, List<Dictionary<string, object>> parameter1) {
- DataRow dr = dt.NewRow();
- foreach (var itemc in item)
- {
- Dictionary<string, object> Sidc = parameter1.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper());
- if (Sidc != null)
- {
- 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("\\\"", "\"");
- }
- }
- dr[itemc.Key] = itemstr;
- }
- else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode")
- {
- dr[itemc.Key] = itemc.Value.ToString().Replace("\"", "");
- }
- }
- }
- dt.Rows.Add(dr);
- }
- private void PrintLabel(DataTable dt,string labelview,string LABELNAME)
- {
- try
- {
- BarcodeReport.Load(System.AppDomain.CurrentDomain.BaseDirectory + LABELNAME + ".frx");
- }
- catch
- {
- MessageBox.Show("未找到可使用的标签文件,请重试");
- return;
- }
- try
- {
- BarcodeReport.RegisterData(dt, labelview);
- BarcodeReport.GetDataSource(labelview).Enabled = true;
- BarcodeReport.PrintSettings.ShowDialog = false;
- BarcodeReport.PrintSettings.Printer = PrinterName;
- BarcodeReport.Print();
- CleanDataTable(dt);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message + "打印出现错误,请检查标签文件选择是否正确");
- }
- }
- private 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;
- }
- }
- private void RequestHandler_Click(object sender, EventArgs e)
- {
- //设置打印机名称
- print.PrintDoc.PrinterSettings.PrinterName = print.Controls["Printer"].Text;
- PrinterName = print.Controls["Printer"].Text;
- RadioButton PRXA = (RadioButton)print.Controls["PRXA"];
- RadioButton PRPIC = (RadioButton)print.Controls["PRPIC"];
- int a = int.Parse(print.Controls["comboBox1"].Text);
- Settings.Default.Printnum = a;
- Settings.Default.Save();
- //获取矩阵图的分辨率
- Graphics gr = print.PrintDoc.PrinterSettings.CreateMeasurementGraphics();
- print.browser.FocusedFrame.ExecuteJavaScriptAsync("(function(value,value1){dpi=value,printType=value1})('" + gr.DpiX + "','" + PrintType + "')");
- if (PType == "all")
- {
- print.browser.FocusedFrame.ExecuteJavaScriptAsync("document.getElementById('confirmBarcodeAndBoxPrint').click();");
- }
- else
- {
- if (PRXA.Checked)
- {
- print.browser.FocusedFrame.ExecuteJavaScriptAsync("document.getElementById('confirmZplPrint').click();");
- }
- else
- {
- print.browser.FocusedFrame.ExecuteJavaScriptAsync("document.getElementById('confirmPicturePrint').click();");
- }
- }
- print.Close();
- }
- void filter_NotifyData(byte[] data)
- {
- if (NotifyMsg != null)
- {
- NotifyMsg(data);
- }
- }
- public bool GetAuthCredentials(IWebBrowser browserControl, IBrowser browser, IFrame frame, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback)
- {
- return false;
- }
- public bool OnBeforeBrowse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, bool isRedirect)
- {
- return false;
- }
- public CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
- {
- return CefReturnValue.Continue;
- }
- public bool OnCertificateError(IWebBrowser browserControl, IBrowser browser, CefErrorCode errorCode, string requestUrl, ISslInfo sslInfo, IRequestCallback callback)
- {
- return false;
- }
- public bool OnOpenUrlFromTab(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, WindowOpenDisposition targetDisposition, bool userGesture)
- {
- return false;
- }
- public void OnPluginCrashed(IWebBrowser browserControl, IBrowser browser, string pluginPath)
- {
- }
- public bool OnProtocolExecution(IWebBrowser browserControl, IBrowser browser, string url)
- {
- return false;
- }
- public bool OnQuotaRequest(IWebBrowser browserControl, IBrowser browser, string originUrl, long newSize, IRequestCallback callback)
- {
- return false;
- }
- public void OnRenderProcessTerminated(IWebBrowser browserControl, IBrowser browser, CefTerminationStatus status)
- {
- }
- public void OnRenderViewReady(IWebBrowser browserControl, IBrowser browser)
- {
- }
- public void OnResourceLoadComplete(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
- {
- }
- public void OnResourceRedirect(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, ref string newUrl)
- {
- }
- bool IRequestHandler.OnResourceResponse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
- {
- return false;
- }
- public void GetData()
- {
- }
- public static void CleanDataTable(DataTable dt)
- {
- for (int i = dt.Rows.Count - 1; i >= 0; i--)
- dt.Rows.Remove(dt.Rows[i]);
- }
- public static Dictionary<string, object> ToDictionary(string JsonData)
- {
- object Data = null;
- Dictionary<string, object> Dic = new Dictionary<string, object>();
- if (JsonData.StartsWith("["))
- {
- //如果目标直接就为数组类型,则将会直接输出一个Key为List的List<Dictionary<string, object>>集合
- //使用示例List<Dictionary<string, object>> ListDic = (List<Dictionary<string, object>>)Dic["List"];
- List<Dictionary<string, object>> List = new List<Dictionary<string, object>>();
- MatchCollection ListMatch = Regex.Matches(JsonData, @"{[\s\S]+?}");//使用正则表达式匹配出JSON数组
- foreach (Match ListItem in ListMatch)
- {
- List.Add(ToDictionary(ListItem.ToString()));//递归调用
- }
- Data = List;
- Dic.Add("List", Data);
- }
- else
- {
- MatchCollection Match = Regex.Matches(JsonData, @"""(.+?)"": {0,1}(\[[\s\S]+?\}\s*\]|null|"".+?""(,+?)|"".+?""(\s*?)(\}+?)|-{0,1}\d*)");//使用正则表达式匹配出JSON数据中的键与值
- foreach (Match item in Match)
- {
- try
- {
- if (item.Groups[2].ToString().StartsWith("["))
- {
- //如果目标是数组,将会输出一个Key为当前Json的List<Dictionary<string, object>>集合
- //使用示例List<Dictionary<string, object>> ListDic = (List<Dictionary<string, object>>)Dic["Json中的Key"];
- List<Dictionary<string, object>> List = new List<Dictionary<string, object>>();
- MatchCollection ListMatch = Regex.Matches(item.Groups[2].ToString(), @"{[\s\S]+?}");//使用正则表达式匹配出JSON数组
- foreach (Match ListItem in ListMatch)
- {
- List.Add(ToDictionary(ListItem.ToString()));//递归调用
- }
- Data = List;
- }
- else if (item.Groups[2].ToString().ToLower() == "null") Data = null;//如果数据为null(字符串类型),直接转换成null
- else if (item.Groups[2].ToString().EndsWith(","))
- Data = item.Groups[2].ToString().Remove(item.Groups[2].ToString().Length - 1); //数据为数字、字符串中的一类,则去掉,直接写入
- else if (item.Groups[2].ToString().EndsWith("}"))
- Data = item.Groups[2].ToString().Remove(item.Groups[2].ToString().Length - 1); //数据为数字、字符串中的一类,则去掉,直接写入
- else
- Data = item.Groups[2].ToString();
- Dic.Add(item.Groups[1].ToString(), Data);
- }
- catch { }
- }
- }
- return Dic;
- }
- }
- }
|