How to Export Barcodes as PDF

by Hairil Hasyimi Bin Omar



C# NuGet Library for

Install with NuGet

Install-Package BarCode
or
Java PDF JAR

Download DLL

Download DLL

Manually install into your project

C# NuGet Library for

Install with NuGet

Install-Package BarCode
or
Java PDF JAR

Download DLL

Download DLL

Manually install into your project

Start using IronPDF in your project today with a free trial.

First Step:
green arrow pointer

Check out IronBarcode on Nuget for quick installation and deployment. With over 8 million downloads, it's transforming with C#.

C# NuGet Library for nuget.org/packages/BarCode/
Install-Package BarCode

Consider installing the IronBarcode DLL directly. Download and manually install it for your project or GAC form: IronBarCode.zip

Manually install into your project

Download DLL

One of the most popular feature in IronBarcode among users is the ability to export GeneratedBarcode as PDF. So in this context, IronBarcode does not only offer options to export the barcode as PDF files, but also as PDF binary data or as PDF stream. This is very important as this gives flexibility for users to use the PDF generated from GeneratedBarcode as intermediary output to be used further inside the program rather than as a the final output that needs to be saved inside the disk. Now let us explore the options available in this context.

Export Barcodes as PDF File

The first functionality in this context is, well, of course is to save the GeneratedBarcode as a PDF file. This is considered as a final product that comes out of IronBarcode, where the output needs to be saved in a disk. To achieve this, we call SaveAsPdf() method to GeneratedBarcode object to save it as a PDF file. Let us see how we implement this in the code snippet below:

:path=/static-assets/barcode/content-code-examples/how-to/ExportBarcodeAsPdfFile.cs
using IronBarCode;

GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.DataMatrix);
myBarcode.SaveAsPdf("myBarcode.pdf");
Imports IronBarCode

Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.DataMatrix)
myBarcode.SaveAsPdf("myBarcode.pdf")
VB   C#

In the code snippet above, we started by generating our barcode using the CreateBarcode() method with the barcode value and the barcode encoding as the arguments for the method. The resulting GeneratedBarcode object is then attached with SaveAsPdf() with a string of file name/file path as its argument. This will convert the GeneratedBarcode as a PDF file with the name given in the method argument.

Export Barcodes as PDF Binary Data

IronBarcode also able to output a PDF from GeneratedBarcode in terms of binary data. This can be done effortlessly by simply calling ToPdfBinaryData() to the GeneratedBarcode object and store it in a byte array type variable to be used further inside a program. This method returns 1 page PDF document containing the barcode as binary data. Code snippet below demonstrates

:path=/static-assets/barcode/content-code-examples/how-to/ExportBarcodeAsPdfBinaryData.cs
using IronBarCode;

GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.DataMatrix);
byte[] myBarcodeByte = myBarcode.ToPdfBinaryData();
Imports IronBarCode

Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.DataMatrix)
Private myBarcodeByte() As Byte = myBarcode.ToPdfBinaryData()
VB   C#

From the code snippet above, we simply call ToPdfBinaryData() method to the GeneratedBarcode object and assign it to a variable with the type of byte array. This variable can later be used further in the program. Optionally, users can call Console.WriteLine(myBarcodeByte.GetType()) to ensure the type of output from running this method to be a byte array.

Export Barcodes as PDF Stream

Apart from PDF binary data, IronBarcode is also able to export GeneratedBarcode output a PDF in type of Stream. This is an alternative to PDF binary data with the same purpose, which is to be used further in the program. Simply callToPdfStream() method and attach it to the GeneratedBarcode and assign it to a Stream variable. This method returns a binary System.IO.Stream containing a 1 page PDF document containing the barcode as binary data. Lets look at the code snippet below

:path=/static-assets/barcode/content-code-examples/how-to/ExportBarcodeAsPdfStream.cs
using IronBarCode;
using System.IO;

GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.DataMatrix);
Stream myBarcodeStream = myBarcode.ToPdfStream();
Imports IronBarCode
Imports System.IO

Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.DataMatrix)
Private myBarcodeStream As Stream = myBarcode.ToPdfStream()
VB   C#

In the code snippet above, we can see that to obtain a PDF stream from the GeneratedBarcode object, we simply attach the ToPdfStream() method to the object and assign it to a variable of System.IO.Stream type, which can later be used further inside a program.

In a nutshell, IronBarcode is an ideal API for those users who wished to create and export barcode image as a PDF, be it as a final product of PDF files or as an intermediary output to be used further inside a program as a PDF binary data or PDF stream.

Hairil Hasyimi Bin Omar

Software Engineer

Like all great engineers, Hairil is an avid learner. He’s refining his knowledge of C#, Python, and Java, using that knowledge to add value to team members across Iron Software. Hairil joined the Iron Software team from Universiti Teknologi MARA in Malaysia, where he graduated with a Bachelor's degree in Chemical and Process Engineering.