Skip to footer content
USING IRONBARCODE

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 a barcode image using the BarcodeWriter.CreateBarcode method
  4. Save the generated barcode as an image using the SaveAs method
  5. Create a PDF document using IronPDF's ImageToPdfConverter
  6. Adjust PrinterSettings using IronPrint
  7. Print using IronPrint's Printer.Print method

IronPrint - The C# Print Library

Developed by Iron Software, IronPrint is 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: IronPDF will 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.
  2. Configure the Project as follows and then 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 the tools menu or Solution Explorer in the Visual Studio project.
  2. In the browse tab of NuGet, search for the libraries and click install.
  3. Install Barcode Library - IronBarcode:
    • Using the NuGet Package Manager Console, add the following command:
      Install-Package Barcode
      Install-Package Barcode
      SHELL
    • 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 IronPdf
      Install-Package IronPdf
      SHELL
    • 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 IronPrint
      Install-Package IronPrint
      SHELL
    • 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.

// References 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
// References 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
' References 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
$vbLabelText   $csharpLabel

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("1212345", BarcodeWriterEncoding.EAN8);
myBarcode.SaveAsImage("assets/barcode.png");
// Code to generate a barcode
var myBarcode = BarcodeWriter.CreateBarcode("1212345", BarcodeWriterEncoding.EAN8);
myBarcode.SaveAsImage("assets/barcode.png");
' Code to generate a barcode
Dim myBarcode = BarcodeWriter.CreateBarcode("1212345", BarcodeWriterEncoding.EAN8)
myBarcode.SaveAsImage("assets/barcode.png")
$vbLabelText   $csharpLabel

In this source code:

  • The BarcodeWriter.CreateBarcode() method is used to generate an EAN-8 barcode with the data "1212345".
  • The resulting barcode is saved as an image file in 'assets/barcode.png'.

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
var pdfDocument = IronPdf.PdfDocument.FromFile("assets/barcode.png");
pdfDocument.SaveAs("assets/composite.pdf");
// Code to convert barcode image to PDF
var pdfDocument = IronPdf.PdfDocument.FromFile("assets/barcode.png");
pdfDocument.SaveAs("assets/composite.pdf");
' Code to convert barcode image to PDF
Dim pdfDocument = IronPdf.PdfDocument.FromFile("assets/barcode.png")
pdfDocument.SaveAs("assets/composite.pdf")
$vbLabelText   $csharpLabel

The barcode.png file is saved as a PDF document. 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
$vbLabelText   $csharpLabel

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)
$vbLabelText   $csharpLabel

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 here and enhance your C# application with printing capabilities.

Frequently Asked Questions

How can I generate barcodes in C#?

You can generate barcodes in C# using IronBarcode's BarcodeWriter.CreateBarcode method to create barcode images suitable for various applications.

What steps are involved in printing barcodes in C#?

To print barcodes in C#, generate barcode images using IronBarcode, convert them to PDFs with IronPDF, and use IronPrint to handle the printing with customizable settings.

How do I install necessary libraries for barcode printing in C#?

Install IronBarcode, IronPDF, and IronPrint libraries using NuGet Package Manager in Visual Studio to set up your C# application for barcode printing.

What is the advantage of using IronPrint for printing barcodes?

IronPrint offers advanced printing capabilities such as customizable print settings, asynchronous printing, and support for printing various file types, making it ideal for precise and flexible barcode printing.

How can I customize print settings for barcodes in C#?

Use IronPrint's PrinterSettings class to customize settings like paper size, DPI, orientation, and margins for barcode printing.

Can I print barcode images on different operating systems using a C# library?

Yes, IronPrint supports multiple platforms including Windows, macOS, Android, and iOS, ensuring consistent printing experiences regardless of the operating system.

Why is it beneficial to use IronPDF when printing barcodes?

IronPDF allows for the conversion of barcode images to PDFs, offering better control over the printing layout and ensuring high-quality prints.

What features make IronPrint a specialized solution for printing in C#?

IronPrint provides fine-grained control over printing with features like customizable print settings, versatile file handling, and asynchronous printing, setting it apart from general-purpose libraries.

How does IronPrint handle printing for different file types?

IronPrint's Printer class can print various file types, including images and PDFs, providing flexibility in managing different printing tasks.

What are the benefits of integrating barcode generation and printing into C# applications?

Integrating barcode generation and printing into C# applications enhances operational efficiency, reduces errors, and streamlines data management processes across various industries.

Jordi Bardia
Software Engineer
Jordi is most proficient in Python, C# and C++, when he isn’t leveraging his skills at Iron Software; he’s game programming. Sharing responsibilities for product testing, product development and research, Jordi adds immense value to continual product improvement. The varied experience keeps him challenged and engaged, and he ...Read More