跳過到頁腳內容
使用IRONBARCODE

如何在C#中打印條碼

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

條碼在現代商務運營中發揮著至關重要的作用,促進高效的追蹤、庫存管理和快速數據檢索。

In this article, we will explore how to generate barcodes and print them in C# using IronBarcode, IronPDF, and IronPrint libraries.

如何在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#的打印能力。 Let's explore the key features that make IronPrint stand out, making it a valuable companion when working alongside IronBarcode and IronPDF in a C# console application.

IronPrint的關鍵特徵

1. 易於定制

IronPrint讓您掌控自如,允許輕鬆定制各種打印方面。 從選擇紙張尺寸、調整DPI和設置邊距到指定副本數、打印機名稱甚至是灰階打印,IronPrint保證了靈活性。

2. 使用Printer Class進行多功能打印

Printer類的引入是一個改變遊戲規則的創新。 它提供了一組方法,用於輕鬆處理從圖像到PDF文件等不同文件類型的打印。 這種多功能性為您的打印選項增加了一層靈活性。

3. 跨平台支持

IronPrint功能強大,支持多個平台,如Windows、macOS、Android和iOS。 這種跨平台的兼容性確保在不同應用環境中提供一致的打印體驗。

IronPrint如何增強打印

1. 精細調整的打印設置: IronPrint允許您深入打印設置,讓您精確控制紙張尺寸、方向、DPI等元素。 這種定制化程度確保您的打印輸出符合您的特定要求。

2. Printer Class的便利性: Printer class擴展了您的可能性,使您能夠輕鬆打印各種文件類型,而不僅僅是文件。 Printer class提供的方法簡化了根據應用程序需求定制的打印工作流程。

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

必要條件

在深入了解創建C#控制台應用程式以打印條碼的步驟之前,請確保您具備以下必要條件:

  1. Visual Studio: 安裝Microsoft Visual Studio,一個功能強大的C#集成開發環境。 您可以從其官方網站下載。
  2. IronBarcode Library: 此庫對於生成條碼圖像至關重要。 Install it using the NuGet Package Manager Console or directly from the official IronBarcode NuGet website.
  3. IronPDF Library:將用於將生成的條碼數據矩陣和圖像轉換為PDF的IronPDF
  4. IronPrint Library: 最後,安裝IronPrint庫,以便在您的C#應用程序中實現無縫打印。

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

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

  1. 打開Visual Studio並創建一個新的C#控制台應用程式。
  2. 如下配置項目,然後點擊"下一步":

如何在C#中打印條碼: 圖1 - 通過指定項目名稱、位置和解決方案名稱配置新的C#控制台應用程式項目。 然後點擊下一步按鈕。

  1. 從附加信息中選擇合適的.NET Framework並點擊"創建"。

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

按照以下步驟安裝必要的庫:

  1. 使用工具菜單或Visual Studio項目中的解決方案資源管理器打開NuGet包管理器控制台或NuGet包管理器。
  2. 在NuGet的瀏覽選項卡中,搜索庫並點擊安裝。
  3. 安裝條碼庫 - IronBarcode:
    • 使用NuGet包管理器控制台,添加以下命令:
      Install-Package Barcode
      Install-Package Barcode
      SHELL
    • 使用解決方案的NuGet包管理器管理:

如何在C#中打印條碼: 圖2 - 使用NuGet包管理器解決方案中的管理NuGet包安裝IronBarcode,在NuGet包管理器的搜索欄中搜索IronBarcode,然後選擇項目並點擊安裝按鈕。

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

如何在C#中打印條碼: 圖3 - 使用NuGet包管理器解決方案中的管理NuGet包安裝IronPDF,在NuGet包管理器的搜索欄中搜索ironpdf,然後選擇項目並點擊安裝按鈕。

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

如何在C#中打印條碼: 圖4 - 使用NuGet包管理器解決方案中的管理NuGet包安裝IronPrint,在NuGet包管理器的搜索欄中搜索ironprint,然後選擇項目並點擊安裝按鈕。

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

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

逐步流程在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應用的強大打印庫。 與主要專注於處理PDF相關任務的IronPDF和C# Microsoft打印機制相比,IronPrint提供了量身定制的類和方法集,用於對打印過程進行細粒度控制。

使用IronPrint,開發者可以利用:

  • 可自定義的打印設置: IronPrint允許開發者精細控制打印過程的各個方面,例如紙張尺寸、方向、DPI、份數、打印機名稱、邊距和灰階打印。 這種定制化程度在精確打印對某些應用要求至關重要時非常有用。
  • 使用Printer Class的多功能打印: IronPrint中新引入的Printer類提供了一套完整的方法,用於打印多種文件類型,包括圖像和PDF文檔。 這種多用途超越了標準打印功能,提供靈活性以處理多種文件格式。
  • 異步打印: IronPrint支持異步函數,防止打印操作阻塞線程。 異步打印提升了應用性能,確保即使在處理大型打印任務時也能保持流暢的用戶體驗。

這些優勢使IronPrint成為開發者需要專業且功能豐富的打印庫時的首選,相比更一般化的打印解決方案提供了更大的控制和自定義選項。

結論

In conclusion, IronBarcode provides a seamless solution for generating barcodes in C#, and when combined with IronPrint, the process of printing these barcodes becomes highly efficient. 通過遵循所列步驟並利用IronPrint提供的特性,開發者可以輕鬆地將條碼生成和打印集成到他們的C#應用程式中。

有關如何高效打印的更多信息,請訪問這個 文檔頁面。

IronPrint提供免費試用以探索其完整功能和容量。 針對不同需求,提供永久許可證選項,價格從$799起。 從這裡下載庫,增強您的C#應用程式的打印功能。

常見問題解答

如何在 C# 中生成條碼?

您可以使用IronBarcode的BarcodeWriter.CreateBarcode方法在C#中創建適用於各種應用的條碼圖像。

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

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

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

使用Visual Studio中的NuGet Package Manager安裝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#應用中可以提高操作效率,降低錯誤,並簡化跨行業的數據管理過程。

Jordi Bardia
軟體工程師
Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。