create BarCode using C#

create BarCode using C#

Barcodes contain information in the form of bars and offer efficiency and robustness in managing stock items. Enterprises organize their inventory by assigning a barcode to the items that store the product information. However, automating the barcode generation process using a barcode generator API gives a competitive edge to your business application. In this blog post, we will learn how to create barcodes using C# programmatically.

The following topics shall be covered in this article:

Barcode generator API installation

The installation process of this library is quite easy. You can download the DLL files or run the following commands in the NuGet package manager.

Install-Package Aspose.BarCode
Install-Package Aspose.PDF

Create BarCode using C#

This .NET library has exposed many methods to create and manipulate barcodes programmatically. You may go through the following steps and the code snippet:

  1. Create a PDF document with a new page by creating an instance of Aspose.Pdf.Document() class.
  2. Convert the barcode image to a PNG stream by instantiating an instance of BarcodeGenerator class.
  3. Invoke the Resolution property to set the barcode image resolution.
  4. Generate the barcode image by calling the GenerateBarCodeImage method and assign it to the Bitmap object.
  5. Initialize an object of MemoryStream class.
  6. Save barcode image to stream in PNG format.
  7. Create an object Rectangle where the image will be placed in the top/left corner.
  8. Add the image to the created PDF page by calling the AddImage method.
  9. Invoke the save method to save the PDF document.

You can see the output in the image below:

add barcode to PDF

How to read barcodes from a multi-page PDF document

In this section, we will write the code snippet that reads a barcode from a PDF file programmatically.

The steps are mentioned below:

  1. Instantiate an instance of the Document class to create a PDF document.
  2. Initialize the constructor of the PdfConverter class with a PDF file.
  3. Set the barcode optimization mode by setting the BarcodeOptimization property.
  4. You can set the resolution to the page by setting the Resolution property.
  5. Set all pages to render into images starting from 1st page.
  6. Call this DoConvert method to render selected pages to the images.
  7. Render current page to memory stream as a PNG image.
  8. Initializes a new instance of the BarCodeReader class with default values to detect barcodes.

Get a Free License

You may get a free temporary license to try the API without evaluation limitations.

Summing up

This brings us to the end of this blog post. We have gone through how to create barcodes using C# and save them into a PDF file programmatically. We also have gone through how to read a barcode from a PDF file using this barcode generator API. In addition, you may visit the documentation of this .NET library to know further features. Finally, is writing new blog posts on new topics. Therefore, please stay in touch for regular updates.

Ask a question

You can let us know about your questions or queries on our forum.


How do you create a barcode program?

You can install this barcode generator API to build a barcode generator programmatically. Moreover, you may find a list of methods here exposed by this library.

How can create a barcode in ASP NET MVC?

Check this section to learn how to create barcodes using C#. In fact, it is a matter of a few lines of source code in a .NET application to create and read barcodes programmatically.

See Also