Model.cs
12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
using System;
using System.Collections.Generic;
using System.Drawing;
using PrintLabel;
using ZXing.PDF417.Internal;
namespace Model
{
/// <summary>
/// 打印的标签
/// </summary>
internal class ClsLabel
{
/// <summary>
/// 文件路径
/// </summary>
public string Path { set; get; }
/// <summary>
/// 标签名称
/// </summary>
public string Name { set; get; }
/// <summary>
/// 标签大小,毫米单位
/// </summary>
public SizeF Size_mm { private set; get; }
/// <summary>
/// 标签大小,像素单位
/// </summary>
public SizeF Size_px { private set; get; }
/// <summary>
/// 打印偏移量
/// </summary>
public Point Offset { get; set; }
/// <summary>
/// 包含的字段
/// </summary>
public List<ClsLabelField> Field { set; get; }
/// <summary>
/// 打印的标签
/// </summary>
public ClsLabel()
{
Field = new List<ClsLabelField>();
}
/// <summary>
/// 设置大小
/// </summary>
/// <param name="s">大小的宽度高度</param>
public void SetSize(string s)
{
string[] tt = s.Split(',');
if (tt.Length != 2) return;
bool rtn = float.TryParse(tt[0], out float width);
if (!rtn) return;
rtn = float.TryParse(tt[1], out float height);
if (!rtn) return;
Size_mm = new SizeF(width, height);
Size_px = new SizeF(Common.MMToPx(width), Common.MMToPx(height));
}
/// <summary>
/// 获取大小(毫米)
/// </summary>
/// <returns></returns>
public string GetSize()
{
return string.Format("{0:0.000},{1:0.000}", Size_mm.Width, Size_mm.Height);
}
/// <summary>
/// 设置大小
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
public void SetSize(float width, float height)
{
Size_mm = new SizeF(width, height);
Size_px = new SizeF(Common.MMToPx(width), Common.MMToPx(height));
}
/// <summary>
/// 设置打印偏移量
/// </summary>
/// <param name="s">偏移量的xy</param>
public void SetOffset(string s)
{
string[] tt = s.Split(',');
if (tt.Length != 2) return;
bool rtn = int.TryParse(tt[0], out int x);
if (!rtn) return;
rtn = int.TryParse(tt[1], out int y);
if (!rtn) return;
Offset = new Point(x, y);
}
/// <summary>
/// 获取打印偏移
/// </summary>
/// <returns></returns>
public string GetOffset()
{
return string.Format("{0},{1}", Offset.X, Offset.Y);
}
}
/// <summary>
/// 打印标签字段
/// </summary>
internal class ClsLabelField
{
/// <summary>
/// 左上角坐标,像素单位
/// </summary>
public PointF Location_px { set; get; }
/// <summary>
/// 左上角坐标,毫米单位
/// </summary>
public PointF Location_mm { set; get; }
/// <summary>
/// 大小,像素单位
/// </summary>
public SizeF Size_px { set; get; }
/// <summary>
/// 大小,毫米单位
/// </summary>
public SizeF Size_mm { set; get; }
/// <summary>
/// 字体
/// </summary>
public Font Font { set; get; }
/// <summary>
/// 打印的文本内容
/// </summary>
public string PrintText { set; get; }
/// <summary>
/// 用于格式化的文本
/// </summary>
public string FormatText { set; get; }
/// <summary>
/// 字段类型
/// </summary>
public LabelFieldType Type { set; get; }
/// <summary>
/// 条码或二维码的图片
/// </summary>
public Bitmap Image { set; get; }
/// <summary>
/// 打印标签字段
/// </summary>
public ClsLabelField()
{
Font = new Font("宋体", 9f);
Type = LabelFieldType.Text;
Image = null;
}
/// <summary>
/// 设置位置和大小(毫米)
/// </summary>
/// <param name="rect">毫米尺寸文本</param>
public void SetLocationSize(string rect)
{
string[] tt = rect.Split(',');
if (tt.Length != 4) return;
bool rtn = float.TryParse(tt[0], out float x);
if (!rtn) return;
rtn = float.TryParse(tt[1], out float y);
if (!rtn) return;
rtn = float.TryParse(tt[2], out float width);
if (!rtn) return;
rtn = float.TryParse(tt[3], out float height);
if (!rtn) return;
Location_mm = new PointF(x, y);
Location_px = new PointF(Common.MMToPx(x), Common.MMToPx(y));
Size_mm = new SizeF(width, height);
Size_px = new SizeF(Common.MMToPx(width), Common.MMToPx(height));
}
/// <summary>
/// 设置位置和大小(像素)
/// </summary>
/// <param name="rect">像素位置大小</param>
public void SetLocationSize(RectangleF rect)
{
Location_px = new PointF(rect.X, rect.Y);
Location_mm = new PointF(Common.PxToMM(rect.X), Common.PxToMM(rect.Y));
Size_px = new SizeF(rect.Width, rect.Height);
Size_mm = new SizeF(Common.PxToMM(rect.Width), Common.PxToMM(rect.Height));
}
/// <summary>
/// 设置位置和大小(毫米)
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="w"></param>
/// <param name="h"></param>
public void SetLocationSize(float x, float y, float w, float h)
{
Location_mm = new PointF(x, y);
Location_px = new PointF(Common.MMToPx(x), Common.MMToPx(y));
Size_mm = new SizeF(w, h);
Size_px = new SizeF(Common.MMToPx(w), Common.MMToPx(h));
}
/// <summary>
/// 设置字体
/// </summary>
/// <param name="s">格式:字体,字号,粗体,斜体</param>
/// <returns></returns>
public void SetFont(string s)
{
string[] t = s.Split(',');
float emSize = Convert.ToSingle(t[1]);
FontStyle style = FontStyle.Regular;
if (t[2] == "B") style |= FontStyle.Bold;
if (t[3] == "I") style |= FontStyle.Italic;
Font = new Font(t[0], emSize, style);
}
public string GetFont()
{
string[] s = new string[4];
s[0] = Font.Name;
s[1] = Font.Size.ToString();
if (Font.Bold) s[2] = "B";
if (Font.Italic) s[3] = "I";
return string.Join(",", s);
}
public RectangleF GetRectangleF()
{
return new RectangleF(Location_px, Size_px);
}
public string GetRectText()
{
return string.Format("{0},{1},{2},{3}", Location_mm.X, Location_mm.Y, Size_mm.Width, Size_mm.Height);
}
/// <summary>
/// 设置条码或二维码图片
/// </summary>
/// <param name="content">内容</param>
public void SetBarQRCode(string content)
{
try
{
Image = null;
if (Type == LabelFieldType.Text) return;
ZXing.BarcodeWriter writer = new ZXing.BarcodeWriter();
ZXing.QrCode.QrCodeEncodingOptions qrCodeEncodingOptions = new ZXing.QrCode.QrCodeEncodingOptions()
{
DisableECI = true,
CharacterSet = "UTF-8",
Width = 300,
Height = 300,
Margin = 1,
PureBarcode = true
};
ZXing.PDF417.PDF417EncodingOptions pDF417EncodingOptions = new ZXing.PDF417.PDF417EncodingOptions()
{
AspectRatio = PDF417AspectRatio.AUTO,
Compact = false,
Compaction = Compaction.AUTO,
DisableECI = true,
ErrorCorrection = PDF417ErrorCorrectionLevel.AUTO,
PureBarcode = false,
GS1Format = false,
Width = 100,
CharacterSet = "UTF-8",
//Height = 70,
Margin = 1
};
writer.Options.PureBarcode = true;
switch (Type)
{
case LabelFieldType.Code39:
writer.Format = ZXing.BarcodeFormat.CODE_39;
Image = writer.Write(content);
break;
case LabelFieldType.Code93:
writer.Format = ZXing.BarcodeFormat.CODE_93;
Image = writer.Write(content);
break;
case LabelFieldType.Code128:
writer.Format = ZXing.BarcodeFormat.CODE_128;
Image = writer.Write(content);
break;
case LabelFieldType.DataMatrix:
writer.Format = ZXing.BarcodeFormat.DATA_MATRIX;
Image = writer.Write(content);
break;
case LabelFieldType.QRCode:
writer.Options = qrCodeEncodingOptions;
writer.Format = ZXing.BarcodeFormat.QR_CODE;
Image = writer.Write(content);
break;
case LabelFieldType.PDF417:
writer.Options = pDF417EncodingOptions;
writer.Format = ZXing.BarcodeFormat.PDF_417;
Image = writer.Write(content);
break;
}
}
catch (Exception ex)
{
}
}
/// <summary>
/// 设置条码或二维码图片
/// </summary>
public void SetBarQRCode()
{
SetBarQRCode(FormatText);
}
/// <summary>
/// 坐标是否包含在范围内
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
public bool Contains(float x, float y)
{
if (x >= Location_px.X && x <= Location_px.X + Size_px.Width &&
y >= Location_px.Y && y <= Location_px.Y + Size_px.Height)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 打印标签字段类型
/// </summary>
internal enum LabelFieldType
{
/// <summary>
/// 39一维码
/// </summary>
Code39,
/// <summary>
/// 93一维码
/// </summary>
Code93,
/// <summary>
/// 128一维码
/// </summary>
Code128,
/// <summary>
/// DM二维码
/// </summary>
DataMatrix,
/// <summary>
/// QR二维码
/// </summary>
QRCode,
/// <summary>
/// 文本
/// </summary>
Text,
/// <summary>
/// PDF417
/// </summary>
PDF417
}
internal enum FieldMargin
{
/// <summary>
/// 外面
/// </summary>
Outside,
/// <summary>
/// 左边
/// </summary>
Left,
/// <summary>
/// 上边
/// </summary>
Top,
/// <summary>
/// 右边
/// </summary>
Right,
/// <summary>
/// 下边
/// </summary>
Bottom,
/// <summary>
/// 左上角
/// </summary>
LeftTop,
/// <summary>
/// 右上角
/// </summary>
RightTop,
/// <summary>
/// 左下角
/// </summary>
LeftBottom,
/// <summary>
/// 右下角
/// </summary>
RightBottom,
/// <summary>
/// 内部
/// </summary>
Inside
}
}