1、1、应用“ Seagull.BarTender.Print “命名空间,2、代码如下:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;/using System.Threading.Tasks;using System.Windows.Forms;using Seagull.BarTender.Print;/using Seagull.BarTen
2、der;namespace BarTenderPrintTest1public partial class Form1 : Formpublic Form1()InitializeComponent();/Barstring dc = “D:lipingBartenderPrintBarTenderPrintTest1BarTenderPrintTest1binDebugHW_CaiHe_and_packbox.btw“;int ff = 163704004;string aa = “00057616-“;int i = 1;private void btn_print_Click(objec
3、t sender, EventArgs e)PrintLabel3();#region 第一种写法private void PrintLabel1()/创建一个 BarTender 打印引擎 ,并启用 Engine engine = new Engine(true);/创建一个模板对象/LabelFormatDocument format = engine.Documents.Open(“c:test.btw“);LabelFormatDocument format = engine.Documents.Open(“D:lipingBartenderPrintBarTenderPrintTes
4、t1BarTenderPrintTest1binDebugHW_CaiHe_and_packbox.btw“);format.Print(“print me label“);/format.Print(“Select printer“, out messages);/format.Close(SaveOptions.SaveChanges );format.Print();engine.Dispose();#endregion#region 打印第二种方法private void PrintLable2()using (Engine engine = new Engine()/启用一个打印引擎
5、engine.Start();/创建一个模板对象 LabelFormatDocument format = engine.Documents.Open(“D:lipingBartenderPrintBarTenderPrintTest1BarTenderPrintTest1binDebugHW_CaiHe_and_packbox.btw“);/打印/format.Print(“print me label“);/改变标签打印数份连载format.PrintSetup.NumberOfSerializedLabels =1;/设定印标签打印数量format.PrintSetup.Identica
6、lCopiesOfLabel = 1;Result nResult = format.Print();/指定打印机打印,不指定则使用默认打印机format.PrintSetup.PrinterName = “Bar Code Printer T-4503E“;/ Display the print result./Console.WriteLine(“Print status = “ + nResult);MessageBox.Show(“打印提示:“+nResult );/ Close the current format without saving./SaveOptions 有三个值 D
7、oNotSaveChanges:不保存 ,PromptSave:提示是否保存 SaveChanges:保存format.Close(SaveOptions.DoNotSaveChanges); /结束打印引擎engine.Stop();#endregion#region 第三种打印方法改变标签的值private void PrintLabel3()using (Engine engine = new Engine(true)LabelFormatDocument btFormat = engine.Documents.Open(dc);/MessageBox.Show(btFormat.Sub
8、Strings0.Name);/获取标签的值 /string AddressSubstring = btFormat.SubStrings“HWbarcode1“.Value;/MessageBox.Show(AddressSubstring);/修改标签的值/btFormat.SubStrings“Address“.Value = “1313 Mockingbird Lane, Anywhere, USA“;/btFormat.SubStrings“Name“.Value = “John Doe“;/btFormat.SubStrings“Quantity“.Value = “10“; in
9、t dd = ff + i;i+;btFormat.SubStrings“HWbarcode1“.Value = aa+dd.ToString ();/改变标签打印数份连载btFormat.PrintSetup.NumberOfSerializedLabels = 1;/设定印标签打印数量btFormat.PrintSetup.IdenticalCopiesOfLabel = 1;Result nResult = btFormat.Print();/指定打印机打印,不指定则使用默认打印机btFormat.PrintSetup.PrinterName = “Bar Code Printer T-
10、4503E“;MessageBox.Show(“打印提示:“ + nResult);btFormat.Close(SaveOptions.DoNotSaveChanges);/结束打印引擎engine.Stop();#endregion三、bartender 说明文档部分Creating a BarTender Print EngineThe Engine class represents a BarTender process and provides the backbone for programming with the BarTender Print SDK. All program
11、s written with the BarTender Print SDK will rely on the Engine class to provide BarTender print functionality. The BarTender Print EngineThe BarTender process (bartend.exe) provides standard BarTender functionality, such as opening label formats, changing label settings, and printing. The BarTender
12、background process is controlled using an instance of the Engine class. The Role of the Engine ClassThe Engine class contains many methods, properties, and events to assist in printing and controlling the BarTender Print Engine. Features of the Engine ClassThe Engine class allows users to: Start, st
13、op, and restart a BarTender background process. Open, access, and save label formats. Use Engine-level events to monitor printing. Manage the BarTender Application window.How To: Start and Stop an EngineAn engine must be created and started in order to launch a BarTender process and commence printin
14、g. The Engine class provides the Engine.Stop method to explicitly shut down the BarTender Print Engine. If the engine is not stopped, a bartend.exe process may be left running in the background. After calling the Engine.Stop method, it is best practice to call the Engine.Dispose method. The Dispose
15、method ensures all non-memory resources are properly released for the class; this includes shutting down the BarTender process if Engine.Stop was not successfully called. The following is the minimal code necessary to create, start, stop, and dispose an Engine object: In C#:/ Calling constructor wit
16、h true automatically starts engine. using (Engine btEngine = new Engine(true) / Do something with the engine. / Stop the BarTender process. btEngine.Stop(); In VB: Calling constructor with true automatically starts engine. Using btEngine As New Engine(True) Do something with the engine. Stop the Bar
17、Tender process. btEngine.Stop() End Using In the above example, an engine is created and started implicitly by passing true as an argument to the constructor. The engine is then stopped by calling the Engine.Stop method. This terminates the background bartend.exe process. Finally, Engine.Dispose is
18、called automatically when execution leaves the using statement, releasing all Engine resources. It is also possible to start the engine explicitly after it has been created using the default Engine constructor and the Engine.Start method. By default, Engine.Stop will close all open formats without s
19、aving, but an overloaded version is provided that allows manual control. The following example shows alternative code for starting and stopping an engine and saving changes:In C#:using (Engine btEngine = new Engine() / Application specific code / Explicitly start the engine btEngine.Start();/ Applic
20、ation-specific code / Assuming the application wants to save changes, /it can be easily done at Stop time. btEngine.Stop(SaveOptions.SaveChanges); In VB:Using btEngine As New Engine(True) Application specific code Explicitly start the engine btEngine.Start() Application-specific code Assuming the ap
21、plication wants to save changes, it can be easily done at Stop time. btEngine.Stop(SaveOptions.SaveChanges) End Using In the above example, a new Engine is created, but not started until later. Some application activity is assumed to execute, then the Stop method is called. In this case, changes to
22、labels done while using the engine are saved back to file. The SaveOptions enumeration specifies the operation concerning open label formats to perform during exit of BarTender. In the above examples and many other examples in this document, the Engine.Dispose method is called implicitly by a using
23、statement. While it is not always appropriate to utilize using, it is a convenient way to ensure Dispose is called even if the block is exited during an exception. How To: Use Engine as a Field in a ClassThe above examples, and most examples in this document, present use of an Engine instance in a s
24、ingle method. This approach is not practical for most real applications. Starting and stopping Engine objects, and by extension BarTender processes, should be done as rarely as possible for optimal performance. Ideally, Engine instances should be started once and only stopped at the end of the appli
25、cation to minimize the overhead of managing processes. The most straightforward object-oriented approach is make an Engine object a field in a class and allow the encapsulating class to determine the Engine objects lifetime. The following is the minimal suggested code for making an Engine object a f
26、ield in a class:In C#:public class EngineWrapper : IDisposable / Engine Field private Engine m_engine = null;/ This property will create and start the engine the first time it is / called. Most methods in this class (and methods in child classes) / should utilize this property instead of the m_engin
27、e field. protected Engine BtEngine get / If the engine has not been created yet, create and start it. if (m_engine = null) m_engine = new Engine(true); return m_engine; / Implement IDisposable public void Dispose() / The engine only needs to be stopped and disposed if it was / created. Use the field
28、 here, not the property. Otherwise, / you might create a new instance in the Dispose method! if (m_engine != null) / Stop the process and release Engine field resources. m_engine.Stop(); m_engine.Dispose(); / Additional methods for specific work in your application. All additional / methods should u
29、se the BtEngine property instead of the m_engine field. In VB:Public Class EngineWrapper Implements IDisposable Engine Field Private m_engine As Engine = Nothing This property will create and start the engine the first time it is called. Most methods in this class (and methods in child classes) shou
30、ld utilize this property instead of the m_engine field. Protected ReadOnly Property BtEngine() As Engine Get If the engine has not been created yet, create and start it. If m_engine Is Nothing Thenm_engine = New Engine(True) End If Return m_engine End Get End Property Implement IDisposable Public Su
31、b Dispose() Implements IDisposable.Dispose The engine only needs to be stopped and disposed if it was created. Use the field here, not the property. Otherwise, you might create a new instance in the Dispose method! If m_engine IsNot Nothing Then Stop the process and release Engine field resources. m
32、_engine.Stop() m_engine.Dispose() End If End Sub Additional methods for specific work in your application. All additional methods should use the BtEngine property instead of the m_engine field. End Class The class above provides lazy instantiation of an Engine object and a method for disposal of its
33、 resources. By using the BtEngine property for all work in its methods, this class will avoid creating and starting a BarTender process until it really needs one. This class offers a means of releasing its resources, its underlying Engine object, by implementing the IDisposable interface. If this cl
34、ass were used in a real application, it would include other methods that did work specific to the application. This code would be a reasonable base class for a hierarchy of classes that perform printing in a real application. In the case where instances of this class are intended to be used from mul
35、tiple threads in an application, locking logic should be added to the BtEngine property to ensure the engine is only created once. How To: Display the BarTender User InterfaceBy default, an Engine objects BarTender process runs BarTender in the background without being seen by a user. However, there
36、 may be times you will want to view and interact with BarTender 抯 user interface. The following example shows how to view BarTender 抯 users interface using the Engine.Window.Visible property. In C#:using (Engine btEngine = new Engine() btEngine.Start(); btEngine.Window.Visible = true;/ Application-s
37、pecific code btEngine.Stop(); In VB:Using btEngine As New Engine() btEngine.Start() btEngine.Window.Visible = True Application-specific code btEngine.Stop() End Using In the above code, a new Engine is initialized and started. The BarTender application window is then shown by setting the Engine.Wind
38、ows Visible property to true. The method assumes some intervening work is done. Finally, the engine is stopped and automatically disposed when leaving the using statement. If this code is run without any intervening work between the call to btEngine.Window.Visible and the btEngine.Stop method, the B
39、arTender window will only flash open for a moment, then immediately close when the engine is stopped and the BarTender process is shutdown. The Engine Class and Print Job EventsThe Engine class provides many engine-wide events. Most of these, such as the JobQueued or JobSent, are used to monitor the
40、 status of a print job. These same events are found in the LabelFormatDocument class, where they are specific to that label format. Unlike the events found in LabelFormatDocument, Engine events provide a means to oversee print job events for all label formats opened by the engine. For more informati
41、on, refer to Working with Print Job Status Events.Printing Label FormatsA label format can be printed by calling the LabelFormatDocuments Print method. The Print method prints a job to a printers spooler and returns a Result enumeration value. It can either return immediately after spooling the prin
42、t job or wait to return until printing is complete. The LabelFormatDocument object contains several overloads for the Print method to assist in label printing. Print() Print(string printJobName) Print(string printJobName, out Messages message) Print(string printJobName, int waitForCompletionTimeout)
43、 Print(string printJobName, int waitForCompletionTimeout, out Messages messages) Using the Print MethodSeveral Print overloads exist; the simplest takes no parameters. The following code shows how to open and print a label format. In C#:LabelFormatDocument btFormat = btEngine.Documents.Open(“c:MyLab
44、el.btw“);Result result = btFormat.Print(); In VB:Dim btFormat As LabelFormatDocument = btEngine.Documents.Open(“c:MyLabel.btw“)Dim result As Result = btFormat.Print() When this method is called, a Result enumeration is immediately returned. A value of Success indicates that the print job successfull
45、y spooled to the printer; a value of Failure indicates otherwise. The Print method specifies the name of the print job, a flag indicating whether to wait for the print job to complete or not, and a collection of messages. The following code shows how to print a format that is open in the BarTender p
46、rint engine. In C#:Messages messages = null;LabelFormatDocument btFormat = btEngine.Documents.Open(“c:MyLabel.btw“);Result result = btFormat.Print(“PrintJob1“, out messages); In VB:Dim messages As Messages = NothingDim btFormat As LabelFormatDocument = btEngine.Documents.Open(“c:MyLabel.btw“)Dim res
47、ult As Result = btFormat.Print(“PrintJob1“, messages) In the above example, the application will immediately resume after the Print method call. In instances where many print jobs are being spooled, an errant print job might delay further printing. In this case it is appropriate to specify a timeout
48、 length before the program resumes. If the second parameter is passed as true, then the third parameter indicates the timeout length. Since the second parameter is passed as false, the timeout parameter should always be set to zero. If the print job is successfully spooled, the method will immediate
49、ly return with a Success Result value. The Result variable stores the results of the print job, indicating whether the print job has succeeded or failed. Result can indicate the print job has succeeded, timed out, or failed for a variety of reasons. If the result indicates the print job was not successful, the messages collection contains messages indicatin