How to Create Barcodes as PDFs in C#

How to Export Barcodes as PDF in C#

IronBarcode enables C# developers to export barcodes as PDFs through three methods: saving directly to file, converting to binary data, or streaming to memory - all with simple one-line operations.

Quickstart: Export a Barcode to PDF Instantly

This example shows how simple it is to export a barcode as PDF in .NET using IronBarcode. With one line you get a PDF-ready barcode – ideal for saving, streaming, or sending quickly.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronBarcode with NuGet Package Manager

    PM > Install-Package BarCode

  2. Copy and run this code snippet.

    var pdfBytes = IronBarCode.BarcodeWriter.CreateBarcode("FastPDF", IronBarCode.BarcodeWriterEncoding.Code128).ToPdfBinaryData();
  3. Deploy to test on your live environment

    Start using IronBarcode in your project today with a free trial
    arrow pointer

How Do I Export Barcodes as a PDF File?

Why Save Barcodes Directly to PDF Files?

Saving barcodes directly to PDF files is the most straightforward approach when you need to generate physical documents, create printable labels, or archive barcodes for long-term storage. This method is particularly useful for inventory management systems, shipping labels, and document generation workflows where the PDF format ensures consistent rendering across different platforms and printers.

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 to disk. The following code snippet demonstrates how this works.

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

For more advanced barcode creation options, check out our comprehensive guide on creating barcodes from various data sources.

What File Path Options Are Available?

IronBarcode provides flexible file path options for saving PDF files. You can specify absolute paths, relative paths, or use environment variables. Here's a more detailed example showing different path options:

using IronBarCode;
using System;
using System.IO;

// Create a barcode
GeneratedBarcode barcode = BarcodeWriter.CreateBarcode("PRODUCT-12345", BarcodeEncoding.Code128);

// Save to current directory
barcode.SaveAsPdf("barcode.pdf");

// Save to absolute path
barcode.SaveAsPdf(@"C:\BarcodeExports\product_barcode.pdf");

// Save to relative path
barcode.SaveAsPdf(@"..\..\exports\barcode.pdf");

// Save using environment path
string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
barcode.SaveAsPdf(Path.Combine(documentsPath, "barcode.pdf"));
using IronBarCode;
using System;
using System.IO;

// Create a barcode
GeneratedBarcode barcode = BarcodeWriter.CreateBarcode("PRODUCT-12345", BarcodeEncoding.Code128);

// Save to current directory
barcode.SaveAsPdf("barcode.pdf");

// Save to absolute path
barcode.SaveAsPdf(@"C:\BarcodeExports\product_barcode.pdf");

// Save to relative path
barcode.SaveAsPdf(@"..\..\exports\barcode.pdf");

// Save using environment path
string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
barcode.SaveAsPdf(Path.Combine(documentsPath, "barcode.pdf"));
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

In this article, we explore how to use IronBarcode to export barcodes to PDF. With IronBarcode, barcodes can be exported as either a file, binary data, or memory stream. For a complete overview of IronBarcode's capabilities, visit our getting started documentation.

When Should I Use File Export vs Other Methods?

Choose file export when:

  • You need permanent storage of barcodes
  • Generating reports or printable documents
  • Creating batch files for offline processing
  • Integrating with file-based systems

Consider binary data or streams when:

  • Working with web applications that need immediate response
  • Storing in databases
  • Sending through APIs without file system access
  • Processing in memory-constrained environments

How Do I Export Barcodes as PDF Binary Data?

Why Use Binary Data Instead of Files?

Binary data export is ideal for scenarios where you need to manipulate PDF data in memory without creating temporary files. This approach is particularly valuable in web applications, cloud environments, and when working with databases. It eliminates file I/O operations, improving performance and security by keeping data in memory.

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;

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

You can also customize the barcode appearance before exporting. Learn more about customizing barcode styles to enhance your PDF exports.

How Can I Send Binary PDF Data to APIs?

Binary PDF data can be easily transmitted through REST APIs, making it perfect for microservices architectures. Here's a practical example of sending barcode PDF data through an HTTP request:

using IronBarCode;
using System.Net.Http;
using System.Threading.Tasks;

public async Task SendBarcodeToAPI()
{
    // Generate barcode and get binary data
    GeneratedBarcode barcode = BarcodeWriter.CreateBarcode("API-DATA-123", BarcodeEncoding.QRCode);
    byte[] pdfData = barcode.ToPdfBinaryData();

    // Send via HTTP POST
    using (HttpClient client = new HttpClient())
    {
        ByteArrayContent content = new ByteArrayContent(pdfData);
        content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");

        HttpResponseMessage response = await client.PostAsync("https://api.example.com/barcode", content);
        // Handle response
    }
}
using IronBarCode;
using System.Net.Http;
using System.Threading.Tasks;

public async Task SendBarcodeToAPI()
{
    // Generate barcode and get binary data
    GeneratedBarcode barcode = BarcodeWriter.CreateBarcode("API-DATA-123", BarcodeEncoding.QRCode);
    byte[] pdfData = barcode.ToPdfBinaryData();

    // Send via HTTP POST
    using (HttpClient client = new HttpClient())
    {
        ByteArrayContent content = new ByteArrayContent(pdfData);
        content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");

        HttpResponseMessage response = await client.PostAsync("https://api.example.com/barcode", content);
        // Handle response
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

What Are Common Use Cases for Binary Export?

Binary export is commonly used in:

  • Database Storage: Store PDF barcodes as BLOB data in databases
  • Email Attachments: Attach barcodes to emails without creating temporary files
  • Cloud Storage: Upload directly to services like Azure Blob Storage or AWS S3
  • In-Memory Processing: Chain multiple operations without disk I/O
  • Web API Responses: Return PDF data directly in HTTP responses

For additional barcode generation techniques and best practices, visit our guide on creating barcodes from various sources. You can also learn about stamping barcodes on existing PDFs for more advanced PDF manipulation scenarios.

How Do I Export Barcodes as a PDF Stream?

Why Use Streams for PDF Export?

Streams provide the most flexible approach for handling PDF data, especially when integrating with other .NET libraries or when you need fine-grained control over data flow. Streams are particularly useful for large-scale operations where memory efficiency is critical, as they allow for buffered reading and writing.

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

For advanced stream operations and other export formats, explore our detailed guide on exporting barcodes as streams.

How Do I Handle Memory Streams Properly?

Proper stream handling is crucial for preventing memory leaks and ensuring efficient resource usage. Here's a comprehensive example showing best practices:

using IronBarCode;
using System.IO;

public void ProcessBarcodeStream()
{
    // Always use using statements for proper disposal
    using (Stream pdfStream = BarcodeWriter.CreateBarcode("STREAM-123", BarcodeEncoding.Code39).ToPdfStream())
    {
        // Example 1: Copy to file
        using (FileStream fileStream = File.Create("output.pdf"))
        {
            pdfStream.CopyTo(fileStream);
        }

        // Reset stream position for reuse
        pdfStream.Position = 0;

        // Example 2: Read into buffer
        byte[] buffer = new byte[pdfStream.Length];
        pdfStream.Read(buffer, 0, buffer.Length);

        // Example 3: Process with another library
        // ProcessPdfStream(pdfStream);
    }
}
using IronBarCode;
using System.IO;

public void ProcessBarcodeStream()
{
    // Always use using statements for proper disposal
    using (Stream pdfStream = BarcodeWriter.CreateBarcode("STREAM-123", BarcodeEncoding.Code39).ToPdfStream())
    {
        // Example 1: Copy to file
        using (FileStream fileStream = File.Create("output.pdf"))
        {
            pdfStream.CopyTo(fileStream);
        }

        // Reset stream position for reuse
        pdfStream.Position = 0;

        // Example 2: Read into buffer
        byte[] buffer = new byte[pdfStream.Length];
        pdfStream.Read(buffer, 0, buffer.Length);

        // Example 3: Process with another library
        // ProcessPdfStream(pdfStream);
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

When Should I Choose Streams Over Binary Data?

Choose streams when:

  • Integrating with other libraries that expect stream inputs
  • Processing large files where loading entire content into memory isn't feasible
  • Implementing streaming responses in web applications
  • Chaining operations with other stream-based APIs
  • Need buffered reading/writing for performance optimization

Choose binary data when:

  • Simple storage in variables or databases is required
  • Quick serialization without complex processing
  • Working with APIs that expect byte arrays

For additional barcode generation techniques and best practices, visit our comprehensive barcode tutorials. You can also learn about stamping barcodes on existing PDFs for more advanced PDF manipulation scenarios.

Frequently Asked Questions

How do I export a barcode to PDF in C#?

IronBarcode provides three methods to export barcodes as PDFs: saving directly to file using SaveAsPdf(), converting to binary data with ToPdfBinaryData(), or streaming to memory. The simplest approach is using BarcodeWriter.CreateBarcode() followed by your preferred export method.

Can I generate a PDF barcode in just one line of code?

Yes, IronBarcode enables one-line PDF barcode generation. Simply use: var pdfBytes = IronBarCode.BarcodeWriter.CreateBarcode("FastPDF", IronBarCode.BarcodeWriterEncoding.Code128).ToPdfBinaryData(); to instantly create a PDF-ready barcode.

What file path options are available when saving barcodes as PDF files?

IronBarcode supports flexible file path options including absolute paths, relative paths, and environment variables. You can use SaveAsPdf() with paths like "barcode.pdf" for current directory, "C:\BarcodeExports\product_barcode.pdf" for absolute paths, or Path.Combine() with Environment.SpecialFolder for system paths.

When should I save barcodes directly to PDF files instead of using streams?

Saving directly to PDF files with IronBarcode's SaveAsPdf() method is ideal for generating physical documents, creating printable labels, or archiving barcodes for long-term storage. This approach is particularly useful for inventory management systems, shipping labels, and document workflows where consistent PDF rendering is required.

How do I convert barcode data to PDF binary data?

Use IronBarcode's ToPdfBinaryData() method to convert barcodes to binary data. First create a GeneratedBarcode object with BarcodeWriter.CreateBarcode(), then call ToPdfBinaryData() to get the PDF as a byte array suitable for database storage or network transmission.

Can I stream barcodes directly to memory instead of saving to disk?

Yes, IronBarcode supports streaming barcodes to memory, allowing you to work with PDF data without creating physical files. This is perfect for web applications or APIs where you need to serve PDF barcodes dynamically without disk I/O operations.

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 ...
Read More
Ready to Get Started?
Nuget Downloads 2,002,059 | Version: 2025.12 just released