- Start Visual Studio .NET.
- Choose File->New->Project… from the menu.
- In the New Project dialog box, choose either "Visual C# Projects" or "Visual Basic Projects" in the Projects Type List, and choose "Windows Application " in the Templates List.
- Type the project name as " PRINTER DRIVER " 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 Leadtools For .NET "<LEADTOOLS_INSTALLDIR>\Bin\DotNet\Win32 " folder and select the following DLLs:
- Leadtools.dll
- Leadtools.Forms.DocumentWriters.dll
- Leadtools.Codecs.dll
- Leadtools.WinForms.CommonDialogs.File.dll
- Leadtools.Printer.dll
- 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:
[Visual Basic]
Imports Leadtools Imports Leadtools.Printer Imports Leadtools.Codecs import Leadtools.Forms.DocumentWriters Imports Leadtools.WinForms
[C#]using Leadtools; using Leadtools.Printer; using Leadtools.Codecs; using Leadtools.Forms.DocumentWriters; using Leadtools.WinForms.CommonDialogs.File;
- Declare the following private variable:
[Visual Basic]
Private WithEvents _printer as Printer
[C#]Printer _printer;
- Add an event handler to the Form1 Load event and code it as follows:
[Visual Basic]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Try Dim printerName as string="LEADTOOLS Printer" Dim printerPassword as string="1234" Dim documentPrinterRegPath As String = "SOFTWARE\\LEAD Technologies, Inc.\\Printer\\" Dim printerInfo As New PrinterInfo() printerInfo.DriverName = printerName printerInfo.ProductName = printerName printerInfo.PrinterName = printerName printerInfo.Password = printerPassword printerInfo.RegistryKey = documentPrinterRegPath + printerName printerInfo.RootDir = "<LEADTOOLS_INSTALLDIR>\Bin\Common\PrinterDriver\" printerInfo.Url = "http://www.Leadtools.com" printerInfo.PrinterExe = Application.ExecutablePath Printer.Install(printerInfo) Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub
[C#]private void Form1_Load(object sender, System.EventArgs e) { try { string printerName="LEADTOOLS Printer"; string printerPassword="1234"; string documentPrinterRegPath = @"SOFTWARE\LEAD Technologies, Inc.\Printer\"; PrinterInfo printerInfo = new PrinterInfo(); printerInfo.DriverName = printerName; printerInfo.ProductName = printerName; printerInfo.PrinterName = printerName; printerInfo.Password = printerPassword; printerInfo.RegistryKey = documentPrinterRegPath + printerName; printerInfo.RootDir = @"<LEADTOOLS_INSTALLDIR>\Bin\Common\PrinterDriver\"; printerInfo.Url = "http://www.Leadtools.com"; printerInfo.PrinterExe = Application.ExecutablePath; Printer.Install(printerInfo); _printer=new Printer(printerName); _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:
[Visual Basic]
Private Sub printerTest_EmfEvent(ByVal sender As Object, ByVal e As EmfEventArgs) Handles printerTest.EmfEvent System.IO.File.WriteAllBytes("c:\LEADTOOLS_IMAGE1.emf", e.Stream.ToArray()) Dim metaFile As New Metafile(e.Stream) End Sub
[C#]void _printer_EmfEvent(object sender, EmfEventArgs e) { System.IO.File.WriteAllBytes(@"c:\1.emf", e.Stream.ToArray()); Metafile metaFile = new Metafile(e.Stream); }
- Add an event handler to the job info event and code it as follows:
[Visual Basic][C#]
void _printer_JobEvent(object sender, JobEventArgs e) { string printerName = e.PrinterName; int jobID = e.JobID; if (e.JobEventState == EventState.JobStart) { MessageBox.Show(string.format("Job {0} was started",jobID)); } else if (e.JobEventState == EventState.JobEnd) { MessageBox.Show(string.format("Job {0} was Ended",jobID)); } }
- Build, and Run the program to test it.