|
|
|
using BBWY.Client.Helpers;
|
|
|
|
using BBWY.Client.Models;
|
|
|
|
using BBWY.Controls;
|
|
|
|
using System;
|
|
|
|
using System.Drawing;
|
|
|
|
using System.Drawing.Printing;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Windows;
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
using BBWY.Client.Extensions;
|
|
|
|
using System.Reflection;
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
namespace BBWY.Client.Views.PackTask
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// PrintWindow.xaml 的交互逻辑
|
|
|
|
/// </summary>
|
|
|
|
public partial class PrintWindow : BWindow
|
|
|
|
{
|
|
|
|
public PrintWindow()
|
|
|
|
{
|
|
|
|
InitializeComponent();
|
|
|
|
InitPrintList();
|
|
|
|
this.DataContext = this;
|
|
|
|
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
|
|
/// 获取打印机名称
|
|
|
|
/// </summary>
|
|
|
|
public void InitPrintList()
|
|
|
|
{
|
|
|
|
|
|
|
|
PrintList = new ObservableCollection<string>();
|
|
|
|
var printingNames = PrinterSettings.InstalledPrinters;//获取本机的打印机数据
|
|
|
|
int index = -1;
|
|
|
|
int selectIndex = 0;
|
|
|
|
foreach (string name in printingNames)
|
|
|
|
{
|
|
|
|
if (name == "Microsoft XPS Document Writer" || name == "Microsoft Print to PDF" || name == "Fax")
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
index++;
|
|
|
|
if (name.Contains("Deli"))
|
|
|
|
{
|
|
|
|
selectIndex = index;
|
|
|
|
}
|
|
|
|
PrintList.Add(name);
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var applicationPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
|
|
|
string printNames = System.IO.Path.Combine(applicationPath, "handPrintBarCerName.init");
|
|
|
|
if (File.Exists(printNames))
|
|
|
|
{
|
|
|
|
PrintName = File.ReadAllText(printNames);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (PrintList.Count > 0)
|
|
|
|
{
|
|
|
|
PrintName = PrintList[0].ToString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ObservableCollection<GoodsNumberCer> GoodsNumberCerList { get; set; }
|
|
|
|
/// <summary>
|
|
|
|
/// 加载预览打印数据
|
|
|
|
/// </summary>
|
|
|
|
public void LoadData()
|
|
|
|
{
|
|
|
|
// PrintData data = new PrintData();
|
|
|
|
if (CertificateModel != null)
|
|
|
|
{
|
|
|
|
GoodsNumberCerList = new ObservableCollection<GoodsNumberCer>();
|
|
|
|
foreach (CertificateModel certificateModel in CertificateModel)
|
|
|
|
GoodsNumberCerList.Add(new GoodsNumberCer
|
|
|
|
{
|
|
|
|
CertificateModel = certificateModel.Copy(),
|
|
|
|
});
|
|
|
|
bar.Visibility = Visibility.Collapsed;
|
|
|
|
cer.Visibility = Visibility.Visible;
|
|
|
|
}
|
|
|
|
if (BarCodeModel != null)
|
|
|
|
{
|
|
|
|
cer.Visibility = Visibility.Collapsed;
|
|
|
|
bar.Visibility = Visibility.Visible;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private CertificateModel[] certificateModel;
|
|
|
|
public CertificateModel[] CertificateModel { get => certificateModel; set { Set(ref certificateModel, value); } }
|
|
|
|
|
|
|
|
private BarCodeModel barCodeModel;
|
|
|
|
public BarCodeModel BarCodeModel { get=> barCodeModel; set {Set(ref barCodeModel,value); } }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 打印机列表
|
|
|
|
/// </summary>
|
|
|
|
public ObservableCollection<string> PrintList { get; set; }
|
|
|
|
public string PrintName { get; set; }
|
|
|
|
public int PrintCount { get => printCount; set { Set(ref printCount, value); } }
|
|
|
|
private int printCount = 1;
|
|
|
|
|
|
|
|
private void BButton_Click(object sender, RoutedEventArgs e)
|
|
|
|
{
|
|
|
|
Task.Factory.StartNew(() =>
|
|
|
|
{
|
|
|
|
if (BarCodeModel != null)
|
|
|
|
{
|
|
|
|
PrintData(PrintCount, PrintName,BarCodeModel);
|
|
|
|
//MyPrintHelper.PrintBarcode(ref args, BarCodeModel, font);
|
|
|
|
}
|
|
|
|
if (CertificateModel != null&& CertificateModel.Count()>0)
|
|
|
|
{
|
|
|
|
foreach (var cer in CertificateModel)
|
|
|
|
{
|
|
|
|
PrintData(PrintCount, PrintName,null,cer);
|
|
|
|
System.Threading.Thread.Sleep(100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var applicationPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
|
|
|
string printNames = System.IO.Path.Combine(applicationPath, "handPrintBarCerName.init");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (File.Exists(printNames))
|
|
|
|
{
|
|
|
|
File.Delete(printNames);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
File.WriteAllText(printNames, PrintName);
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void PrintData(int printCount, string printName, BarCodeModel barCode = null, CertificateModel certificateModel = null)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
|
|
|
|
PrintDocument document = new PrintDocument();
|
|
|
|
document.PrinterSettings.PrinterName = printName;//使用打印机名称,指定特定的打印机进行打印。
|
|
|
|
//设置打印页面
|
|
|
|
//document.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("Custom", 236, 157);
|
|
|
|
document.PrintPage += (s, a) =>
|
|
|
|
{
|
|
|
|
Font font = new Font("宋体", 6, System.Drawing.FontStyle.Regular);
|
|
|
|
if (barCode != null)
|
|
|
|
{
|
|
|
|
MyPrintHelper.PrintBarcode(ref a, barCode, font);
|
|
|
|
}
|
|
|
|
if (certificateModel != null)
|
|
|
|
{
|
|
|
|
MyPrintHelper.PrintCertificate(ref a, certificateModel, font);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
document.PrinterSettings.Copies = (short)printCount;//打印份数
|
|
|
|
document.Print();
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
App.Current.Dispatcher.Invoke(() =>
|
|
|
|
{
|
|
|
|
new TipsWindow($"打印失败,{ex.Message}").Show();
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Document_PrintPage(object sender, PrintPageEventArgs args)
|
|
|
|
{
|
|
|
|
//RectangleF bounds = new RectangleF(0, 0, 236, 157);
|
|
|
|
//args.Graphics.SetClip(bounds);//设置打印区域
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//设置 打印纸张类型
|
|
|
|
//var paperSizes = args.PageSettings.PrinterSettings.PaperSizes;
|
|
|
|
|
|
|
|
|
|
|
|
//foreach (System.Drawing.Printing.PaperSize item in paperSizes)
|
|
|
|
//{
|
|
|
|
// if (item.Height==157)
|
|
|
|
// {
|
|
|
|
// args.PageSettings.PaperSize=item;
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
//var sss = args.PageSettings.PaperSize;
|
|
|
|
//默认设置 打印类型
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
//打印 纵向打印 会模糊
|
|
|
|
//args.PageSettings.PrinterSettings.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("Custom", 236, 157);
|
|
|
|
//args.PageSettings.PrinterSettings.DefaultPageSettings.Landscape = true;
|
|
|
|
|
|
|
|
//Bitmap bitmap = new Bitmap(236,157);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
Font font = new Font("宋体", 6, System.Drawing.FontStyle.Regular);
|
|
|
|
if (BarCodeModel != null)
|
|
|
|
{
|
|
|
|
MyPrintHelper.PrintBarcode(ref args, BarCodeModel, font);
|
|
|
|
}
|
|
|
|
if (CertificateModel != null)
|
|
|
|
{
|
|
|
|
foreach (var cer in CertificateModel)
|
|
|
|
{
|
|
|
|
MyPrintHelper.PrintCertificate(ref args, cer, font);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class PrintData
|
|
|
|
{
|
|
|
|
public CertificateModel CertificateModel { get; set; }
|
|
|
|
|
|
|
|
public BarCodeModel BarCodeModel { get; set; }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|