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
- Create BarCode using C#
- How to read barcodes from a multi-page PDF document
Barcode generator API installation
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:
- Create a PDF document with a new page by creating an instance of Aspose.Pdf.Document() class.
- Convert the barcode image to a PNG stream by instantiating an instance of BarcodeGenerator class.
- Invoke the Resolution property to set the barcode image resolution.
- Generate the barcode image by calling the GenerateBarCodeImage method and assign it to the Bitmap object.
- Initialize an object of MemoryStream class.
- Save barcode image to stream in PNG format.
- Create an object Rectangle where the image will be placed in the top/left corner.
- Add the image to the created PDF page by calling the AddImage method.
- Invoke the save method to save the PDF document.
You can see the output in the image below:
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:
- Instantiate an instance of the Document class to create a PDF document.
- Initialize the constructor of the PdfConverter class with a PDF file.
- Set the barcode optimization mode by setting the BarcodeOptimization property.
- You can set the resolution to the page by setting the Resolution property.
- Set all pages to render into images starting from 1st page.
- Call this DoConvert method to render selected pages to the images.
- Render current page to memory stream as a PNG image.
- 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.
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, conholdate.com 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?
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.