USING IRONPRINT

How to Print a QR Code in C#

Published April 29, 2024
Share:

QR codes, or Quick Response codes, have become ubiquitous in our digital age. They store information in a matrix of black squares on a white background and can be scanned using a smartphone or dedicated QR code reader. These QR codes/ barcodes using .NET barcode DLL are used for various purposes, including product labeling, mobile payments, and marketing materials. Printing QR codes is crucial for integrating physical and digital interactions seamlessly.

In this article, we are going to first generate a QR code in C# using IronQR, the QR code generator library, then convert it to PDF using IronPDF, and finally print the pixel-perfect document with a QR code image using the C# IronPrint library.

How to Print a QR Code in C#

  1. Create a Visual Studio Project
  2. Install IronQR, IronPDF, and IronPrint libraries
  3. Create a QR Code using QrWriter.Write() method
  4. Save the generated QR Code as an image using the SaveAs method
  5. Create a PDF document using IronPDF's ImageToPdfConverter 6. Adjust PrinterSettings using IronPrint
  6. Print using IronPrint's Printer.Print() method

IronPrint - The C# Printing Library

IronPrint, developed by Iron Software, is a powerful print library for .NET, offering a versatile set of tools for handling printing tasks in C#. It supports a wide range of environments, including Windows, macOS, Android, and iOS. In this article, we'll explore how IronPrint, in conjunction with IronQR and IronPDF, can be leveraged to create QR codes, and convert, and print QR codes in a C# console application.

Features of IronPrint

IronPrint distinguishes itself with dedicated classes and methods tailored for print-related functionalities. Key features include:

  • Comprehensive Print Settings: IronPrint allows developers to customize various aspects of the printing process, such as paper size, orientation, DPI, number of copies, printer name, margins, and grayscale printing.
  • Versatile Printing with Printer Class: The library introduces the Printer class, providing a comprehensive set of methods for printing various file types, including images and PDF documents.
  • Cross-Platform Support: IronPrint supports printing across multiple platforms, making it suitable for a variety of applications.

Prerequisites

Before diving into the steps of creating a console application in C# to print QR codes, ensure you have the following prerequisites:

  1. Visual Studio: Install Microsoft Visual Studio, a powerful integrated development environment for C#. You can download it from its official website.
  2. IronQR Library: This library is essential for generating QR codes. Install it using the NuGet Console or directly from the official IronQR NuGet website.
  3. IronPDF Library: IronPDF will be used to convert the generated QR code barcode images to a PDF. Install it using the same NuGet installation method.
  4. IronPrint Library: Finally, install the IronPrint library to enable seamless printing in your C# application.

Create a C# Console Application in Visual Studio

Follow these steps to set up a C# console application in Visual Studio:

  1. Open Visual Studio and create a new C# Console Application
    1. Configure the Project as follows and then click "Next"

How to Print a QR Code in C#: Figure 1 - Configure your new C# Console App project by specifying the Project name, location and solution name. Then click on the "Next" button.

  1. Next, for additional information, choose the .NET Framework and click "Create".

Install Necessary Libraries via NuGet Package Manager

Follow the steps to install the necessary libraries:

  1. Open NuGet Package Manager Console or NuGet Package Manager for Solutions using the Tools menu or Solution Explorer in the Visual Studio project.

    1. Install IronQR QR Code library:

      • Using the NuGet Package Manager Console, add the following command:
      Install-Package IronQR
      • Using the Manage NuGet Packages for Solutions: In the browse tab of NuGet, search for the "IronQR", QR Code library and click Install.

How to Print a QR Code in C#: Figure 2 - Install IronQR using the Manage NuGet Package for Solutions by searching "IronQR"

  1. Install IronPDF PDF Library:

    • Using the NuGet Package Manager Console, enter the following command:

      Install-Package IronPdf
    • Using the Manage NuGet Packages for Solutions: In the browse tab of NuGet, search for the "IronPDF" library and click Install.

How to Print a QR Code in C#: Figure 3 - Install IronPDF using the Manage NuGet Package for Solutions by searching "IronPDF"

  1. Install IronPrint Printing Library:

    • Using the NuGet Package Manager Console, add the following command:

      Install-Package IronPrint
    • Using the Manage NuGet Packages for Solutions: In the browse tab of NuGet, search for the "IronPrint" library and click install.

How to Print a QR Code in C#: Figure 4 - Install IronPrint using the Manage NuGet Package for Solutions by searching "IronPrint"

Steps to Create, Convert, and Print QR Codes

Let's break down the process of creating QR Codes, converting QR codes to PDF, and finally printing the output of the QR code generator in a step-by-step procedure using IronQR, IronPDF, and IronPrint.

Step-by-Step Procedure to Print C# QR Code

Step 1: Reference Libraries

In the Program.cs file at the top, we will include references to the required libraries. This ensures that the libraries are installed and ready to be used.

// Reference to libraries
using IronPrint;                    // Library for printing functionalities
using IronPdf;                      // Library for PDF handling
using IronQr;                       // Library for QR code generation
using IronSoftware.Drawing;         // Library for image processing
// Reference to libraries
using IronPrint;                    // Library for printing functionalities
using IronPdf;                      // Library for PDF handling
using IronQr;                       // Library for QR code generation
using IronSoftware.Drawing;         // Library for image processing
' Reference to libraries
Imports IronPrint ' Library for printing functionalities
Imports IronPdf ' Library for PDF handling
Imports IronQr ' Library for QR code generation
Imports IronSoftware.Drawing ' Library for image processing
VB   C#

Step 2: Create a QR Code Using IronQR

In this step, firstly we are going to generate a QR code using IronQR and then save it as an image, as shown in the following code example:

// Code to create a QR code using IronQR
QrCode myQr = QrWriter.Write("Hello IronPrint!");  // Generate a QR code with the message
AnyBitmap qrImage = myQr.Save();                    // Save QR code as an image
qrImage.SaveAs("assets/qr.png");                    // Save the png image file to the "assets" folder
// Code to create a QR code using IronQR
QrCode myQr = QrWriter.Write("Hello IronPrint!");  // Generate a QR code with the message
AnyBitmap qrImage = myQr.Save();                    // Save QR code as an image
qrImage.SaveAs("assets/qr.png");                    // Save the png image file to the "assets" folder
' Code to create a QR code using IronQR
Dim myQr As QrCode = QrWriter.Write("Hello IronPrint!") ' Generate a QR code with the message
Dim qrImage As AnyBitmap = myQr.Save() ' Save QR code as an image
qrImage.SaveAs("assets/qr.png") ' Save the png image file to the "assets" folder
VB   C#

In this code snippet:

  • A QR code is generated with the message "Hello IronPrint!" using the QrWriter class. The Write() method allows for the generation of a QR Code with a message or even with numeric data.
  • The QR code is then saved as an AnyBitmap file, a universally compatible C# Bitmap class provided by IronSoftware.
  • The QR code image is saved to the "assets" folder with the name "qr.png".

Here is the QR code output:

How to Print a QR Code in C#: Figure 5 - QR Code Output Image: qr.png

Step 3: Convert QR Image to PDF Using IronPDF

Next, we are going to convert the QR code image to a PDF using IronPDF. PDFs preserve the format of the document and are suitable for sharing and printing. Here, each image file will be placed on a separate PDF file page.

// Code to convert QR Image to PDF using IronPDF
var imageFiles = Directory.EnumerateFiles("assets").Where(f => f.EndsWith(".jpg") || f.EndsWith(".png"));  // Reading QR codes image files
// Convert the QR code images to a PDF and save it
ImageToPdfConverter.ImageToPdf(imageFiles).SaveAs("assets/composite.pdf");
// Code to convert QR Image to PDF using IronPDF
var imageFiles = Directory.EnumerateFiles("assets").Where(f => f.EndsWith(".jpg") || f.EndsWith(".png"));  // Reading QR codes image files
// Convert the QR code images to a PDF and save it
ImageToPdfConverter.ImageToPdf(imageFiles).SaveAs("assets/composite.pdf");
' Code to convert QR Image to PDF using IronPDF
Dim imageFiles = Directory.EnumerateFiles("assets").Where(Function(f) f.EndsWith(".jpg") OrElse f.EndsWith(".png")) ' Reading QR codes image files
' Convert the QR code images to a PDF and save it
ImageToPdfConverter.ImageToPdf(imageFiles).SaveAs("assets/composite.pdf")
VB   C#

In this code snippet:

  • Firstly, it enumerates through image files in the "assets" folder with extensions ".jpg" or ".png" and then proceeds to read QR codes from the directory.
  • Utilizes ImageToPdfConverter.ImageToPdf() method from IronPDF to convert the images to a PDF named "composite.pdf".

Here is the output:

How to Print a QR Code in C#: Figure 6 - Output PDF containing QR code image: composite.pdf

Step 4: Print PDF Using IronPrint

Finally, we will use IronPrint - a versatile Printing library to print the generated PDF with Printer Settings.

// Code for Printing using IronPrint
// Get available printer names
List<string> printerNames = Printer.GetPrinterNames();
// Create print settings object
PrintSettings printerSettings = new PrintSettings();
foreach(string printerName in printerNames)
{
    // Set desired printer name
    if(printerName.Equals("Microsoft Print to PDF"))
        printerSettings.PrinterName = printerName;
}
//Configure print setting
printerSettings.PaperSize = PaperSize.A4;                    // Set paper size
Margins margins = new Margins(30,10);                        // Set paper margins
printerSettings.PaperMargins = margins;                      // Apply margins
Printer.Print("assets/composite.pdf", printerSettings);      // Print the PDF
// Code for Printing using IronPrint
// Get available printer names
List<string> printerNames = Printer.GetPrinterNames();
// Create print settings object
PrintSettings printerSettings = new PrintSettings();
foreach(string printerName in printerNames)
{
    // Set desired printer name
    if(printerName.Equals("Microsoft Print to PDF"))
        printerSettings.PrinterName = printerName;
}
//Configure print setting
printerSettings.PaperSize = PaperSize.A4;                    // Set paper size
Margins margins = new Margins(30,10);                        // Set paper margins
printerSettings.PaperMargins = margins;                      // Apply margins
Printer.Print("assets/composite.pdf", printerSettings);      // Print the PDF
' Code for Printing using IronPrint
' Get available printer names
Dim printerNames As List(Of String) = Printer.GetPrinterNames()
' Create print settings object
Dim printerSettings As New PrintSettings()
For Each printerName As String In printerNames
	' Set desired printer name
	If printerName.Equals("Microsoft Print to PDF") Then
		printerSettings.PrinterName = printerName
	End If
Next printerName
'Configure print setting
printerSettings.PaperSize = PaperSize.A4 ' Set paper size
Dim margins As New Margins(30,10) ' Set paper margins
printerSettings.PaperMargins = margins ' Apply margins
Printer.Print("assets/composite.pdf", printerSettings) ' Print the PDF
VB   C#

In this source code snippet:

  • Fetches available printer names using Printer.GetPrinterNames().
  • Sets the desired printer name (in this case, "Microsoft Print to PDF" to demonstrate the printing process). If not specified, it will use the default printer attached.
  • Configures print settings, specifying paper size as A4 and setting margins. Margins struct have multiple overloads and it also presents fields like Top, Bottom, Left, and Right to set the margin as needed.
  • Prints the PDF using a Printer.Print(). The first argument is the path to the file to be printed and the second is the printerSettings if specified.

Here is the output of the print file. This shows how the image will be printed:

How to Print a QR Code in C#: Figure 7 - Output print file: composite.pdf

Printing to a physical printer is much easier with IronPrint. To get more control while printing, you can use ShowPrintDialog() method. For more information on how to Print efficiently, please visit this documentation page.

Advantages of IronPrint for Printing in C#

IronPrint is specifically designed as a powerful print library for .NET applications. Unlike IronPDF, which is primarily focused on handling PDF-related tasks, and Microsoft printing, which is a general-purpose printing mechanism, IronPrint provides a dedicated set of classes and methods tailored for fine-grained control over the printing process.

With IronPrint, developers can leverage:

1. Asynchronous Printing

IronPrint provides asynchronous functions, preventing print operations from blocking threads and enhancing performance.

2. Versatile Printing

The dedicated Printer class in IronPrint allows for versatile printing of various file types, offering flexibility beyond standard printing.

3. Cross-Platform Support

IronPrint supports multiple platforms, including Windows, Android, iOS, and macOS, making it suitable for diverse application environments.

4. Customizable Print Settings

Developers can finely control print settings, including paper size, orientation, DPI, number of copies, and more, through the PrintSettings class.

Conclusion

In conclusion, the combination of IronPrint, IronQR, and IronPDF provides a robust solution for creating, converting, and printing QR codes in C#. The advantages of asynchronous printing, versatile printing options, and cross-platform support make IronPrint a valuable tool for developers. By following the detailed steps outlined in this guide, you can generate QR code barcodes and also seamlessly integrate their printing into your C# applications, bridging the gap between physical and digital interactions.

IronPrint offers a free-trialstarts from $749. Download the library from here and give it a try.

< PREVIOUS
How to Print with Network Printer in C#
NEXT >
How to Print a File to a Printer in C#

Ready to get started? Version: 2024.10 just released

Free NuGet Download Total downloads: 10,762 View Licenses >