玩儿条形码之条码打印
条码打印机终于到货了,拿到打印机后才发现原来条码打印机是不需要自己写条码生成程序的,只要把相应的打印机语言指令(例如斑马打印机的EPL2)发送到打印机就行了,白白浪费了那么多时间去研究条码打印的代码,真是郁闷呀
。象平时写打印的程序一样,打开一打印对话框,让用户选择打印机,开始打印。咦,结果怎么跟想像中不一样,虽然我发送了EPL2的指令,但是并没有生成条形码出来,打印出来的结果还是EPL2指令呀。看来对于条码打印机,这种方式好象是行不通的,结合打印机文档的实例,只能象在DOS下面那样直接把指令发送到打印机端口才行了(详见上一篇blog)。关于怎么在windows下直接发送打印机指令到打印机,可以参考MSDN文档How to send raw data to a printer by using Visual Basic .NET。
因此,定义一个接口ICommand来取得打印指令:
定义类Label,对条形码标签进封装:
最后,定义标签,把你打印的标签指令送到打印机,标签成功打印(我采用方式是生成临时文件,再把文件发到打印机)
![](/CuteSoft_Client/CuteEditor/images/emsad.gif)
因此,定义一个接口ICommand来取得打印指令:
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![](/Images/OutliningIndicators/None.gif)
5
namespace PrinterUtils.Commands
6
{
7
public interface ICommand
8
{
9
string GetCommandString();
10
}
11
}
定义接口IComplexCommand来进行各种指令的操作:![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
11
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
1
using System;
2
using System.Collections.Generic;
3
using System.Collections.ObjectModel;
4
using System.Text;
5![](/Images/OutliningIndicators/None.gif)
6
namespace PrinterUtils.Commands
7
{
8
public interface IComplexCommand:ICommand
9
{
10
ReadOnlyCollection<ICommand> Commands{ get; }
11
void AddCommand(ICommand cmd);
12
void RemoveCommand(ICommand cmd);
13
void RemoveCommandAt(int pos);
14
}
15
}
定义类PrintTextCommand、BarCodeCommand和Print1bppImageCommand分别对应文本打印、条码打印及图形打印的指令:![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
15
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![](/Images/OutliningIndicators/None.gif)
5
namespace PrinterUtils.Commands
6
{
7
public class BarCodeCommand:BasePositioning, ICommand
8
{
9
private PrintingRotation _rotation;
10
private int _wideBarWidth;
11
private int _codeBarHeight;
12
private string _code;
13
private HumanReadableCode _printHumanCode;
14
private BarCode _barCodeType;
15
private int _narrowBarWidthInDots;
16![](/Images/OutliningIndicators/InBlock.gif)
17
public BarCodeCommand( int x, int y, PrintingRotation rotation,
18
int narrowBarWidthInDots, int wideBarWidth,
19
int codeBarHeight, HumanReadableCode printHumanCode, BarCode barCodeType, string code)
20
:base(x,y)
21
{
22
_rotation = rotation;
23
_wideBarWidth = wideBarWidth;
24
_codeBarHeight = codeBarHeight;
25
_code = code;
26
_printHumanCode = printHumanCode;
27
_barCodeType = barCodeType;
28
_narrowBarWidthInDots = narrowBarWidthInDots;
29
}
30
//preciso adaptar ao codigo usado por predefinicao
31
public BarCodeCommand( int x, int y, string code )
32
:this(x, y, PrintingRotation.NoRotation, 3, 3, 10, HumanReadableCode.Yes,
33
BarCode.Codabar, code)
34
{}
35![](/Images/OutliningIndicators/InBlock.gif)
36
public static BarCodeCommand CreateEAN13BarCodeCommand(int x, int y, string code)
37
{
38
return new BarCodeCommand(x, y, PrintingRotation.NoRotation, 2, //narrowBand
39
2, //widebarwidth
40
50,
41
HumanReadableCode.Yes,
42
BarCode.EAN13,
43
code);
44
45
}
46![](/Images/OutliningIndicators/InBlock.gif)
47
public static BarCodeCommand CreateEAN128BarCodeCommand(int x, int y, string code)
48
{
49
//same as UCC
50
return new BarCodeCommand(x, y, PrintingRotation.NoRotation,
51
2, //narrowBand
52
2, //widebarwidth
53
50,
54
HumanReadableCode.Yes,
55
BarCode.UCCEAN128, code);
56![](/Images/OutliningIndicators/InBlock.gif)
57
}
58![](/Images/OutliningIndicators/InBlock.gif)
59
public static BarCodeCommand CreateCode39BarCodeCommand(int x, int y, string code)
60
{
61
return new BarCodeCommand(x, y, PrintingRotation.NoRotation,
62
1,
63
3,
64
50,
65
HumanReadableCode.Yes,
66
BarCode.Code39Std, code);
67
}
68![](/Images/OutliningIndicators/InBlock.gif)
69
ICommand Members
86
}
87
}
88![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/InBlock.gif)
45
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
46
![](/Images/OutliningIndicators/InBlock.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/InBlock.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/InBlock.gif)
54
![](/Images/OutliningIndicators/InBlock.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/InBlock.gif)
57
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
58
![](/Images/OutliningIndicators/InBlock.gif)
59
![](/Images/OutliningIndicators/InBlock.gif)
60
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
61
![](/Images/OutliningIndicators/InBlock.gif)
62
![](/Images/OutliningIndicators/InBlock.gif)
63
![](/Images/OutliningIndicators/InBlock.gif)
64
![](/Images/OutliningIndicators/InBlock.gif)
65
![](/Images/OutliningIndicators/InBlock.gif)
66
![](/Images/OutliningIndicators/InBlock.gif)
67
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
68
![](/Images/OutliningIndicators/InBlock.gif)
69
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
86
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
87
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
88
![](/Images/OutliningIndicators/None.gif)
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![](/Images/OutliningIndicators/None.gif)
5
namespace PrinterUtils.Commands
6
{
7
public class PrintTextCommand:BasePositioning, ICommand
8
{
9
10
private string _txt;
11
private int _fontSelection;
12
private PrintingRotation _rotation;
13
private PrintingMultiplier _horizontalMultiplier;
14
private PrintingMultiplier _verticalMultiplier;
15
private PrintingReverse _printingReverse;
16
17
public PrintTextCommand( int x, int y, string txt, PrintingRotation rotation, int fontSelection,
18
PrintingMultiplier horizontalMultiplier, PrintingMultiplier verticalMultiplier,
19
PrintingReverse printingReverse)
20
:base(x, y)
21
{
22
_txt = txt;
23
_fontSelection = fontSelection;
24
_rotation = rotation;
25
_horizontalMultiplier = horizontalMultiplier;
26
_verticalMultiplier = verticalMultiplier;
27
_printingReverse = printingReverse;
28
}
29![](/Images/OutliningIndicators/InBlock.gif)
30
public PrintTextCommand( int x, int y, string txt, int size)
31
:this(x, y, txt, PrintingRotation.NoRotation, size, PrintingMultiplier.One,
32
PrintingMultiplier.One, PrintingReverse.N)
33
{}
34![](/Images/OutliningIndicators/InBlock.gif)
35
public PrintTextCommand( int x, int y, string txt)
36
:this(x, y, txt, 2)
37
{
38
39
}
40![](/Images/OutliningIndicators/InBlock.gif)
41
ICommand Members
57
}
58
}
59![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
57
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
58
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
59
![](/Images/OutliningIndicators/None.gif)
定义类Label,对条形码标签进封装:
1
using System;
2
using System.Collections.Generic;
3
using System.Collections.ObjectModel;
4
using System.Drawing;
5
using System.Text;
6![](/Images/OutliningIndicators/None.gif)
7
namespace PrinterUtils.Commands
8
{
9
public class Label: IComplexCommand
10
{
11
private Size _size;
12
private int _gapLength;
13
private Point _referencePoint;
14
private int _numberCopies;
15![](/Images/OutliningIndicators/InBlock.gif)
16
private IList<ICommand> _cmds;
17![](/Images/OutliningIndicators/InBlock.gif)
18
public Label(int width, int height)
19
:this( new Size(width, height))
20
{
21
}
22![](/Images/OutliningIndicators/InBlock.gif)
23
public Label( Size size)
24
:this( size, 19, Point.Empty, 1)
25
{
26
}
27![](/Images/OutliningIndicators/InBlock.gif)
28
public Label( Size size, int gapLength, Point referencePoint, int numberCopies)
29
{
30
_size = size;
31
_gapLength = gapLength;
32
_referencePoint = referencePoint;
33
_cmds = new List<ICommand>();
34
_numberCopies = numberCopies;
35
}
36![](/Images/OutliningIndicators/InBlock.gif)
37![](/Images/OutliningIndicators/InBlock.gif)
38
public Size Size
39
{
40
get { return _size; }
41
set { _size = value; }
42
}
43![](/Images/OutliningIndicators/InBlock.gif)
44
public int GapLength
45
{
46
get { return _gapLength; }
47
set { _gapLength = value; }
48
}
49![](/Images/OutliningIndicators/InBlock.gif)
50
ICommand Members
73![](/Images/OutliningIndicators/InBlock.gif)
74
IComplexCommand Members
97
}
98
}
99![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
21
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
26
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
40
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
41
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
42
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/InBlock.gif)
45
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
46
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
47
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
48
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
73
![](/Images/OutliningIndicators/InBlock.gif)
74
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
97
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
98
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
99
![](/Images/OutliningIndicators/None.gif)
最后,定义标签,把你打印的标签指令送到打印机,标签成功打印(我采用方式是生成临时文件,再把文件发到打印机)
1
private void buttonPrint_Click(object sender, EventArgs e)
2
{
3
if (dataGridView1.Rows.Count < 1)
4
MessageBox.Show("没有条码可以打印!请选择条码后再按此按钮。", "条码打印", MessageBoxButtons.OK, MessageBoxIcon.Warning);
5
//if (printDialog1.ShowDialog() == DialogResult.OK)
6
//{
7
using (IZebraPrinter printer = new LPT1Printer(ConfigurationManager.AppSettings["PrinterName"]))
8
{
9
List<ShimaoLabel> labels = GetPrintLabels();
10
printer.PreparePrinter();
11
foreach (ShimaoLabel sLabel in labels)
12
{
13
string printString = ((ICommand)sLabel).GetCommandString();
14
File.WriteAllText(System.Guid.NewGuid().ToString() + ".prn", printString, Encoding.GetEncoding(0));
15
printer.WriteCommandToPrinter(sLabel);
16
}
17
}
18
//}
19
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
17
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)