跳至页脚内容
USING IRONBARCODE

How to Print a Barcode in 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类的多功能打印

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库: 这一库对于生成条码图像至关重要。 Install it using the NuGet Package Manager Console or directly from the official IronBarcode NuGet website.
  3. IronPDF库: IronPDF将用于将生成的条码数据矩阵和图像转换为PDF。
  4. IronPrint库: 最后,安装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”来安装IronBarcode,然后选择项目并点击安装按钮。

  1. 安装IronPDF PDF库:
    • 使用NuGet包管理器控制台:
      Install-Package BarCode
    • 使用为解决方案管理NuGet包:

如何在C#中打印条码: 图3 - 使用为解决方案管理NuGet包通过在NuGet包管理器的搜索栏中搜索“ironpdf”来安装IronPDF,然后选择项目并点击安装按钮。

  1. 安装IronPrint 打印库:
    • 使用NuGet包管理器控制台,输入以下命令:
      Install-Package BarCode
    • 使用为解决方案管理NuGet包:

如何在C#中打印条码: 图4 - 使用为解决方案管理NuGet包通过在NuGet包管理器的搜索栏中搜索“ironprint”来安装IronPrint,然后选择项目并点击安装按钮。

生成、转换和打印条码的步骤

让我们分步骤按照步骤进行条码生成、将生成的条码图像转换为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应用程序设计为强大的打印库。 与主要处理PDF相关任务的IronPDF和一般用途打印机制的C#微软打印不同,铁取提供了一套专用类和方法,为打印过程提供细致控制。

借助IronPrint,开发人员可以利用:

  • 可定制的打印设置: IronPrint允许开发人员精细地控制打印过程的各个方面,如纸张尺寸、方向、DPI、份数、打印机名称、边距和灰度打印。 当对特定应用需求的打印精度至关重要时,这种定制水平是有益的。
  • 具有Printer类的多功能打印: 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 生成条形码图像,将其转换为 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# 应用程序中,可以提高操作效率,减少错误,并简化各行业的数据管理流程。

Jordi Bardia
软件工程师
Jordi 最擅长 Python、C# 和 C++,当他不在 Iron Software 利用这些技能时,他就在游戏编程。分享产品测试、产品开发和研究的责任,Jordi 在持续的产品改进中增加了巨大的价值。多样的经验使他面临挑战并保持投入,他表示这是在 Iron Software 工作的最喜欢的方面之一。Jordi 在佛罗里达州迈阿密长大,并在佛罗里达大学学习计算机科学和统计学。