Print Any File to Installed Printer - Console C# .NET 6

This tutorial shows how to print out any supported file format to an installed printer using the LEADTOOLS SDK in a C# Windows Console application.

Overview  
Summary This tutorial covers how to print a file to an installed printer in a C# Windows Console application.
Completion Time 20 minutes
Visual Studio Project Download tutorial project (2 KB)
Platform Windows Console C# Application
IDE Visual Studio 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 Any File 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>\LEADTOOLS23\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 functionality 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 File 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 code below to load in the file as a LEADDocument object to be passed to the printer.

C#
static void Main(string[] args) 
{ 
    SetLicense(); 
 
    using (LEADDocument document = DocumentFactory.LoadFromFile(@"C:\LEADTOOLS23\Resources\Images\Leadtools.pdf", new LoadDocumentOptions())) 
        PrintDocument(document); 
 
    Console.WriteLine("File Printed!"); 
} 

Inside the Program class, add a new method named PrintDocument(LEADDocument document). This method will be called inside the Main() method as shown above. Add the below code to the PrintDocument() method to create the PrintDocument instance, set the Printer Settings, and print the file.

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; 
        // This is where you pass the name to your installed printer 
        printDocument.PrinterSettings.PrinterName = "ADD PRINTER NAME"; 
        printDocument.DefaultPageSettings = new PageSettings(); 
 
        int pageNumber = printDocument.PrinterSettings.FromPage; 
 
        printDocument.PrintPage += (object sender, PrintPageEventArgs e) => PrintPageHandler(e, document, printDocument, ref pageNumber); 
        printDocument.Print(); 
    } 
} 

Create a new print page event handler named PrintPageHandler(PrintPageEventArgs e, LEADDocument document, PrintDocument printDocument, ref int pageNumber). Hook the event handler inside the PrintDocument() method as shown above. Add the code below to the new event handler.

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() method.

C#
static void PrintPage(LEADDocument document, int pageNumber, PrintPageEventArgs e) 
{ 
    DocumentPage 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 and loads in a LEADTOOLS supported file that is then sent as a print job to the specified printer to be printed.

Wrap-up

This tutorial showed how to load in a file, ready that file's pages for a print job, and then print that file to a specified installed printer. It also covered how to use the LEADDocument, DocumentPage, and PrintDocument(System.Drawing.Printing) classes.

See Also

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


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