步步为盈
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

246 lines
7.4 KiB

2 years ago
using BBWY.Client.Helpers;
using BBWY.Client.Models;
using BBWY.Controls;
using NPOI.SS.UserModel;
2 years ago
using System;
using System.Collections.Generic;
2 years ago
using System.Drawing;
using System.Drawing.Printing;
2 years ago
using System.IO;
using System.Linq;
using System.Printing;
using System.Runtime.ConstrainedExecution;
2 years ago
using System.Text;
2 years ago
using System.Threading.Tasks;
2 years ago
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
2 years ago
using System.Windows.Shapes;
using System.Drawing.Printing;
2 years ago
using System.Collections.ObjectModel;
using BBWY.Client.Extensions;
2 years ago
namespace BBWY.Client.Views.PackTask
{
/// <summary>
/// PrintWindow.xaml 的交互逻辑
/// </summary>
public partial class PrintWindow : BWindow
{
public PrintWindow()
{
InitializeComponent();
2 years ago
this.Loaded += PrintWindow_Loaded;
2 years ago
}
2 years ago
private void PrintWindow_Loaded(object sender, RoutedEventArgs e)
{
LoadPrints();
}
/// <summary>
/// 获取打印机名称
/// </summary>
private void LoadPrints()
{
2 years ago
var printingNames = PrinterSettings.InstalledPrinters;//获取本机的打印机数据
2 years ago
int index = -1;
int selectIndex = 0;
foreach (string name in printingNames)
{
if (name == "Microsoft XPS Document Writer" || name == "Microsoft Print to PDF" || name == "Fax")
2 years ago
{
continue;
}
index++;
if (name.Contains("Deli"))
{
selectIndex = index;
}
cbPrints.Items.Add(name);
}
if (cbPrints.Items.Count > selectIndex)
2 years ago
{
cbPrints.SelectedIndex = selectIndex;
}
}
2 years ago
public ObservableCollection<GoodsNumberCer> GoodsNumberCerList { get; set; }
2 years ago
/// <summary>
/// 加载预览打印数据
/// </summary>
public void LoadData()
{
// PrintData data = new PrintData();
if (CertificateModel != null)
2 years ago
{
2 years ago
GoodsNumberCerList = new ObservableCollection<GoodsNumberCer>();
foreach (CertificateModel certificateModel in CertificateModel)
GoodsNumberCerList.Add(new GoodsNumberCer
{
CertificateModel = certificateModel.Copy(),
});
this.DataContext = this;
2 years ago
bar.Visibility = Visibility.Collapsed;
cer.Visibility = Visibility.Visible;
2 years ago
}
if (BarCodeModel != null)
2 years ago
{
2 years ago
this.DataContext = BarCodeModel;
cer.Visibility = Visibility.Collapsed;
bar.Visibility = Visibility.Visible;
2 years ago
}
}
2 years ago
public CertificateModel[] CertificateModel { get; set; }
2 years ago
public BarCodeModel BarCodeModel { get; set; }
2 years ago
private void BButton_Click(object sender, RoutedEventArgs e)
{
int printCount = 0;
2 years ago
try
{
printCount = Convert.ToInt32(tbCount.Text);
2 years ago
}
catch
{
new TipsWindow("请输入打印份数", 1).ShowDialog();
return;
}
string printName = cbPrints.Text;//选择的要打印的打印机名称
Task.Factory.StartNew(() =>
2 years ago
{
2 years ago
if (BarCodeModel != null)
{
2 years ago
PrintData(printCount, printName,BarCodeModel);
//MyPrintHelper.PrintBarcode(ref args, BarCodeModel, font);
}
2 years ago
if (CertificateModel != null&& CertificateModel.Count()>0)
{
2 years ago
foreach (var cer in CertificateModel)
{
2 years ago
PrintData(printCount, printName,null,cer);
System.Threading.Thread.Sleep(100);
// MyPrintHelper.PrintCertificate(ref args, cer, font);
}
}
2 years ago
2 years ago
});
2 years ago
}
2 years ago
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("打印失败").ShowDialog();
});
}
}
2 years ago
private void Document_PrintPage(object sender, PrintPageEventArgs args)
2 years ago
{
2 years ago
//RectangleF bounds = new RectangleF(0, 0, 236, 157);
//args.Graphics.SetClip(bounds);//设置打印区域
2 years ago
//设置 打印纸张类型
//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;
//默认设置 打印类型
2 years ago
#if DEBUG
//打印 纵向打印 会模糊
2 years ago
//args.PageSettings.PrinterSettings.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("Custom", 236, 157);
//args.PageSettings.PrinterSettings.DefaultPageSettings.Landscape = true;
2 years ago
//Bitmap bitmap = new Bitmap(236,157);
2 years ago
#endif
2 years ago
Font font = new Font("宋体", 6, System.Drawing.FontStyle.Regular);
2 years ago
if (BarCodeModel != null)
2 years ago
{
2 years ago
MyPrintHelper.PrintBarcode(ref args, BarCodeModel, font);
2 years ago
}
if (CertificateModel != null)
2 years ago
{
2 years ago
foreach (var cer in CertificateModel)
{
MyPrintHelper.PrintCertificate(ref args, cer, font);
}
2 years ago
}
2 years ago
}
}
2 years ago
public class PrintData
{
public CertificateModel CertificateModel { get; set; }
2 years ago
public BarCodeModel BarCodeModel { get; set; }
2 years ago
2 years ago
}
2 years ago
}