跳過到頁腳內容
使用 IRONPRINT

使用 IronPrint 在 C# 中打印 QR 代碼

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# 中打印 QR 碼

**IronPrint** 由 IronPrint 的特點先決條件在 Visual Studio 中建立 C# 主控台應用程式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」的圖片檔,然後從目錄中讀取 QR 代碼。 * 利用 IronPDF 中的 **ImageToPdfConverter.ImageToPdf()** 方法將影像轉換為名為「composite.pdf」的 PDF。 以下是輸出結果: ![How to Print a QR code in C#:圖 6 - 包含 QR 碼影像的 PDF 輸出:composite.pdf](/static-assets/print/blog/csharp-print-qr/csharp-print-qr-6.webp) #### 步驟 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 並設定邊距。 Margins 結構有多種重載,它也會呈現 Top、Bottom、Left 和 Right 等欄位,以根據需要設定頁邊。 * 使用 **Printer.Print().** 來列印 PDF。第一個參數是要列印檔案的路徑,第二個參數是 **printerSettings** (如果指定的話)。 以下是列印檔案的輸出。這顯示了影像的列印方式: ![How to Print a QR code in C#:圖 7 - 輸出列印檔案: composite.pdf](/static-assets/print/blog/csharp-print-qr/csharp-print-qr-7.webp) 使用 [**IronPrint**](/csharp/print/),向實體印表機列印就容易多了。 若要在列印時取得更多控制權,您可以使用 **ShowPrintDialog()** 方法。 如需更多關於如何有效率列印的資訊,請造訪此 [** 文件說明**](/csharp/print/docs/) 頁面。

IronPrint 在 C# 中打印的優勢

2.多功能列印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 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。