《C调用把BarTender模板.doc》由会员分享,可在线阅读,更多相关《C调用把BarTender模板.doc(32页珍藏版)》请在三一办公上搜索。
1、1、应用 Seagull.BarTender.Print 命名空间,2、代码如下:using System;using System.Collections.Generic;using System.ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Te*t;/using System.Threading.Tasks;using System.Windows.Forms;using Seagull.BarTender.Print;/using Seagull.BarTender;n
2、amespace BarTenderPrintTest1 public partial class Form1 : Form public Form1() Initializeponent(); /Bar string dc = D:lipingBartenderPrintBarTenderPrintTest1BarTenderPrintTest1binDebugHW_CaiHe_and_packbo*.btw; int ff = 163704004; string aa = 00057616-; int i = 1; private void btn_print_Click(object s
3、ender, 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:lipingBartenderPrintBarTenderPrintTest1
4、BarTenderPrintTest1binDebugHW_CaiHe_and_packbo*.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_packbo*.btw); /打印 /format.Print(print me label); /改变标签打印数份连载 format.PrintSetup.NumberOfSerializedLabels =1; /设定印标签打印数量 format.PrintSetup.Id
6、enticalCopiesOfLabel = 1; Result nResult = format.Print(); /指定打印机打印,不指定则使用默认打印机 format.PrintSetup.PrinterName = Bar Code Printer T-4503E; / Display the print result. /Console.WriteLine(Print status = + nResult); MessageBo*.Show(打印提示:+nResult ); / Close the current format without saving. /SaveOptions
7、 有三个值 DoNotSaveChanges:不保存,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); /MessageBo*.Show
8、(btFormat.SubStrings0.Name); /获取标签的值 /string AddressSubstring = btFormat.SubStringsHWbarcode1.Value; /MessageBo*.Show(AddressSubstring); /修改标签的值 /btFormat.SubStringsAddress.Value = 1313 Mockingbird Lane, Anywhere, USA; /btFormat.SubStringsName.Value = John Doe; /btFormat.SubStringsQuantity.Value = 1
9、0; int dd = ff + i; i+; btFormat.SubStringsHWbarcode1.Value = aa+dd.ToString (); /改变标签打印数份连载 btFormat.PrintSetup.NumberOfSerializedLabels = 1; /设定印标签打印数量 btFormat.PrintSetup.IdenticalCopiesOfLabel = 1; Result nResult = btFormat.Print(); /指定打印机打印,不指定则使用默认打印机 btFormat.PrintSetup.PrinterName = Bar Code
10、 Printer T-4503E; MessageBo*.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 SD
11、K. All programs written with the BarTender Print SDK will rely on the Engine class to provide BarTender print functionality. The BarTender Print EngineThe BarTender process (bartend.e*e) provides standard BarTender functionality, such as opening label formats, changing label settings, and printing.
12、The BarTender 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
13、 to: Start, stop, 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 me
14、nce printing. The Engine class provides the Engine.Stop method to e*plicitly shut down the BarTender Print Engine. If the engine is not stopped, a bartend.e*e process may be left running in the background. After calling the Engine.Stop method, it is best practice to call the Engine.Dispose method. T
15、he Dispose 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 cons
16、tructor with 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
17、the BarTender process.btEngine.Stop() End UsingIn the above e*ample, 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.e*e process. Finally, Engine.Dispos
18、e is called automatically when e*ecution leaves the using statement, releasing all Engine resources. It is also possible to start the engine e*plicitly after it has been created using the default Engine constructor and the Engine.Start method. By default, Engine.Stop will close all open formats with
19、out saving, but an overloaded version is provided that allows manual control. The following e*ample shows alternative code for starting and stopping an engine and saving changes:In C*:using (Engine btEngine = newEngine() / Application specific code/ E*plicitly start the enginebtEngine.Start();/ Appl
20、ication-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 E*plicitly start the enginebtEngine.Start() Application-specific code Assuming the app
21、lication wants to save changes, it can be easily done at Stop time.btEngine.Stop(SaveOptions.SaveChanges) End UsingIn the above e*ample, a new Engine is created, but not started until later. Some application activity is assumed to e*ecute, then the Stop method is called. In this case, changes to lab
22、els done while using the engine are saved back to file. The SaveOptions enumeration specifies the operation concerning open label formats to perform during e*it of BarTender. In the above e*amples and many other e*amples in this document, the Engine.Dispose method is called implicitly by a using sta
23、tement. While it is not always appropriate to utilize using, it is a convenient way to ensure Dispose is called even if the block is e*ited during an e*ception. How To: Use Engine as a Field in a ClassThe above e*amples, and most e*amples in this document, present use of an Engine instance in a sing
24、le method. This approach is not practical for most real applications. Starting and stopping Engine objects, and by e*tension 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 applicat
25、ion 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 fiel
26、d in a class:In C*:public classEngineWrapper : IDisposable/ Engine FieldprivateEngine 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_engine field.p
27、rotectedEngine BtEngine get / If the engine has not been created yet, create and start it.if (m_engine = null) m_engine = newEngine(true); return m_engine; / Implement IDisposablepublic void Dispose() / The engine only needs to be stopped and disposed if it was/ created. Use the field here, not the
28、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 use the BtEngine pro
29、perty instead of the m_engine field. In VB:Public Class EngineWrapper Implements IDisposable Engine FieldPrivate 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) should utilize this prop
30、erty 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 IfReturn m_engine End GetEnd Property Implement IDisposablePublic Sub Dispose() Implements ID
31、isposable.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_engine.Stop() m_engine.Dis
32、pose() End IfEnd Sub Additional methods for specific work in your application. All additional methods should use the BtEngine property instead of the m_engine field.End ClassThe class above provides lazy instantiation of an Engine object and a method for disposal of its resources. By using the BtEng
33、ine 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 class were used in a real applic
34、ation, 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 multiple threads in an applicatio
35、n, 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 may be times you will want to
36、 view and interact with BarTender抯 user interface. The following e*ample shows how to view BarTender抯 users interface using the property. In C*:using (Engine btEngine = newEngine() btEngine.Start(); btEngine.Window.Visible = true;/ Application-specific codebtEngine.Stop(); In VB:Using btEngine As Ne
37、w Engine() btEngine.Start() btEngine.Window.Visible = True Application-specific codebtEngine.Stop() End UsingIn the above code, a new Engine is initialized and started. The BarTender application window is then shown by setting the Engine.Windows Visible property to true. The method assumes some inte
38、rvening 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 BarTender window will only flash open for a moment, then im
39、mediately 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 status of a print job. These same events are found in the
40、 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 information, refer to Working with Print Job Status Events.Printing
41、 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 print job or wait to return until printing is plete. The Label
42、FormatDocument 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 waitForpletionTimeout) Print(string printJobName, int waitForpletionTimeout, out Messa
43、ges messages)Using the Print MethodSeveral Print overloads e*ist; 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:MyLabel.btw);Result result = btFormat.Print(); In VB:Dim btFormat As Label
44、FormatDocument = 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 successfully spooled to the printer; a value of Failure indicates otherwise. The Pr
45、int method specifies the name of the print job, a flag indicating whether to wait for the print job to plete or not, and a collection of messages. The following code shows how to print a format that is open in the BarTender print engine. In C*:Messages messages = null;LabelFormatDocument btFormat =
46、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 result As Result = btFormat.Print(PrintJob1, messages) In the above e*ample, the app
47、lication 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 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