如何在C#中將條碼作為PDF創建

如何在 C# 中將條碼匯出為 PDF

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

IronBarcode讓 C# 開發人員能夠透過三種方法將條碼匯出為 PDF:直接儲存到檔案、轉換為二進位資料或串流到記憶體——所有這些都只需簡單的一行操作即可完成。

快速入門:立即將條碼匯出為 PDF 文件

本範例展示了使用IronBarcode在.NET中將條碼匯出為 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

如何將條碼匯出為PDF檔案?

為什麼要將條碼直接儲存到 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 的功能,請造訪我們的入門文件

何時應該使用文件匯出而不是其他方法?

選擇文件匯出條件: 您需要永久儲存條碼。 產生報告或可列印文檔

  • 建立用於離線處理的批次文件
  • 與基於文件的系統集成

在以下情況下應考慮二進位資料或資料流:

  • 處理需要立即回應的 Web 應用程式
  • 儲存在資料庫中
  • 透過 API 發送數據,無需存取檔案系統
  • 在記憶體受限的環境中進行處理

如何將條碼匯出為 PDF 二進位資料?

為什麼要使用二進位資料而不是檔案?

二進位資料匯出非常適合需要在記憶體中操作 PDF 資料而無需建立臨時檔案的場景。 這種方法在 Web 應用程式、雲端環境以及處理資料庫時尤其有價值。 它消除了檔案 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

您也可以在匯出前自訂條碼外觀。 了解更多關於自訂條碼樣式以增強 PDF 匯出效果的資訊。

如何將二進位PDF資料傳送到API?

二進位 PDF 資料可以透過 REST API 輕鬆傳輸,非常適合微服務架構。 以下是一個透過HTTP請求傳送條碼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資料儲存在資料庫中 -電子郵件附件:無需建立臨時檔案即可將條碼附加到電子郵件中
-雲端儲存:直接上傳到 Azure Blob Storage 或 AWS S3 等服務 -記憶體處理:無需磁碟 I/O 即可連結多個操作

  • Web API 回應:直接在 HTTP 回應中傳回 PDF 數據

有關更多條碼產生技術和最佳實踐,請造訪我們的條碼產生指南(來自各種來源) 。 您也可以學習如何在現有 PDF 上新增條碼,以進行更進階的 PDF 操作。

如何將條碼匯出為 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

何時應該選擇流資料而不是二進位資料?

選擇串流媒體播放條件: -與其他需要流式輸入的庫集成 處理大型檔案時,如果無法將全部內容載入到記憶體中,則難以實現。

  • 在 Web 應用程式中實現串流回應
  • 與其他基於流的 API進行鍊式操作 -需要緩衝讀寫以優化效能

選擇二進位資料的情況:

  • 僅需在變數或資料庫中進行簡單存儲 快速序列化,無需複雜處理 -使用需要位元組數組的 API

有關更多條碼生成技術和最佳實踐,請訪問我們的綜合條碼教程。 您也可以學習如何在現有 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 資料。這對於需要動態提供 PDF 條碼而無需磁碟 I/O 操作的 Web 應用程式或 API 來說是完美的。

Hairil Hasyimi Bin Omar
軟體工程師
和所有优秀的工程师一样,Hairil 是個努力学习者。他正在细化自己的 C# 、Python 和 Java 知识,将这些知识應用于 Iron Software 各個团队成员以增加价值。Hairil 自马来西亚 Universiti Teknologi MARA 加入 Iron Software 团队,并以化学与工艺工程学士学位毕业。
準備好開始了嗎?
Nuget 下載 2,121,847 | 版本: 2026.3 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想要快速證明? PM > Install-Package BarCode
執行範例 看您的字串變成 BarCode。