using System; using System.Collections.Generic; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Data; using System.Diagnostics; using System.Drawing; using System.Drawing.Printing; using ZXing; using ZXing.Common; namespace SiaSun.LMS.WPFClient.MANAGE { public partial class MANAGE_PRINT : AvalonDock.DocumentContent { public MANAGE_PRINT() { InitializeComponent(); } private void BtnSave_Click(object sender, RoutedEventArgs e) { try { if (string.IsNullOrEmpty(this.tbGoods_code.Text)) { MessageBox.Show("物料条码为空"); return; } if (string.IsNullOrEmpty(this.tbLeft.Text)) { MessageBox.Show("左边距为空"); return; } if (string.IsNullOrEmpty(this.tbTop.Text)) { MessageBox.Show("上边距为空"); return; } if (string.IsNullOrEmpty(this.tbSize.Text)) { MessageBox.Show("放大倍数为空"); return; } string sGoodsBarcode = this.tbGoods_code.Text.Trim(); string[] strArr = sGoodsBarcode.Split('-'); if (strArr.Length != 7) { MessageBox.Show("物料条码规则不正确"); return; } //Print("Microsoft Print to PDF", sGoodsBarcode, Convert.ToSingle(this.tbLeft.Text), Convert.ToSingle(this.tbTop.Text)); bool bSuccess = true; foreach (string sPrinter in PrinterSettings.InstalledPrinters) { if (sPrinter.Contains("ZDesigner")) { Print(sPrinter, sGoodsBarcode, Convert.ToSingle(this.tbSize.Text), Convert.ToSingle(this.tbLeft.Text), Convert.ToSingle(this.tbTop.Text)); break; } } if (bSuccess) { MessageBox.Show("打印成功"); } else { MessageBox.Show("打印失败,未找到打印机"); } } catch (Exception ex) { MessageBox.Show(string.Format("打印失败:{0}", ex.ToString())); } } private void BtnClear_Click(object sender, RoutedEventArgs e) { this.tbGoods_code.Text = string.Empty; this.tbGoods_code.Focus(); } private void tbGoods_code_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { if (string.IsNullOrWhiteSpace(this.tbGoods_code.Text)) { return; } else { BtnSave_Click(null, null); } } } private void Print(string sPrinterName, string sContent, float fPageScale, float fLeft, float fTop) { string sLine1 = string.Empty; string sLine2 = string.Empty; string[] asContent = sContent.Split('-'); sLine1 = asContent[0] + "-" + asContent[1] + "-" + asContent[2]; sLine2 = "-" + asContent[3] + "-" + asContent[4] + "-" + asContent[5] + "-" + asContent[6]; PrintDocument printDocument = new PrintDocument(); printDocument.PrintController = new StandardPrintController(); printDocument.PrinterSettings.PrinterName = sPrinterName; //printDocument.DefaultPageSettings.PaperSize = new PaperSize("Custom", 100, 100); printDocument.PrintPage += (sender, e) => { Font font = new Font("Arial", 4 * fPageScale, System.Drawing.FontStyle.Regular); System.Drawing.Brush b = new SolidBrush(System.Drawing.Color.Black); Graphics g = e.Graphics; g.PageScale = fPageScale; g.PageUnit = GraphicsUnit.Millimeter; g.DrawImage(new BarcodeWriter { Options = new EncodingOptions { Width = 1000, Height = 1000, Margin = 0, PureBarcode = true }, Format = BarcodeFormat.QR_CODE, }.Write(sContent), new RectangleF(fLeft + 5f, fTop, 10f, 10f)); g.DrawString(sLine1, font, b, new PointF(fLeft, fTop + 10.5f)); g.DrawString(sLine2, font, b, new PointF(fLeft + 2f, fTop + 12.5f)); e.HasMorePages = false; }; printDocument.Print(); } } }