How to Create Barcodes as PDFs in C#

如何在 C# 中以 PDF 格式导出 BarCode;

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronBarcode 使 C# 开发人员能够通过三种方法将条形码导出为 PDF:直接保存到文件、转换为二进制数据或流式传输到内存--所有这些都只需简单的单线操作。

快速入门:立即将条形码导出为 PDF 文件

此示例展示了如何在.NET中使用IronBarcode轻松将条形码导出为PDF。 只需一行代码即可获得PDF格式的条形码——非常适合于快速保存、流式传输或发送。

  1. 使用 NuGet 包管理器安装 https://www.nuget.org/packages/BarCode

    PM > Install-Package BarCode
  2. 复制并运行这段代码。

    var pdfBytes = IronBarCode.BarcodeWriter.CreateBarcode("FastPDF", IronBarCode.BarcodeWriterEncoding.Code128).ToPdfBinaryData();
  3. 部署到您的生产环境中进行测试

    通过免费试用立即在您的项目中开始使用IronBarcode

    arrow pointer

如何将 BarCode 导出为 PDF 文件?

为什么直接将 BarCode 保存为 PDF 文件?

当您需要生成物理文档、创建可打印标签或将条形码存档以便长期存储时,将条形码直接保存到 PDF 文件是最直接的方法。 这种方法尤其适用于库存管理系统、运输标签和文档生成工作流程,其中 PDF 格式可确保在不同平台和打印机上呈现一致的效果。

要将条形码保存为 PDF 文件,首先创建一个 GeneratedBarcode 对象,然后使用 BarcodeWriter.CreateBarcode 方法进行转换并保存到磁盘。 以下代码片段演示了其工作方法。

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

如需了解更多高级条形码创建选项,请查看我们关于从各种数据源创建条形码的综合指南。

有哪些文件路径选项?

IronBarcode 为保存 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"));
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"));
$vbLabelText   $csharpLabel

在本文中,我们将探讨如何使用 IronBarcode 将条形码导出到 PDF。 使用IronBarcode,条形码可以导出为文件、二进制数据或内存流。 如需全面了解 IronBarcode 的功能,请访问我们的 入门文档

何时应使用文件导出与其他方法?

选择文件导出时:

  • 您需要永久存储 BarCode
  • 生成报告或可打印文档
  • 创建用于离线处理的批处理文件
  • 与基于文件的系统集成

考虑二进制数据或流时:

  • 处理需要立即响应的网络应用程序
  • 数据库存储
  • 通过 API 发送,无需访问文件系统
  • 在内存受限的环境中进行处理

如何将 BarCode 导出为 PDF 二进制数据?

为什么使用二进制数据而不是文件?

二进制数据导出非常适合需要在内存中操作 PDF 数据而不创建临时文件的情况。 这种方法在网络应用程序、云环境和使用数据库时尤为重要。 它消除了文件 I/O 操作,通过将数据保存在内存中提高了性能和安全性。

要导出为 PDF 二进制数据,请生成条形码,然后调用 ToPdfBinaryData() 方法。 这将 PDF 二进制数据输出为 byte[] 数组。 以下代码片段演示了其工作方法。

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

您还可以在导出前自定义 BarCode 的外观。 了解有关自定义条形码样式以增强 PDF 导出的更多信息。

如何将二进制 PDF 数据发送到 API?

二进制 PDF 数据可以通过 REST API 轻松传输,因此非常适合微服务架构。 下面是一个通过 HTTP 请求发送 BarCode PDF 数据的实用示例:

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
    }
}
$vbLabelText   $csharpLabel

二进制导出的常见用例有哪些?

二进制导出常用于

  • 数据库存储:将 PDF 条形码作为 BLOB 数据存储在数据库中
  • 电子邮件附件:将 BarCode 附加到电子邮件,而无需创建临时文件
  • 云存储:直接上传至 Azure Blob Storage 或 AWS S3 等服务
  • 内存处理:无需磁盘 I/O 即可连锁执行多个操作
  • Web API 响应:在 HTTP 响应中直接返回 PDF 数据

有关其他条形码生成技术和最佳实践,请访问我们的从各种来源创建条形码指南。 您还可以了解在现有 PDF 上加盖条形码,以实现更高级的 PDF 操作场景。

如何将 BarCode 导出为 PDF 流?

为什么使用流导出 PDF?

流为处理 PDF 数据提供了最灵活的方法,尤其是在与其他 .NET 库集成或需要对数据流进行细粒度控制时。 流对于内存效率至关重要的大规模操作特别有用,因为它们允许缓冲读写。

要导出为内存流,请生成条形码,然后调用 ToPdfStream() 方法。 此方法返回一个 System.IO.Stream 对象。 以下代码片段演示了其工作方法。

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

有关高级流操作和其他导出格式,请浏览我们关于以流形式导出条形码的详细指南。

如何正确处理内存流?

正确的流处理对于防止内存泄漏和确保资源的有效使用至关重要。 下面是一个展示最佳实践的综合示例:

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

何时应选择流而不是二进制数据?

在以下情况下选择流:

  • 与其他需要流输入的库集成
  • 处理大文件,将整个内容加载到内存中不可行
  • <在网络应用程序中实施流式响应
  • 与其他基于流的 API 进行链式操作
  • 需要缓冲读/写以优化性能

选择二进制数据时:

  • 要求在变量或数据库中进行简单存储
  • 快速序列化,无需复杂处理
  • 与期望使用字节数组的 API 协同工作

有关其他条码生成技术和最佳实践,请访问我们全面的 BarCode 教程。 您还可以了解在现有 PDF 上加盖条形码,以实现更高级的 PDF 操作场景。

常见问题解答

如何在 C# 中将 BarCode 导出到 PDF?

IronBarcode 提供了三种将条形码导出为 PDF 的方法:使用 SaveAsPdf() 直接保存到文件,使用 ToPdfBinaryData() 转换为二进制数据,或者流式传输到内存。最简单的方法是使用 BarcodeWriter.CreateBarcode(),然后使用您喜欢的导出方法。

只需一行代码就能生成 PDF BarCode 吗?

是的,IronBarcode 支持单行 PDF 条码生成。只需使用: var pdfBytes = IronBarCode.BarcodeWriter.CreateBarcode("FastPDF", IronBarCode.BarcodeWriterEncoding.Code128).ToPdfBinaryData(); 即可立即创建一个PDF-ready条码。

将 BarCode 保存为 PDF 文件时,有哪些文件路径选项?

IronBarcode 支持灵活的文件路径选项,包括绝对路径、相对路径和环境变量。您可以使用 SaveAsPdf(),如 "barcode.pdf "用于当前目录,"C:\BarcodeExports\product_barcode.pdf "用于绝对路径,或 Path.Combine() 与 Environment.SpecialFolder 用于系统路径。

何时应将 BarCode 直接保存到 PDF 文件而不是使用流?

使用 IronBarcode 的 SaveAsPdf() 方法直接保存为 PDF 文件非常适合生成物理文档、创建可打印标签或将条形码归档以便长期保存。这种方法尤其适用于库存管理系统、运输标签和需要一致 PDF 渲染的文档工作流程。

如何将 BarCode 数据转换为 PDF 二进制数据?

使用 IronBarcode 的 ToPdfBinaryData() 方法将条形码转换为二进制数据。首先使用 BarcodeWriter.CreateBarcode() 创建一个 GeneratedBarcode 对象,然后调用 ToPdfBinaryData() 将 PDF 转换为适合数据库存储或网络传输的字节数组。

我可以直接将 BarCode 流式传输到内存而不是保存到磁盘吗?

是的,IronBarcode 支持将条码流传输至内存,使您无需创建物理文件即可处理 PDF 数据。这非常适合网络应用程序或应用程序接口,您需要在不进行磁盘 I/O 操作的情况下动态提供 PDF 条形码。

Hairil Hasyimi Bin Omar
软件工程师
如所有伟大的工程师一般,Hairil 是个热心的学习者。他正在提高对 C#、Python 和 Java 的知识,并利用这些知识为 Iron Software 团队成员增值。Hairil 从马来西亚的玛拉工业大学加入 Iron Software 团队,获得化学与工艺工程学士学位。
准备开始了吗?
Nuget 下载 2,121,847 | 版本: 2026.3 刚刚发布
Still Scrolling Icon

还在滚动吗?

想快速获得证据? PM > Install-Package BarCode
运行示例 观看您的字符串变成 BarCode。