巨石化纤
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.

167 lines
8.6 KiB

using System.Diagnostics;
using System.Drawing;
using System.Drawing.Printing;
using ZXing;
using ZXing.Common;
namespace SSWMS.Print
{
public class Program
{
//private static void Print_2210324(string sPrinterName, string sContent)
//{
// try
// {
// string[] asContent = sContent.Split(',');
// if (asContent.Length != 18)
// {
// return;
// }
// const float fPageScale = 1.7f;
// const float top = 7f;
// const float left = 0f;
// const float leftTitle = 3f;
// const float leftContent = 16f;
// const float lineHeight = 4f;
// 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 fontTitle = new Font("Arial", 6.5f * fPageScale, FontStyle.Regular);
// Font font = new Font("Arial", 6 * fPageScale, FontStyle.Regular);
// Brush b = new SolidBrush(Color.Black);
// Graphics g = e.Graphics;
// g.PageScale = fPageScale;
// g.PageUnit = GraphicsUnit.Millimeter;
// g.DrawRectangle(new Pen(Brushes.Black, 0.2f), left + 2f, top - 5f, 50f, 64f);
// // 新二维码
// g.DrawString(asContent[0], fontTitle, b, new PointF(left + leftTitle, top - 2f));
// g.DrawString("产品名称:", font, b, new PointF(left + leftTitle, top + lineHeight));
// g.DrawString("数 量:", font, b, new PointF(left + leftTitle, top + lineHeight * 3));
// g.DrawString("入库编号:", font, b, new PointF(left + leftTitle, top + lineHeight * 4));
// g.DrawString("生产日期:", font, b, new PointF(left + leftTitle, top + lineHeight * 5));
// g.DrawString("批 号:", font, b, new PointF(left + leftTitle, top + lineHeight * 6));
// g.DrawString("登 记 号:", font, b, new PointF(left + leftTitle, top + lineHeight * 7));
// g.DrawString("产品标准号:", font, b, new PointF(left + leftTitle, top + lineHeight * 8));
// g.DrawString("地 址:", font, b, new PointF(left + leftTitle, top + lineHeight * 9));
// g.DrawString("电 话:", font, b, new PointF(left + leftTitle, top + lineHeight * 10));
// g.DrawString(asContent[2], font, b, new PointF(left + leftContent, top + lineHeight));
// g.DrawString(asContent[4], font, b, new PointF(left + leftContent, top + lineHeight * 3));
// g.DrawString(asContent[5], font, b, new PointF(left + leftContent, top + lineHeight * 4));
// g.DrawString(asContent[7], font, b, new PointF(left + leftContent, top + lineHeight * 7));
// g.DrawString(asContent[10], font, b, new PointF(left + leftContent, top + lineHeight * 8));
// g.DrawString(asContent[11], font, b, new PointF(left + leftContent, top + lineHeight * 9));
// g.DrawString(asContent[12], font, b, new PointF(left + leftContent, top + lineHeight * 10));
// // 新二维码
// g.DrawString("产品规格:", font, b, new PointF(left + leftTitle, top + lineHeight * 2));
// g.DrawString(asContent[3], font, b, new PointF(left + leftContent, top + lineHeight * 2));
// g.DrawImage(new BarcodeWriter
// {
// Options = new EncodingOptions { Width = 1000, Height = 1000, Margin = 0, PureBarcode = true },
// Format = BarcodeFormat.QR_CODE,
// }.Write($"12345qwcewfwgverefegvwefqegv67890"),
// new RectangleF(left + leftContent + 19f, top + lineHeight * 3 - 1f, 14f, 14f));
// e.HasMorePages = false;
// };
// printDocument.Print();
// }
// catch
// {
// }
//}
private static void PrintTest()
{
try
{
const float fPageScale = 1.7f;
const float top = 7f;
const float left = 0f;
const float leftTitle = 3f;
const float leftContent = 16f;
const float lineHeight = 4f;
PrintDocument printDocument = new PrintDocument();
printDocument.PrintController = new StandardPrintController();
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
printDocument.DefaultPageSettings.PaperSize = new PaperSize("Custom", 100, 100);
int iPage = 0;
printDocument.PrintPage += (sender, e) =>
{
Font fontTitle = new Font("Arial", 6.5f * fPageScale, FontStyle.Regular);
Font font = new Font("Arial", 6 * fPageScale, FontStyle.Regular);
Brush b = new SolidBrush(Color.Black);
Graphics g = e.Graphics;
g.PageScale = fPageScale;
g.PageUnit = GraphicsUnit.Millimeter;
g.DrawRectangle(new Pen(Brushes.Black, 0.2f), left + 2f, top - 5f, 50f, 64f);
g.DrawString("沈阳新松机器人自动化股份有限公司", fontTitle, b, new PointF(left + leftTitle, top - 2f));
g.DrawString("产品名称:", font, b, new PointF(left + leftTitle, top + lineHeight * 1));
g.DrawString("测试产品名称", font, b, new PointF(left + leftContent, top + lineHeight * 1));
g.DrawString("数 量:", font, b, new PointF(left + leftTitle, top + lineHeight * 2));
g.DrawString("测试数量", font, b, new PointF(left + leftContent, top + lineHeight * 2));
g.DrawImage(Image.FromFile("logo.png"), new RectangleF(left + leftContent + 21f, top + lineHeight * 4, 10f, 10f), new RectangleF(0, 0, 24f, 24f), GraphicsUnit.Millimeter);
g.DrawImage(new BarcodeWriter
{
Options = new EncodingOptions { Width = 1000, Height = 1000, Margin = 0, PureBarcode = true },
Format = BarcodeFormat.QR_CODE,
}.Write($"12345qwcewfwgverefegvwefqegv67890"),
new RectangleF(left + leftTitle + 19f, top + lineHeight * 4, 14f, 14f));
++iPage;
if (iPage < 1)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
};
printDocument.Print();
//PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
//printPreviewDialog.Document = printDocument;
//printPreviewDialog.ShowDialog();
}
catch
{
}
}
public static void Print(string sPrinterName, string[] asContent)
{
//Print.Program.Print_2210324(sPrinterName, asContent);
if (string.IsNullOrWhiteSpace(sPrinterName))
{
sPrinterName = "Microsoft Print to PDF"; // Test
// sPrinter = "ZDesigner ZT410-203dpi ZPL"; // 2210324 山东药玻
}
string sContent = string.Join("\" \"", asContent); // 2210324 山东药玻
Process process = new Process();
process.StartInfo.FileName = "SSWMS.Print.exe";
process.StartInfo.Arguments = $"\"{sPrinterName}\" \"{sContent}\"";
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
if (!process.WaitForExit(10000))
{
process.Kill();
}
}
public static void Main(string[] args)
{
if (args.Length == 2)
{
PrintTest();
//Print_2210324(args[0], args[1]);
}
else
{
PrintTest();
}
}
}
}