Print LEADDocument to Installed Printer - Console C#

This tutorial shows how to load any LEADTOOLS supported file as a LEADDocument and then print that file to an installed printer in a C# Window Console application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to print a LEADDocument to an installed printer in a C# Windows Console application.
Completion Time 20 minutes
Visual Studio Project Download tutorial project (3 KB)
Platform C# Windows Console Application
IDE Visual Studio 2019, 2022
Development License Download LEADTOOLS

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Print LEADDocument to Installed Printer - Console C# tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added by one or the other of the following two methods (but not both).

If using NuGet references, this tutorial requires the following NuGet package:

If using local DLL references, the following DLLs are needed.

The DLLs are located at <INSTALL_DIR>\LEADTOOLS22\Bin\Dotnet4\x64:

For a complete list of which DLL files are required for your application, refer to Files to be Included in your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Add the Print Document Code

With the project created, the references added, and the license set, coding can begin.

In the Solution Explorer, open Program.cs. Add the following statements to the using block at the top of Program.cs:

C#
using System; 
using System.Drawing.Printing; 
using System.IO; 
using Leadtools; 
using Leadtools.Document; 
using Leadtools.Drawing; 

Inside the Main() method add the following code below the SetLicense() method call, to load your file as a LEADDocument object.

C#
static void Main(string[] args) 
{ 
 
    SetLicense(); 
    using (LEADDocument _document = DocumentFactory.LoadFromFile(@"PLACE FILE PATH TO FILE HERE", new LoadDocumentOptions())) 
        PrintDocument(_document); 
} 

Add a new method to the Program class named PrintDocument(LEADDocument _document). This new method will be called inside the Main() method, as shown above. Add the code to the PrintDocument() method to create a System.Drawing.Printing.PrintDocument from our LEADDocument and start the printing process.

C#
static void PrintDocument(LEADDocument _document) 
{ 
    using (PrintDocument printDocument = new PrintDocument()) 
    { 
        printDocument.PrinterSettings.MinimumPage = 1; 
        printDocument.PrinterSettings.MaximumPage = _document.Pages.Count; 
        printDocument.PrinterSettings.FromPage = 1; 
        printDocument.PrinterSettings.ToPage = _document.Pages.Count; 
        printDocument.PrinterSettings.PrinterName = "INPUT PRINTER NAME"; 
        printDocument.DefaultPageSettings = new PageSettings(); 
 
        var pageNumber = printDocument.PrinterSettings.FromPage; 
 
        printDocument.PrintPage += (object sender, PrintPageEventArgs e) => PrintPageHandler(e, _document, printDocument, ref pageNumber); 
        printDocument.Print(); 
    } 
} 

Note

This tutorial will allow you to print the loaded LEADDocument to any installed physical or virtual printer. Just be sure to insert the printer name string for the PrinterName property, as shown above. You can install a virtual printer using the Virtual Printer Driver Demo, located here: <INSTALL_DIR>\LEADTOOLS22\Shortcuts\Virtual Printer\.NET Framework Class Libraries\Virtual Printer.

Add a new PrintPage event handler to the Program class, named PrintPageHandler(PrintPageEventArgs e, LEADDocument document, PrintDocument printDocument, ref int pageNumber). This event handler will be hooked inside the PrintDocument() method. Add the code below to PrintPageHandler() to create the output document to print.

C#
private static void PrintPageHandler(PrintPageEventArgs e, LEADDocument document, PrintDocument printDocument, ref int pageNumber) 
{ 
    PrintPage(document, pageNumber, e); 
    pageNumber++; 
 
    e.HasMorePages = (pageNumber <= printDocument.PrinterSettings.ToPage); 
    if (!e.HasMorePages) 
        pageNumber = 1; 
} 

Add a new method to the Program class named PrintPage(LEADDocument document, int pageNumber, PrintPageEventArgs e). This method will be called inside the PrintPageHandler event handler, as shown above. Add the code below to the PrintPage() to create the output and print it.

C#
static void PrintPage(LEADDocument document, int pageNumber, PrintPageEventArgs e) 
{ 
    var page = document.Pages[pageNumber - 1]; 
 
    // Get page size in pixels 
    var pixelSize = page.SizeToPixels(page.Size); 
    // Convert to DPI 
    var size = LeadSizeD.Create(pixelSize.Width * 96.0 / page.Resolution, pixelSize.Height * 96.0 / page.Resolution).ToLeadSize(); 
    // Fit in the margin bounds 
    var destRect = LeadRect.Create(e.MarginBounds.X, e.MarginBounds.Y, e.MarginBounds.Width, e.MarginBounds.Height); 
    destRect = RasterImage.CalculatePaintModeRectangle(size.Width, size.Height, destRect, RasterPaintSizeMode.Fit, RasterPaintAlignMode.Center, RasterPaintAlignMode.Center); 
 
    // Get the page image 
    using (var rasterImage = page.GetImage()) 
    using (var bitmap = RasterImageConverter.ConvertToImage(rasterImage, ConvertToImageOptions.None)) 
        e.Graphics.DrawImage(bitmap, destRect.X, destRect.Y, destRect.Width, destRect.Height); 
} 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the application runs, loads the LEADDocument, and prints the file to the given printer specified in the PrinterName property.

Wrap-up

This tutorial showed how to use the LEADDocument and PrintDocument classes to print any LEADTOOLS supported file format to an installed printer on your machine. It also covered how to use the PrintPageEventArgs and DocumentPage classes.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.


Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.