跳至頁尾內容
使用IRONBARCODE

如何在C#中列印條碼

條碼作為唯一的識別碼,以視覺可掃描的格式儲存資訊。 它們廣泛應用於零售、物流、醫療保健和許多其他行業,用於庫存管理、產品標籤和資產追蹤等任務。 印刷條碼對於簡化操作、減少錯誤和提高資料管理的整體效率至關重要。

條碼在現代商業運營中扮演了至關重要的角色,便於高效追蹤、庫存管理和快速資料檢索。

在本文中,我們將探討如何使用IronBarcodeIronPDFIronPrint程式庫在C#中生成條碼並列印。

如何在C#中列印條碼

  1. 建立一個Visual Studio專案
  2. 安裝IronBarcodeIronPDFIronPrint程式庫
  3. 使用BarcodeWriter.CreateBarcode方法建立條碼圖像
  4. 使用SaveAs方法將生成的條碼保存為圖像
  5. 使用IronPDF的ImageToPdfConverter建立PDF文件
  6. 使用IronPrint調整PrinterSettings
  7. 使用IronPrint的Printer.Print方法進行列印

IronPrint - C#的列印程式庫

由Iron Software開發的IronPrint是一個為.NET設計的強大列印程式庫,提升了C#的列印能力。 讓我們探討使IronPrint脫穎而出的關鍵特性,這使其成為與IronBarcodeIronPDF配合使用時的有力助手。

IronPrint的關鍵特性

1. 簡單的定制化

IronPrint讓您掌控自如,能輕鬆定制各種列印方面。 從選擇紙張大小、調整DPI和設置邊距,到指定拷貝數量、列印機名稱,甚至灰階列印,IronPrint確保了靈活性。

2. 使用Printer類的多功能列印

引入的Printer類是一個改變遊戲規則的重要因素。 它提供了一套方法,讓您能夠輕鬆處理從圖像到PDF文件等不同型別文件的列印。 這種多功能性為您的列印選項新增了一層靈活性。

3. 跨平台支持

IronPrint具備多功能性,支援各種平台,如Windows、macOS、Android和iOS。 這種跨平台的相容性確保了在不同應用環境中的一致列印體驗。

IronPrint如何增強列印功能

1. 細緻的列印設置:IronPrint允許您進入列印設置,讓您能精確控制紙張大小、方向、DPI等因素。 這種定製化程度可確保您的列印輸出滿足您特定的需求。

2. Printer類的方便性:Printer類擴展了您的可能性,使得您不僅能列印文件,還可輕鬆處理多種文件型別的列印。 Printer類提供的方法可根據您應用程式的需求簡化列印工作流程。

3. 平台友好:IronPrint致力於支援多種平台,這使其成為開發者為不同環境應用程式工作時的理想選擇。 無論是在Windows上的桌面應用程式還是在iOS或Android上的移動應用中,IronPrint均能提供可靠的列印體驗。

必要條件

在深入建立一個用於列印條碼的C#控制台應用程式的步驟之前,請確保您擁有以下先決條件:

  1. Visual Studio: 安裝Microsoft Visual Studio,這是用于C#的強大整合開發環境。 您可以從其官方網站下載。
  2. IronBarcode程式庫: 此程式庫對於生成條碼圖像是必不可少的。 使用NuGet包管理控制台安裝它,或直接從官方IronBarcode NuGet 網站安裝。
  3. IronPDF程式庫: IronPDF將用於將生成的條碼資料矩陣和圖像轉換為PDF。
  4. IronPrint程式庫: 最後,安裝IronPrint程式庫以在C#應用程式中實現無縫列印。

在Visual Studio中建立C#控制台應用程式

按照以下步驟在Visual Studio中設置一個C#控制台應用程式:

  1. 打開Visual Studio並建立一個新的C#控制台應用程式。
  2. 按照以下方式配置專案,然後點擊"下一步":

!請怎麼在C#中列印條碼: 圖1 - 通過指定專案名稱、位置和解決方案名稱來配置您的新的C#控制檯應用。 然後單擊"下一步"按鈕。](/static-assets/barcode/blog/csharp-print-barcode-tutorial/csharp-print-barcode-tutorial-1.webp)

  1. 從附加資訊中選擇合適的.NET框架,然後點擊"建立"。

通過NuGet包管理器安裝必要程式庫

按照步驟安裝所需程式庫:

  1. 使用工具選單或在Visual Studio專案中使用解決方案總管打開NuGet包管理控制台或NuGet包管理器。
  2. 在NuGet的瀏覽標籤中搜尋程式庫並點擊安裝。
  3. 安裝條碼程式庫 - IronBarcode:
    • 使用NuGet包管理控制台,新增以下命令:
      Install-Package Barcode
      Install-Package Barcode
      SHELL
    • 使用管理NuGet解決方案的包:

How to Print a Barcode in C#: Figure 2 - Install IronBarcode using the Manage NuGet Package for Solutions by searching IronBarcode in the search bar of NuGet Package Manager, then select the project and click on the Install button.

  1. 安裝IronPDF PDF程式庫:
    • 使用NuGet包管理控制台:
      Install-Package BarCode
    • 使用管理NuGet解決方案的包:

How to Print a Barcode in C#: Figure 3 - Install IronPDF using the Manage NuGet Package for Solutions by searching IronPDF in the search bar of NuGet Package Manager, then select the project and click on the Install button.

  1. 安裝IronPrint列印程式庫:
    • 使用NuGet包管理控制台,輸入以下命令:
      Install-Package BarCode
    • 使用管理NuGet解決方案的包:

How to Print a Barcode in C#: Figure 4 - Install IronPrint using the Manage NuGet Package for Solutions by searching IronPrint in the search bar of NuGet Package Manager, then select the project and click on the Install button.

生成、轉換和列印條碼的步驟

讓我們逐步分解生成條碼、將生成的條碼圖像轉換為PDF並最終列印條碼的過程,使用IronBarcodeIronPDFIronPrint

C#條碼列印的逐步程式

步驟1:引用程式庫

在Program.cs文件的頂部,我們將包含要引用的所需程式庫。 這樣可以確保程式庫已安裝並可以使用。

// References to libraries
using IronPrint;                    // Library for printing functionalities
using IronPdf;                      // Library for PDF handling
using IronBarcode;                  // Library for barcode generation
using IronSoftware.Drawing;         // Library for image processing
// References to libraries
using IronPrint;                    // Library for printing functionalities
using IronPdf;                      // Library for PDF handling
using IronBarcode;                  // Library for barcode generation
using IronSoftware.Drawing;         // Library for image processing
' References to libraries
Imports IronPrint ' Library for printing functionalities
Imports IronPdf ' Library for PDF handling
Imports IronBarcode ' Library for barcode generation
Imports IronSoftware.Drawing ' Library for image processing
$vbLabelText   $csharpLabel

步驟2:使用IronBarcode生成條碼

在這裡,我們使用IronBarcode建立條碼並保存為圖像。 我們甚至可以生成QR碼型別的條碼。

// Code to generate a barcode
var myBarcode = BarcodeWriter.CreateBarcode("1212345", BarcodeWriterEncoding.EAN8);
myBarcode.SaveAsImage("assets/barcode.png");
// Code to generate a barcode
var myBarcode = BarcodeWriter.CreateBarcode("1212345", BarcodeWriterEncoding.EAN8);
myBarcode.SaveAsImage("assets/barcode.png");
' Code to generate a barcode
Dim myBarcode = BarcodeWriter.CreateBarcode("1212345", BarcodeWriterEncoding.EAN8)
myBarcode.SaveAsImage("assets/barcode.png")
$vbLabelText   $csharpLabel

在此源程式碼中:

  • 使用BarcodeWriter.CreateBarcode()方法生成帶有資料"1212345"的EAN-8條碼。
  • 將生成的條碼作為圖像文件保存在'assets/barcode.png'中。

這是輸出條碼圖像:

C#中如何列印條碼: 圖5 - 使用IronBarcode程式庫生成的條碼輸出圖像

步驟3:使用IronPDF將條碼圖像轉換為PDF

可以選擇使用IronPDF將條碼圖像轉換為PDF。 這保留了格式並提供了更多的大小和列印功能控住。

// Code to convert barcode image to PDF
var pdfDocument = IronPdf.PdfDocument.FromFile("assets/barcode.png");
pdfDocument.SaveAs("assets/composite.pdf");
// Code to convert barcode image to PDF
var pdfDocument = IronPdf.PdfDocument.FromFile("assets/barcode.png");
pdfDocument.SaveAs("assets/composite.pdf");
' Code to convert barcode image to PDF
Dim pdfDocument = IronPdf.PdfDocument.FromFile("assets/barcode.png")
pdfDocument.SaveAs("assets/composite.pdf")
$vbLabelText   $csharpLabel

barcode.png文件保存為PDF文件。 這是輸出:

C#中如何列印條碼: 圖6 - 使用IronPDF將條碼圖像轉換為PDF輸出文件: composite.pdf

步驟4:使用IronPrint調整PrinterSettings

使用IronPrint配置列印設置,例如紙張大小、邊距和列印機名稱。

// Code to adjust PrinterSettings using IronPrint
List<string> printerNames = Printer.GetPrinterNames();
PrintSettings printerSettings = new PrintSettings();
foreach(string printerName in printerNames)
{
    if(printerName.Equals("Microsoft Print to PDF"))
        printerSettings.PrinterName = printerName;
}
printerSettings.PaperSize = PaperSize.A4;
Margins margins = new Margins(30, 10);
printerSettings.PaperMargins = margins;
// Code to adjust PrinterSettings using IronPrint
List<string> printerNames = Printer.GetPrinterNames();
PrintSettings printerSettings = new PrintSettings();
foreach(string printerName in printerNames)
{
    if(printerName.Equals("Microsoft Print to PDF"))
        printerSettings.PrinterName = printerName;
}
printerSettings.PaperSize = PaperSize.A4;
Margins margins = new Margins(30, 10);
printerSettings.PaperMargins = margins;
' Code to adjust PrinterSettings using IronPrint
Dim printerNames As List(Of String) = Printer.GetPrinterNames()
Dim printerSettings As New PrintSettings()
For Each printerName As String In printerNames
	If printerName.Equals("Microsoft Print to PDF") Then
		printerSettings.PrinterName = printerName
	End If
Next printerName
printerSettings.PaperSize = PaperSize.A4
Dim margins As New Margins(30, 10)
printerSettings.PaperMargins = margins
$vbLabelText   $csharpLabel

上面的範例程式碼提供了PrinterSettings選項,這些選項可以在任何.NET條碼生成應用程式中給予更大的列印過程控制。

要獲得更多的列印選項,請造訪程式碼範例頁面。

步驟5:使用IronPrint的Printer.Print方法進行列印

最後,如以下程式碼所示,觸發使用IronPrint的條碼列印行為:

// Code to print
Printer.Print("assets/composite.pdf", printerSettings);
// Code to print
Printer.Print("assets/composite.pdf", printerSettings);
' Code to print
Printer.Print("assets/composite.pdf", printerSettings)
$vbLabelText   $csharpLabel

儘管IronPrint提供了從圖像格式直接列印的功能,這裡我們從PDF中列印。 這是使用IronPrint生成的輸出PDF文件:

C#中如何列印條碼: 圖7 - 使用IronPrint的輸出PDF文件: Barcode.pdf

IronPrint的優勢:全面的列印能力

IronPrint被專門設計為.NET應用程式的強大列印程式庫。 不同於IronPDF主要專注於處理PDF相關任務,C# 微軟列印主要是一種通用的列印機制,IronPrint提供了一組專門針對列印過程進行細粒度控制的類和方法。

使用IronPrint,開發人員可以利用:

  • 可定制的列印設置:IronPrint允許開發人員細緻控製列印過程中的多個方面,如紙張大小、方向、DPI、拷貝數量、列印機名稱、邊距和灰階列印。 在需要特定應用要求的重要列印精確度時,這種定制化程度是有幫助的。
  • 使用Printer類的多功能列印:IronPrint中引入的Printer類提供了一個完整的方法集來列印各種文件型別,包括圖像和PDF文件。 這種多功能性超越了標準的列印功能,並為處理不同文件格式提供了靈活性。
  • 非同步列印:IronPrint支持非同步函式,防止列印操作阻塞執行緒。 非同步列印提高了應用程式性能,確保即便在處理大規模列印任務時,也能提供順暢的使用者體驗。

這些優勢使IronPrint成為當開發者需要一個專業的、功能豐富的列印程式庫時的首選,提供了比更通用的列印解決方案更大的控制和定製選擇。

結論

總結來說,IronBarcode為在C#中生成條碼提供了一個輕鬆的解決方案,當與IronPrint結合使用時,條碼的列印過程變得極為高效。 通過遵循上述步驟並充分利用IronPrint提供的功能,開發人員可以輕鬆地將條碼生成和列印整合到他們的C#應用程式中。

請存取此文件頁面以獲取有關如何效率列印的更多資訊。

IronPrint提供免費試用來探索其完整功能和能力。 針對不同的需求有永久授權選項可用,起始價格為$999。 從這裡下載程式庫,並增強您的C#應用程式的列印功能。

常見問題

我如何在 C# 中生成條碼?

您可以在C#中使用IronBarcode的BarcodeWriter.CreateBarcode方法生成條碼,以建立適合各種應用的條碼圖像。

C#中列印條碼涉及哪些步驟?

要在C#中列印條碼,可以使用IronBarcode生成條碼圖像,使用IronPDF將其轉換為PDF,然後使用IronPrint處理列印,並自訂設置。

我如何在C#中安裝條碼列印所需的程式庫?

使用Visual Studio中的NuGet套件管理器安裝IronBarcode、IronPDF和IronPrint程式庫,以設置您的C#應用程式進行條碼列印。

使用IronPrint列印條碼有何優勢?

IronPrint提供了先進的列印功能,例如可自訂的列印設置、異步列印以及對多種文件型別的支援,非常適合於精確且靈活的條碼列印。

如何自訂C#中的條碼列印設置?

使用IronPrint的PrinterSettings類來自訂條碼列印的設置,例如紙張大小、DPI、方向和邊距。

我能否在不同的作業系統中使用C#程式庫列印條碼圖像?

可以,IronPrint 支援多個平台,包括Windows、macOS、Android和iOS,確保不論作業系統如何,皆能提供一致的列印體驗。

列印條碼時使用IronPDF有何好處?

IronPDF允許將條碼圖像轉換為PDF,提供更好的列印佈局控制並確保高品質列印。

IronPrint具有哪些專為C#列印的特殊功能?

IronPrint提供了精細的列印控制,具有可自訂的列印設置、多功能的文件處理和異步列印等功能,使其與通用程式庫區分開來。

IronPrint如何處理不同文件型別的列印?

IronPrint的Printer類可列印多種文件型別,包括圖像和PDF,靈活地管理不同的列印任務。

將條碼生成和列印整合到C#應用上的好處是什麼?

將條碼生成和列印整合到C#應用中能提高運營效率,減少錯誤,並簡化各行業的資料管理流程。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

Iron 支援團隊

我們線上24小時,每週5天。
聊天
電子郵件
給我打電話