跳至页脚内容
使用 IRONPRINT

用 IronPrint 在 C# 中打印 QR 代码

QR 代码或快速反应代码在数字时代已经无处不在。 它们将信息存储在白底黑字的矩阵中,可以使用智能手机或专用 QR 码阅读器扫描。 这些使用 .NET 条形码 DLL 的 QR 码/条形码可用于各种用途,包括产品标签、移动支付和营销材料。 打印 QR 代码对于无缝集成物理和数字交互至关重要。

In this article, we are going to first generate a QR code in C# using IronQR, the QR code generator library, then convert it to PDF using IronPDF, and finally print the pixel-perfect document with a QR code image using the C# IronPrint library.

如何在 C# 中打印二维码

IronPrint--C# 打印库IronPrint 的特点前提条件在 Visual Studio 中创建 C# 控制台应用程序通过 NuGet 包管理器安装必要的库How to Print a QR code in C#:图 2 - 通过搜索 "IronQR",使用 Manage NuGet Package for Solutions 安装 IronQR。 3.安装 **IronPDF** PDF 库: * 使用 NuGet 软件包管理器控制台,输入以下命令: ```shell :ProductInstall ``` * 使用管理解决方案的 NuGet 软件包:在 NuGet 的 "浏览 "选项卡中,搜索 "IronPDF "库并单击 "安装"。 !a href="/static-assets/print/blog/csharp-print-qr/csharp-print-qr-3.webp">How to Print a QR code in C#:图 3 - 通过搜索 "IronPDF",使用 Manage NuGet Package for Solutions 安装 IronPDF. 4.安装 **IronPrint** 打印库: * 使用 NuGet 软件包管理器控制台,添加以下命令: ```shell :ProductInstall ``` * 使用管理解决方案的 NuGet 软件包:在 NuGet 的 "浏览 "选项卡中,搜索 "IronPrint "库并单击 "安装"。 !a href="/static-assets/print/blog/csharp-print-qr/csharp-print-qr-4.webp">How to Print a QR code in C#:图 4 - 通过搜索 "IronPrint",使用 Manage NuGet Package for Solutions 安装 IronPrint .

创建、转换和打印 QR 代码的步骤

打印 C# QR 代码的步骤步骤 1:参考库步骤 2:使用 IronQR 创建 QR 代码How to Print a QR code in C#:图 5 - QR 码输出 图片: qr.png #### 第 3 步:使用 IronPDF 将 QR 图像转换为 PDF. Next, we are going to convert the QR code [**image to a PDF**](https://ironpdf.com/examples/image-to-pdf/) using [**IronPDF**](https://ironpdf.com/). PDF 文件保留了文档的格式,适合共享和打印。 在此,每个图像文件都将放在单独的 PDF 文件页面上。 ```csharp // Code to convert QR Image to PDF using IronPDF var imageFiles = Directory.EnumerateFiles("assets").Where(f => f.EndsWith(".jpg") || f.EndsWith(".png")); // Reading QR codes image files // Convert the QR code images to a PDF and save it ImageToPdfConverter.ImageToPdf(imageFiles).SaveAs("assets/composite.pdf"); ``` 在此代码片段中 * 首先,它会枚举 "assets "文件夹中扩展名为".jpg "或".png "的图像文件,然后读取目录中的二维码。 * 利用 IronPDF 中的 **ImageToPdfConverter.ImageToPdf()** 方法将图像转换为名为 "composite.pdf "的 PDF。 以下是输出结果: !a href="/static-assets/print/blog/csharp-print-qr/csharp-print-qr-6.webp">How to Print a QR code in C#:图 6 - 输出包含 QR 代码图像的 PDF:composite.pdf。 #### 第 4 步:使用 IronPrint 打印 PDF. Finally, we will use [**IronPrint**](/csharp/print/) - a versatile Printing library to [**print the generated PDF**](/csharp/print/examples/print-settings/) with Printer Settings. ```csharp // Code for Printing using IronPrint // Get available printer names List printerNames = Printer.GetPrinterNames(); // Create print settings object PrintSettings printerSettings = new PrintSettings(); foreach(string printerName in printerNames) { // Set desired printer name if(printerName.Equals("Microsoft Print to PDF")) printerSettings.PrinterName = printerName; } // Configure print settings printerSettings.PaperSize = PaperSize.A4; // Set paper size Margins margins = new Margins(30, 10); // Set paper margins printerSettings.PaperMargins = margins; // Apply margins // Print the PDF Printer.Print("assets/composite.pdf", printerSettings); // Print the PDF ``` 在此源代码片段中 * 使用 **Printer.GetPrinterNames()** 抓取可用的打印机名称。 * 设置所需的打印机名称(本例中为 "Microsoft Print to PDF",以演示打印过程)。 如果未指定,将使用所附的默认打印机。 * 配置打印设置,指定纸张大小为 A4 并设置页边距。 页边距结构有多个重载,它还提供了顶部、底部、左侧和右侧等字段,可根据需要设置页边距。 * 使用 **Printer.Print() 打印 PDF。** 第一个参数是要打印文件的路径,第二个参数是 **printerSettings** (如果指定)。 下面是打印文件的输出。这显示了图像的打印效果: !a href="/static-assets/print/blog/csharp-print-qr/csharp-print-qr-7.webp">How to Print a QR code in C#:图 7 - 输出打印文件:composite.pdf。 使用[**IronPrint**](/csharp/print/),向物理打印机打印变得更加容易。 要在打印时获得更多控制,您可以使用 **ShowPrintDialog()** 方法。 有关如何高效打印的更多信息,请访问 [** 文档**](/csharp/print/docs/) 页面。

IronPrint 在 C# 中打印的优势

1.异步打印2.多功能打印3.跨平台支持4.可定制的打印设置结论

常见问题解答

QR 码在数字应用中如何工作?

QR 码将信息存储在白色背景上的黑色方块矩阵中,可以使用智能手机或专用 QR 码阅读器进行扫描。广泛用于产品标签、移动支付和营销材料。

如何在 .NET 应用程序中生成 QR 码?

要在 .NET 应用程序中生成 QR 码,请使用 IronQR 库。QrWriter.Write() 方法允许您创建 QR 码,之后可以将其保存为图像以便进一步处理。

使用 C# 打印 QR 码涉及哪些步骤?

该过程包括使用 IronQR 生成 QR 码,使用 IronPDF 将其转换为 PDF,然后使用 IronPrint 打印。此过程可确保高质量的输出,适用于各种应用。

是什么让 IronPrint 成为 .NET 打印任务的多功能选择?

IronPrint 提供丰富的功能,如异步打印、全面的打印设置自定义和跨平台支持,使其成为 .NET 打印任务的多功能选择。

IronPrint 支持哪些平台的打印任务?

IronPrint 支持多个平台的打印,包括 Windows、macOS、Android 和 iOS,为在多样化环境工作的开发人员提供灵活性。

如何在 Visual Studio 中安装 IronQR、IronPDF 和 IronPrint 库?

使用 Visual Studio 中的 NuGet 包管理器安装这些库。在 NuGet 控制台中使用 'Install-Package' 命令将每个库添加到您的项目中。

IronPrint 相较于传统的 Microsoft 打印有哪些优势?

IronPrint 提供诸如异步打印、灵活的文件类型支持和可定制的打印设置优势,为将 QR 码打印集成到 C# 应用程序中提供了强大的解决方案。

如何在 C# 中将 QR 码图像转换为 PDF?

您可以使用 IronPDF 库在 C# 中将 QR 码图像转换为 PDF。使用 RenderHtmlAsPdf 方法将 QR 码图像包含在 PDF 文档中。

设置 C# 中的 QR 码打印需要哪些预备条件?

确保您已安装 Visual Studio,以及 IronQR、IronPDF 和 IronPrint 库。可以通过 Visual Studio 中的 NuGet 包管理器安装这些库。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。