C#调用把BarTender模板 下载本文

内容发布更新时间 : 2024/6/16 1:20:28星期一 下面是文章的全部内容请认真阅读。

End Class

The class above provides lazy instantiation of an Engine object and a method for disposal of its 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 class 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 multiple 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 Interface

By default, an Engine object's BarTender process runs BarTender in the background without being seen by a user. However, there 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 (EnginebtEngine = newEngine()) {

btEngine.Start();

btEngine.Window.Visible = true; // Application-specific code btEngine.Stop(); } In VB:

UsingbtEngineAs NewEngine() 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.Window'sVisible 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 BarTender 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 Events

The 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 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 Label Formats

A label format can be printed by calling the LabelFormatDocument'sPrint method. The Print method prints a job to a printer's spooler and returns a Result enumeration value. It can either return immediately after spooling the print 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)

?

Print(string printJobName, int waitForCompletionTimeout, out Messages messages)

Using the Print Method

Several Print overloads exist; the simplest takes no parameters. The following code shows how to open and print a label format. In C#:

LabelFormatDocumentbtFormat =

btEngine.Documents.Open(@\); Resultresult = btFormat.Print(); In VB:

DimbtFormatAsLabelFormatDocument =

btEngine.Documents.Open(\) 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 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 print engine. In C#:

Messagesmessages = null;

LabelFormatDocumentbtFormat =

btEngine.Documents.Open(@\);

Resultresult = btFormat.Print(\, out messages); In VB:

Dim messages As Messages = Nothing DimbtFormatAsLabelFormatDocument =

btEngine.Documents.Open(\)

Dim result As Result = btFormat.Print(\, 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 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 immediately 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 indicating any errors BarTender encountered.

The Messages Collection

While printing, one or more messages may be generated indicating print success or error. These messages can be viewed by enumerating the Messages collection returned as a parameter from the Print method. Each Message object in the collection contains a Text property giving a description of the message. The message severity and type can be examined by using the Severity and Type properties, respectively.

Printing Multiple Formats

The Engine class contains a collection of LabelFormatDocument objects that are opened within the BarTender print engine. By looping through this collection, it is possible to print many format files at once. The following code demonstrates a method for printing multiple formats. In C#: int i = 0;

foreach (LabelFormatDocument format inbtEngine.Documents) {

i++;

format.Print(\ + i); } In VB:

Dim i As Integer = 0

For Each format AsLabelFormatDocumentInbtEngine.Documents i += 1

format.Print(\& i) Next format

Every format that is opened in BarTender will be printed and given a print job name corresponding to the order in which it was printed.

Changing the Number of Copies to Print

Printing identical copies of a label or printing serialized copies of a label is common when printing barcode labels. To change the number of identical label copies to print, change the value of Format's

PrintSetup.IdenticalCopiesOfLabel property. To change the number of serialized label copies to print, change the value of Format's PrintSetup.NumberOfSerializedLabels property.

The following code shows how to use IdenticalCopiesOfLabel and NumberOfSerializedLabels. In C#:

// Initialize and start a new engine using (EnginebtEngine = newEngine()) {

btEngine.Start(); // Open a label format.

LabelFormatDocumentbtFormat =

btEngine.Documents.Open(@\);