Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
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.
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.
IronPrint distinguishes itself with dedicated classes and methods tailored for print-related functionalities. Key features include:
Before diving into the steps of creating a console application in C# to print QR codes, ensure you have the following prerequisites:
Follow these steps to set up a C# console application in Visual Studio:
Configure the Project as follows and then click "Next"
Follow the steps to install the necessary libraries:
Install IronQR QR Code library:
Install-Package IronPrint
Install IronPDF PDF Library:
Using the NuGet Package Manager Console, enter the following command:
Install-Package IronPrint
Install IronPrint Printing Library:
Using the NuGet Package Manager Console, add the following command:
Install-Package IronPrint
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.
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
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
In this code snippet:
Here is the QR code output:
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")
In this code snippet:
Here is the output:
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
In this source code snippet:
Here is the output of the print file. This shows how the image will be printed:
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.
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:
IronPrint provides asynchronous functions, preventing print operations from blocking threads and enhancing performance.
The dedicated Printer class in IronPrint allows for versatile printing of various file types, offering flexibility beyond standard printing.
IronPrint supports multiple platforms, including Windows, Android, iOS, and macOS, making it suitable for diverse application environments.
Developers can finely control print settings, including paper size, orientation, DPI, number of copies, and more, through the PrintSettings class.
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-trial starts from $749. Download the library from here and give it a try.
9 .NET API products for your office documents