How to Print a Barcode in C#

Barcodes serve as unique identifiers that store information in a visually scannable format. They are widely used in retail, logistics, healthcare, and many other industries for tasks such as inventory management, product labeling, and asset tracking. Printing barcodes is essential for streamlining operations, reducing errors, and enhancing overall efficiency in data management.

Barcodes play a crucial role in modern-day business operations, facilitating efficient tracking, inventory management, and quick data retrieval.

In this article, we will explore how to generate barcodes and print them in C# using IronBarcode, IronPDF, and IronPrint libraries.

How to Print a Barcode in C#

  1. Create a Visual Studio Project
  2. Install IronBarcode, IronPDF and IronPrint libraries

  3. Create barcode image using BarcodeWriter.CreateBarcode method

  4. Save generated barcode as Image using SaveAs method

  5. Create PDF document using IronPDF's ImageToPdfConverter 6. Adjust PrinterSettings using IronPrint

  6. Print using IronPrint's Printer.Print method

IronPrint - The C# Print Library

Developed by Iron Software, IronPrintis a robust print library designed for .NET, elevating C# printing capabilities. Let's explore the key features that make IronPrint stand out, making it a valuable companion when working alongside IronBarcode and IronPDF in a C# console application.

Key Features of IronPrint

1. Easy Customization

IronPrint puts you in control, allowing easy customization of various printing aspects. From choosing paper size, adjusting DPI, and setting margins to specifying the number of copies, printer name, and even grayscale printing, IronPrint ensures flexibility.

2. Versatile Printing with Printer Class

The introduction of the Printer class is a game-changer. It provides a set of methods for effortlessly handling the printing of different file types, from images to PDF documents. This versatility adds a layer of flexibility to your printing options.

3. Cross-Platform Support

IronPrint is versatile, supporting various platforms such as Windows, macOS, Android, and iOS. This cross-platform compatibility ensures a consistent printing experience across different application environments.

How IronPrint Enhances Printing

1. Fine-Tuned Print Settings: IronPrint allows you to dive into print settings, giving you precise control over elements like paper size, orientation, DPI, and more. This level of customization ensures that your printed output meets your specific requirements.

2. Printer Class Convenience: The Printer class expands your possibilities, enabling you to print not just documents but a variety of file types effortlessly. The methods offered by the Printer class streamline your printing workflows tailored to your application's needs.

3. Platform-Friendly: IronPrint's commitment to supporting multiple platforms makes it an ideal choice for developers working on applications for different environments. Whether it's a desktop application on Windows or a mobile app on iOS or Android, IronPrint delivers a reliable printing experience.

Prerequisites

Before diving into the steps of creating a C# console application to print Barcodes, 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. IronBarcode Library: This library is essential to generate barcode images. Install it using the NuGet Package Manager Console or directly from the official IronBarcode NuGet website.

  3. IronPDF Library: IronPDFwill be used to convert the generated barcode data matrix and images to a PDF.

  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 following and the click "Next"

How to Print a Barcode 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. From Additional Information, choose the appropriate .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 tools menu or Solution Explorer in Visual Studio project.
  2. In the browse tab of NuGet, search for the libraries and click install.

    1. Install Barcode Library - IronBarcode:

      • Using the NuGet Package Manager Console, add the following command:
      Install-Package BarCode
      • Using the Manage NuGet Packages for Solutions

How to Print a Barcode in C#: Figure 2 - Install IronBarcode using the Manage NuGet Package for Solutions by searching "IronBarcode" in the search bar of NuGet Package Manager, then select the project and click on the Install button.

  1. Install IronPDF PDF Library:

    • Using the NuGet Package Manager Console

      Install-Package BarCode
    • Using the Manage NuGet Packages for Solutions

How to Print a Barcode in C#: Figure 3 - Install IronPDF using the Manage NuGet Package for Solutions by searching "ironpdf" in the search bar of NuGet Package Manager, then select the project and click on the Install button.

  1. Install IronPrint Printing Library:

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

      Install-Package BarCode
    • Using the Manage NuGet Packages for Solutions

How to Print a Barcode in C#: Figure 4 - Install IronPrint using the Manage NuGet Package for Solutions by searching "ironprint" in the search bar of NuGet Package Manager, then select the project and click on the Install button.

Steps to Generate, Convert and Print Barcode

Let's break down the process of generating barcodes, converting the generated barcode image to PDF and finally printing the barcode in a step-by-step procedure using IronBarcode, IronPDF, and IronPrint.

Step-by-Step Procedure to Print C# Barcode

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 IronBarcode;                       // Library for barcode generation
using IronSoftware.Drawing;         // Library for image processing
// Reference to libraries
using IronPrint;                    // Library for printing functionalities
using IronPdf;                      // Library for PDF handling
using IronBarcode;                       // Library for barcode generation
using IronSoftware.Drawing;         // Library for image processing
' Reference to libraries
Imports IronPrint ' Library for printing functionalities
Imports IronPdf ' Library for PDF handling
Imports IronBarcode ' Library for barcode generation
Imports IronSoftware.Drawing ' Library for image processing
VB   C#

Step 2: Generate Barcode Using IronBarcode

Here, we use IronBarcode to create a barcode and save it as an image. We can even generate a barcode of QR code type.

// Code to generate a barcode
var myBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.EAN8); myBarcode.SaveAsImage("assets/barcode.png");
// Code to generate a barcode
var myBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.EAN8); myBarcode.SaveAsImage("assets/barcode.png");
' Code to generate a barcode
Dim myBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.EAN8)
myBarcode.SaveAsImage("assets/barcode.png")
VB   C#

In this source code:

  • The BarcodeWriter.CreateBarcode() method is used to generate an EAN-8 barcode with the data "12345".
  • The resulting barcode is saved as an image file (EAN8.jpeg).

Here is the output barcode image:

How to Print a Barcode in C#: Figure 5 - Barcode output image using IronBarcode library

Step 3: Convert Barcode Image to PDF Using IronPDF

Optionally, convert the barcode image to a PDF using IronPDF. This preserves the formatting and gives more control over size and printing functionalities.

// Code to convert barcode image to PDF using IronPDF ImageToPdfConverter.ImageToPdf("assets/barcode.png").SaveAs("assets/composite.pdf");
// Code to convert barcode image to PDF using IronPDF ImageToPdfConverter.ImageToPdf("assets/barcode.png").SaveAs("assets/composite.pdf");
' Code to convert barcode image to PDF using IronPDF ImageToPdfConverter.ImageToPdf("assets/barcode.png").SaveAs("assets/composite.pdf");
VB   C#

The barcode.png file is saved as a PDF document. Each image is placed on a separate page. Here is the output:

How to Print a Barcode in C#: Figure 6 - Barcode Image to PDF output file using IronPDF: composite.pdf

Step 4: Adjust PrinterSettings using IronPrint

Configure print settings such as paper size, margins, and printer name using IronPrint.

// Code to adjust PrinterSettings using IronPrint
List<string> printerNames = Printer.GetPrinterNames();
PrintSettings printerSettings = new PrintSettings();
foreach(string printerName in printerNames)
{
    if(printerName.Equals("Microsoft Print to PDF"))
        printerSettings.PrinterName = printerName;
}
printerSettings.PaperSize = PaperSize.A4;
Margins margins = new Margins(30, 10);
printerSettings.PaperMargins = margins;
// Code to adjust PrinterSettings using IronPrint
List<string> printerNames = Printer.GetPrinterNames();
PrintSettings printerSettings = new PrintSettings();
foreach(string printerName in printerNames)
{
    if(printerName.Equals("Microsoft Print to PDF"))
        printerSettings.PrinterName = printerName;
}
printerSettings.PaperSize = PaperSize.A4;
Margins margins = new Margins(30, 10);
printerSettings.PaperMargins = margins;
' Code to adjust PrinterSettings using IronPrint
Dim printerNames As List(Of String) = Printer.GetPrinterNames()
Dim printerSettings As New PrintSettings()
For Each printerName As String In printerNames
	If printerName.Equals("Microsoft Print to PDF") Then
		printerSettings.PrinterName = printerName
	End If
Next printerName
printerSettings.PaperSize = PaperSize.A4
Dim margins As New Margins(30, 10)
printerSettings.PaperMargins = margins
VB   C#

The above sample code provides PrinterSettings options that gives more control over the printing process in any .NET barcode generator application.

For more Printing options, please visit the code examples page.

Step 5: Print using IronPrint Printer.Print Method

Finally, trigger the barcode printing using IronPrint as shown in the following code:

// Code to print
Printer.Print("assets/composite.pdf", printerSettings);
// Code to print
Printer.Print("assets/composite.pdf", printerSettings);
' Code to print
Printer.Print("assets/composite.pdf", printerSettings)
VB   C#

Although IronPrint provides the facility to print directly from the image format, here we are printing it from PDF. Here is the output PDF file by IronPrint:

How to Print a Barcode in C#: Figure 7 - Output PDF file using IronPrint: Barcode.pdf

Advantage of IronPrint: Comprehensive Printing Capabilities

IronPrint is specifically designed as a powerful print library for .NET applications. Unlike IronPDF, which is primarily focused on handling PDF-related tasks, and C# 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:

  • Customizable Print Settings: IronPrint allows developers to finely control various aspects of the printing process, such as paper size, orientation, DPI, number of copies, printer name, margins, and grayscale printing. This level of customization is beneficial when precision in printing is crucial for specific application requirements.
  • Versatile Printing with Printer Class: The introduction of the Printer class in IronPrint provides a comprehensive set of methods for printing various file types, including images and PDF documents. This versatility goes beyond standard printing functionalities and offers flexibility in handling diverse file formats.
  • Asynchronous Printing: IronPrint supports asynchronous functions, preventing print operations from blocking threads. Asynchronous printing enhances application performance, ensuring a smooth user experience even when dealing with extensive printing tasks.

These advantages make IronPrint a preferred choice when developers require a specialized and feature-rich printing library, offering greater control and customization options compared to more generalized printing solutions.

Conclusion

In conclusion, IronBarcode provides a seamless solution for generating barcodes in C#, and when combined with IronPrint, the process of printing these barcodes becomes highly efficient. By following the outlined steps and taking advantage of the features provided by IronPrint, developers can integrate barcode generation and printing into their C# applications with ease.

For more information on how to print efficiently, please visit this documentation page.

IronPrint offers a free trial to explore its complete functionality and capabilities. Perpetual license options are available for various needs, starting from $749. Download the library from hereand enhance your C# application with printing capabilities.