Printing Documents to a File

By using print-to-file functionality, you can create printer code using BarTender, save the code to a file, and later send that file to a printer through a server or client application. To print to file, you use the BarTender ActiveX Automation Interface and the BarTender Print Client DLL. You must have the BarTender Enterprise Automation edition activated in order to print to file.

ClosedTo print to a file

  1. Obtain a Print to File license key using the BarTender Print Client library.

  2. Set up BarTender to print to file.

  3. Print the document.

You can then send the resulting printer code file contents to the printer for a printed copy.

The BarTender Print Client Library

The BarTender Print Client library is a signed ActiveX COM control that is easily integrated into client applications. It creates print to file license keys, enumerates the names of installed printers and models, and sends print code to printers. The Print Client library can be used for either internet or standard windows printing solutions as long as the DLL resides on the computer from which printing occurs.

Your Visual Studio project must reference the BarTender Print Client library in order to use it.

ClosedTo reference the Print Client library

  1. Open the Add Reference dialog and select the COM tab.

  2. Select BarTender Print Client Library and click OK.

Obtaining a Print to File License Key

To print to file with BarTender, you must have a license key generated on the computer from which you wish to print. BarTender is not required to be installed to generate a print to file license key, but the BarTender Print Client DLL must be installed and registered on the computer from which you desire to print.

The following code shows how to generate a print to file license key.

ClosedVB.NET

'Instantiate a new Printer instance

Dim printClient As New BarTenderPrintClient.Printer()

'Create a new print to file license key

Dim printLicense As String = Nothing

printClient.CreatePrintToFileLicense("HP LaserJet 1200", printLicense)

ClosedC#

// Instantiate a new Printer instance

BarTenderPrintClient.Printer printClient = new BarTenderPrintClient.Printer();

// Create a new print to file license key

string printLicense = null;

printClient.CreatePrintToFileLicense("HP LaserJet 1200", out printLicense);

The above example instantiates a new BarTenderPrintClient.Printer object and creates a new print to file license key using the CreatePrintToFileLicense method, storing the print license key in the printLicense string.

The printer name’s driver must be installed on the computer or the CreatePrintToFileLicense method will fail and return false. If CreatePrintToFileLicense fails, printLicense will contain details about the error.

Printing to File

In order to print your document to a file, you will need to open the BarTender document, set the necessary "Print to File" properties using the Format or PrintSetup objects, then execute the print job.

ClosedVB.NET

'Declare a BarTender application variable

Dim btApp As BarTender.Application

'Declare a BarTender document variable

Dim btFormat As BarTender.Format

'Create a new instance of BarTender variable

btApp = New BarTender.Application

'Set the BarTender application visible

btApp.Visible = True

'Open a BarTender document

btFormat = btApp.Formats.Open("c:\Format1.btw", False, "")

'Set the print to file option

btFormat.PrintToFile = True

'Set the print license

btFormat.PrintToFileLicense = printLicense

'Set the location of the print file output

btFormat.PrinterFile = "c:\out.prn"

'Print the document

btFormat.PrintOut(False, False)

'End the BarTender process

btApp.Quit(BarTender.BtSaveOptions.btDoNotSaveChanges)

ClosedC#

// Declare a BarTender application variable

BarTender.Application btApp;

// Declare a BarTender document variable

BarTender.Format btFormat;

// Create a new instance of BarTender

btApp = new BarTender.Application();

// Set the BarTender application visible

btApp.Visible = true;

// Open a BarTender document

btFormat = btApp.Formats.Open("c:\\Format1.btw", false, "");

// Set the print to file option

btFormat.PrintToFile = true;

// Set the print license

btFormat.PrintToFileLicense = printLicense;

// Set the location of the print file output

btFormat.PrinterFile = "c:\\out.prn";

// Print the document

btFormat.PrintOut(false, false);

// End the BarTender process

btApp.Quit(BarTender.BtSaveOptions.btDoNotSaveChanges);

Printing the Licensed Print File

The print file generated by BarTender is encrypted. It can only be printed from the computer on which the print license key was generated; any attempt to send the print file to a printer without explicitly using the BarTender Print Client library will fail.

To print the generated file, use the BarTenderPrintClient.Printer.SendPrintCode method.

ClosedVB.NET

'Read print file

Dim sr As System.IO.StreamReader = New System.IO.StreamReader("c:\Print\PrintFile.prn", System.Text.Encoding.Default)

Dim printerFileData As String = sr.ReadToEnd()

sr.Close()

'Instantiate a new Printer instance

Dim prtClient As BarTenderPrintClient.Printer()

'Send the data to My Printer with a job name

'of PrintJob_1234

prtClient.SendPrintCode("My Printer", "PrintJob_1234", printerFileData)

ClosedC#

// Read print file

System.IO.StreamReader sr = new System.IO.StreamReader(@"c:\Print\PrintFile.prn", System.Text.Encoding.Default);

String printerFileData = sr.ReadToEnd();

sr.Close();

// Instantiate a new Printer instance

BarTenderPrintClient.Printer prtClient = new BarTenderPrintClient.Printer();

// Send the data to My Printer with a job name

// of PrintJob_1234

prtClient.SendPrintCode("My Printer", "PrintJob_1234", printerFileData);