Take the following steps to create and run a program that print using LEADTOOLS Network printer on the server machine.
Make sure you have IIS installed on the machine correctly. And make sure the LEADTOOLS virtual printer web service is configured correctly.
Start Visual Studio.
Choose File-> New-> Project... from the menu.
In the New Project dialog box, choose either "Visual C# Projects" or "VB Projects" in the Projects Type List, and choose "Windows Application " in the Templates List.
Type the project name as "PRINTER DRIVER SERVER" in the Project Name field, and then choose OK. If desired, type a new location for your project or select a directory using the Browse button, and then choose OK.
In the "Solution Explorer" window, right-click on the "References" folder, and select "Add Reference..." from the context menu. In the "Add Reference" dialog box, select the ".NET" tab and browse to the Leadtools For .NET "<LEADTOOLS_INSTALLDIR>\Bin\Dotnet\Win32 " folder and select the following DLLs:
Switch to Form1 code view (right-click Form1 in the solution explorer then select View Code ) and add the following lines at the beginning of the file:
Imports SystemImports System.TextImports System.DiagnosticsImports System.Drawing.ImagingImports System.Windows.FormsImports LeadtoolsImports Leadtools.PrinterImports Leadtools.CodecsImports Leadtools.Document.Writer
using System;using System.Text;using System.Diagnostics;using System.Drawing.Imaging;using System.Windows.Forms;using Leadtools;using Leadtools.Printer;using Leadtools.Codecs;using Leadtools.Document.Writer;
Declare the following private variable:
Private WithEvents _printer as Printer Printer _printer; Add an event handler to the Form1 Load event and code it as follows:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)TryDim printerName As String = "LEADTOOLS Printer"If (Not Printer.IsLeadtoolsPrinter(printerName)) ThenDim printerPassword As String = "1234"Dim documentPrinterRegPath As String = "SOFTWARE\LEAD Technologies, Inc.\Printer\"Dim printerInfo As PrinterInfo = New PrinterInfo()printerInfo.ProductName = printerNameprinterInfo.PrinterName = printerNameprinterInfo.Password = printerPasswordprinterInfo.RegistryKey = documentPrinterRegPath & printerNameprinterInfo.RootDir = "C:\LEADTOOLS 20\Bin\Common\PrinterDriver\"printerInfo.Url = "https://www.leadtools.com"printerInfo.PrinterExe = Application.ExecutablePathPrinter.Install(printerInfo)End If_printer = New Printer(printerName)_printer.EnableNetworkPrinting = TrueAddHandler _printer.EmfEvent, AddressOf Of EmfEventArgsAddHandler _printer.JobEvent, AddressOf Of JobEventArgsCatch ex As ExceptionMessageBox.Show(Me, ex.Message)End TryEnd Sub
private void Form1_Load(object sender, System.EventArgs e){try{string printerName = "LEADTOOLS Printer";if (!Printer.IsLeadtoolsPrinter(printerName)){string printerPassword = "1234";string documentPrinterRegPath = @"SOFTWARE\LEAD Technologies, Inc.\Printer\";PrinterInfo printerInfo = new PrinterInfo();printerInfo.ProductName = printerName;printerInfo.PrinterName = printerName;printerInfo.Password = printerPassword;printerInfo.RegistryKey = documentPrinterRegPath + printerName;printerInfo.RootDir = @"C:\LEADTOOLS 20\Bin\Common\PrinterDriver\";printerInfo.Url = "https://www.leadtools.com";printerInfo.PrinterExe = Application.ExecutablePath;Printer.Install(printerInfo);}_printer = new Printer(printerName);_printer.EnableNetworkPrinting = true;_printer.EmfEvent += new EventHandler<EmfEventArgs>(_printer_EmfEvent);_printer.JobEvent += new EventHandler<JobEventArgs>(_printer_JobEvent);}catch (Exception ex){MessageBox.Show(this, ex.Message);}}
Add an event handler to the Emf event and code it as follows:
Private Sub printerTest_EmfEvent(ByVal sender As Object, ByVal e As EmfEventArgs) Handles printerTest.EmfEventSystem.IO.File.WriteAllBytes("c:\LEADTOOLS_IMAGE1.emf", e.Stream.ToArray())Dim metaFile As New Metafile(e.Stream)End Sub
void _printer_EmfEvent(object sender, EmfEventArgs e){System.IO.File.WriteAllBytes(@"c:\LEADTOOLS_IMAGE1.emf", e.Stream.ToArray());Metafile metaFile = new Metafile(e.Stream);}
Add an event handler to the job info event and code it as follows:
Private Sub _printer_JobEvent(ByVal sender As Object, ByVal e As JobEventArgs)Dim printerName As String = e.PrinterNameDim jobID As Integer = e.JobIDIf e.JobEventState = EventState.JobStart ThenTry'Data is a string. The user specifies the data to be sent from the client machine'check user demo for more infoDim enc As Encoding = Encoding.ASCIIDim strData As String = enc.GetString(_printer.RemoteData.UserData)MessageBox.Show(String.Format("Job {0} was started with remote data " & strData, jobID))Catch ex As ExceptionMessageBox.Show(ex.Message)ThrowEnd TryElse If e.JobEventState = EventState.JobEnd ThenMessageBox.Show(String.Format("Job {0} was Ended", jobID))Process.Start("mspaint", "c:\LEADTOOLS_IMAGE1.emf")End IfEnd Sub
void _printer_JobEvent(object sender, JobEventArgs e){string printerName = e.PrinterName;int jobID = e.JobID;if (e.JobEventState == EventState.JobStart){try{// Data is a string. The user specifies the data to be sent from the client machine// check user demo for more infoEncoding enc = Encoding.ASCII;String strData = enc.GetString(_printer.RemoteData.UserData);MessageBox.Show(string.Format("Job {0} was started with remote data " + strData, jobID));}catch (Exception ex){MessageBox.Show(ex.Message);throw;}}else if (e.JobEventState == EventState.JobEnd){MessageBox.Show(string.Format("Job 0 was Ended", jobID));Process.Start("mspaint", @"c:\LEADTOOLS_IMAGE1.emf");}}
Note: when the job event is received, the _printer.RemoteDate will return the data sent from the remote machine, this data will be available until demo receives EventState.JobEnd _printer.RemoteDate will contain row data, this raw data will be specified by the user.
Build, and Run the program to test it.