How to Export Barcodes as PDF
In this article, we'll explore how we can use IronBarcode to export barcodes to PDF. With IronBarcode, barcodes can be exported as either a file, binary data, or memory stream.
How to Export Barcodes as PDF in C#
- Download C# library to Export Barcodes as PDFs in C#
- Export Barcodes as PDF File
- Export Barcodes as PDF Binary Data
- Export Barcodes as PDF Stream
Start using IronBarcode in your project today with a free trial.
Export Barcodes as PDF File
To save a barcode as a PDF file, first create a GeneratedBarcode
object with BarcodeWriter.CreateBarcode
and then use the SaveAsPdf()
method to convert and save a file to disk. The following code snippet demonstrates how this works.
:path=/static-assets/barcode/content-code-examples/how-to/ExportBarcodeAsPdfFile.cs
using IronBarCode;
// Generate a DataMatrix barcode from the provided URL.
// BarcodeWriter.CreateBarcode is used to create the barcode, specifying the content and the type of barcode encoding.
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.DataMatrix);
// Save the generated barcode in PDF format.
// The SaveAsPdf method takes a file path as an argument and saves the barcode into that file.
myBarcode.SaveAsPdf("myBarcode.pdf");
Imports IronBarCode
' Generate a DataMatrix barcode from the provided URL.
' BarcodeWriter.CreateBarcode is used to create the barcode, specifying the content and the type of barcode encoding.
Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.DataMatrix)
' Save the generated barcode in PDF format.
' The SaveAsPdf method takes a file path as an argument and saves the barcode into that file.
myBarcode.SaveAsPdf("myBarcode.pdf")
Export Barcodes as PDF Binary Data
To export as PDF binary data, generate a barcode and then call the ToPdfBinaryData()
method. This outputs the PDF binary data as a byte[]
array. The following code snippet demonstrates how this works.
:path=/static-assets/barcode/content-code-examples/how-to/ExportBarcodeAsPdfBinaryData.cs
using IronBarCode;
/// <summary>
/// A class responsible for generating DataMatrix barcodes and converting them into PDF format.
/// </summary>
class BarcodeGenerator
{
/// <summary>
/// Generates a DataMatrix barcode from a given URL and returns it as a PDF byte array.
/// </summary>
/// <param name="url">The URL to encode in the barcode.</param>
/// <returns>A byte array representing the PDF with the encoded barcode.</returns>
public static byte[] GenerateBarcodePdf(string url)
{
// Create a DataMatrix barcode using the specified URL
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode(url, BarcodeEncoding.DataMatrix);
// Convert the generated barcode to a PDF format and obtain the binary data
byte[] myBarcodeBytes = myBarcode.ToPdfBinaryData();
// Return the byte array of the PDF
return myBarcodeBytes;
}
/// <summary>
/// Entry point of the program.
/// </summary>
/// <param name="args">Command-line arguments.</param>
public static void Main(string[] args)
{
// Define the URL to be encoded in the barcode
string url = "https://ironsoftware.com/csharp/barcode/";
// Generate the barcode as a PDF byte array using the method
byte[] barcodePdf = GenerateBarcodePdf(url);
// Output the length of the generated PDF byte array for verification
Console.WriteLine("Barcode PDF generated with length: " + barcodePdf.Length);
}
}
Imports IronBarCode
''' <summary>
''' A class responsible for generating DataMatrix barcodes and converting them into PDF format.
''' </summary>
Friend Class BarcodeGenerator
''' <summary>
''' Generates a DataMatrix barcode from a given URL and returns it as a PDF byte array.
''' </summary>
''' <param name="url">The URL to encode in the barcode.</param>
''' <returns>A byte array representing the PDF with the encoded barcode.</returns>
Public Shared Function GenerateBarcodePdf(ByVal url As String) As Byte()
' Create a DataMatrix barcode using the specified URL
Dim myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode(url, BarcodeEncoding.DataMatrix)
' Convert the generated barcode to a PDF format and obtain the binary data
Dim myBarcodeBytes() As Byte = myBarcode.ToPdfBinaryData()
' Return the byte array of the PDF
Return myBarcodeBytes
End Function
''' <summary>
''' Entry point of the program.
''' </summary>
''' <param name="args">Command-line arguments.</param>
Public Shared Sub Main(ByVal args() As String)
' Define the URL to be encoded in the barcode
Dim url As String = "https://ironsoftware.com/csharp/barcode/"
' Generate the barcode as a PDF byte array using the method
Dim barcodePdf() As Byte = GenerateBarcodePdf(url)
' Output the length of the generated PDF byte array for verification
Console.WriteLine("Barcode PDF generated with length: " & barcodePdf.Length)
End Sub
End Class
Export Barcodes as PDF Stream
To export as a memory stream, generate a barcode and then call the ToPdfStream()
method. This method returns a System.IO.Stream
object. The following code snippet demonstrates how this works.
:path=/static-assets/barcode/content-code-examples/how-to/ExportBarcodeAsPdfStream.cs
// Importing the necessary namespaces for barcode creation and file handling
using IronBarCode; // For creating and handling barcodes
using System.IO; // For working with streams
namespace BarcodeExample
{
class Program
{
static void Main(string[] args)
{
// Generating a DataMatrix barcode with the specified URL
/*
BarcodeEncoding.DataMatrix specifies the type of barcode we want to create.
This line uses BarcodeWriter to create a new barcode image with the encoded data.
*/
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.DataMatrix);
// Converting the generated barcode to a PDF stream
/*
The ToPdfStream method is used to convert the barcode image into a stream of a PDF document.
Useful for storing the barcode as a PDF file.
*/
Stream myBarcodeStream = myBarcode.ToPdfStream();
// To demonstrate, we're writing the barcode stream to a file
/*
This block writes the PDF stream to a file named "BarcodeOutput.pdf" on disk.
It uses a FileStream to open or create the file and then copies the contents of the barcode stream into it.
The using statement ensures that the file stream is closed and disposed after use, releasing system resources.
Note: Ensure that your application has permission to write to the desired directory.
*/
using (FileStream fileStream = new FileStream("BarcodeOutput.pdf", FileMode.Create, FileAccess.Write))
{
myBarcodeStream.CopyTo(fileStream);
}
// Clean up the stream after use
/*
It's important to dispose of streams to free up resources.
Here, we explicitly call Dispose on myBarcodeStream to ensure it is properly cleaned up.
*/
myBarcodeStream.Dispose();
}
}
}
' Importing the necessary namespaces for barcode creation and file handling
Imports IronBarCode ' For creating and handling barcodes
Imports System.IO ' For working with streams
Namespace BarcodeExample
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Generating a DataMatrix barcode with the specified URL
'
' BarcodeEncoding.DataMatrix specifies the type of barcode we want to create.
' This line uses BarcodeWriter to create a new barcode image with the encoded data.
'
Dim myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.DataMatrix)
' Converting the generated barcode to a PDF stream
'
' The ToPdfStream method is used to convert the barcode image into a stream of a PDF document.
' Useful for storing the barcode as a PDF file.
'
Dim myBarcodeStream As Stream = myBarcode.ToPdfStream()
' To demonstrate, we're writing the barcode stream to a file
'
' This block writes the PDF stream to a file named "BarcodeOutput.pdf" on disk.
' It uses a FileStream to open or create the file and then copies the contents of the barcode stream into it.
' The using statement ensures that the file stream is closed and disposed after use, releasing system resources.
'
' Note: Ensure that your application has permission to write to the desired directory.
'
Using fileStream As New FileStream("BarcodeOutput.pdf", FileMode.Create, FileAccess.Write)
myBarcodeStream.CopyTo(fileStream)
End Using
' Clean up the stream after use
'
' It's important to dispose of streams to free up resources.
' Here, we explicitly call Dispose on myBarcodeStream to ensure it is properly cleaned up.
'
myBarcodeStream.Dispose()
End Sub
End Class
End Namespace
Frequently Asked Questions
What is IronBarcode?
IronBarcode is a powerful .NET Barcode Library that allows you to create, read, and export barcodes in various formats, including PDF.
How can I export barcodes as a PDF file using IronBarcode?
To export barcodes as a PDF file, create a GeneratedBarcode object with BarcodeWriter.CreateBarcode and use the SaveAsPdf() method to save it to disk.
How do I export barcodes as PDF binary data?
To export barcodes as PDF binary data, generate a barcode and then use the ToPdfBinaryData() method, which outputs the PDF binary data as a byte[] array.
Can I export barcodes as a PDF stream?
Yes, you can export barcodes as a PDF stream by generating a barcode and calling the ToPdfStream() method, which returns a System.IO.Stream object.
What are the steps to download the library needed to export barcodes as PDFs in C#?
To download the library, visit the NuGet package page for the BarCode library and follow the instructions to install it in your C# project.
What code is needed to save a barcode as a PDF file?
Use the following code snippet: ```csharp using IronBarCode; GeneratedBarcode barcode = BarcodeWriter.CreateBarcode("Example", BarcodeEncoding.Code128); barcode.SaveAsPdf("ExampleBarcode.pdf"); Console.WriteLine("Barcode saved as PDF file."); ```
How do I convert a barcode to PDF binary data?
To convert a barcode to PDF binary data, use the following code snippet: ```csharp using IronBarCode; GeneratedBarcode barcode = BarcodeWriter.CreateBarcode("Example", BarcodeEncoding.Code128); byte[] pdfBinaryData = barcode.ToPdfBinaryData(); Console.WriteLine("Barcode PDF binary data generated."); ```
What method should be used to convert a barcode to a PDF stream?
Use the ToPdfStream() method to convert a barcode to a PDF stream, which returns a System.IO.Stream object.
Is it possible to work with the PDF stream to save it as a file or send it over a network?
Yes, once you have the PDF stream from the ToPdfStream() method, you can work with it as you would with any System.IO.Stream object, including saving it to a file or sending it over a network.