如何在 C# 中列印 QR 程式碼
QR Code,或稱快速反應碼,已成為我們數位時代中無處不在的存在。 它們儲存資訊在一個由黑色方塊組成的矩陣中,背景為白色,可以使用智能手機或專用的QR Code閱讀器進行掃描。 使用 .NET barcode DLL 的這些QR Code/條碼被用於各種用途,包括產品標籤、移動支付和行銷材料。 列印QR Code 對於無縫整合實體和數位互動至關重要。
在本文中,我們將首先使用 IronQR,QR Code生成程式庫在C#中生成一個QR Code,然後使用 IronPDF 將其轉換為PDF,最後使用C# IronPrint程式庫列印具備完美像素的帶有QR Code圖像的文件。
How to Print a QR Code in C
- 建立一個Visual Studio專案
- 安裝 IronQR、IronPDF 和 IronPrint 程式庫
- 使用 QrWriter.Write() 方法建立一個QR Code
- 使用 SaveAs 方法將生成的QR Code保存為圖像
- 使用IronPDF的 ImageToPdfConverter 建立一個PDF文件
- 使用IronPrint調整 PrinterSettings
- 使用IronPrint的 Printer.Print() 方法列印
IronPrint - The C# Printing Library
IronPrint,由 Iron Software 開發,是一個強大的.NET列印程式庫,提供了一套多功能的工具來處理C#中的列印任務。 它支持廣泛的環境,包括Windows、macOS、Android和iOS。 在本文中,我們將探索如何利用IronPrint與IronQR和IronPDF來在C#控制台應用程式中建立QR Code,並轉換和列印QR Code。
IronPrint 的功能特點
IronPrint 以專用的用於列印相關功能的類別和方法為特點。 主要功能包括:
- 全面的列印設定: IronPrint 允許開發者自定義列印過程的各個方面,例如紙張大小、方向、DPI、影印本數、列印機名稱、邊距和灰階列印。
- 多功能列印利器Printer Class: 程式庫中引入了 Printer 類別,提供了一組全面的方法來列印各種文件型別,包括圖像和PDF文件。
- 跨平臺支持: IronPrint 支持多個平臺的列印,使其適用於多種型別的應用程式。
先決條件
在深入瞭解如何建立C#控制台應用程式以列印QR Code之前,請確保您已具備以下必要條件:
Create a C# Console Application in Visual Studio
按照以下步驟在Visual Studio中設置一個C#控制台應用程式:
- 打開Visual Studio並建立一個新的C#控制台應用程式
- 配置專案如下,然後點擊"下一步"
如何在C#中列印QR Code:圖1 - 通過指定專案名稱、位置和解決方案名稱配置您的新的C#控制台應用程式專案。 接下來,點擊 "下一步" 按鈕。]
- 接下來,選擇 .NET Framework 以獲取附加資訊,然後點擊 "Create"。
通過NuGet套件管理器安裝必要的程式庫
按照步驟安裝必要的程式庫:
- 在Visual Studio專案中,使用工具選單或解決方案總覽打開NuGet套件管理器控制台或解決方案的NuGet套件管理器。
-
安裝 IronQR QR Code程式庫:
-
在NuGet套件管理器控制台中,新增以下命令:
Install-Package IronPrint
- 使用解決方案的NuGet套件管理器進行管理:在NuGet的瀏覽標籤中搜尋"IronQR",QR Code程式庫,然後點擊安裝。

-
-
安裝 IronPDF PDF程式庫:
-
在NuGet套件管理器控制台中輸入以下命令:
Install-Package IronPrint
- 使用解決方案的NuGet套件管理器進行管理:在NuGet的瀏覽標籤中搜尋"IronPDF"程式庫,然後點擊安裝。

-
-
安裝 IronPrint 列印程式庫:
-
在NuGet套件管理器控制台中,新增以下命令:
Install-Package IronPrint
- 使用解決方案的NuGet套件管理器進行管理:在NuGet的瀏覽標籤中搜尋"IronPrint"程式庫,然後點擊安裝。

-
建立、轉換和列印QR Code的步驟
讓我們逐步分解使用IronQR、IronPDF和IronPrint建立QR Code、將QR Code轉換為PDF並最終列印QR Code生成器輸出的過程。
Step-by-Step Procedure to Print C# QR Code
步驟1:引用程式庫
在Program.cs文件的頂部,我們將包含對必需程式庫的引用。 這確保程式庫被安裝並準備好使用。
// Reference to libraries
using IronPrint; // Library for printing functionalities
using IronPdf; // Library for PDF handling
using IronQr; // Library for QR code generation
using IronSoftware.Drawing; // Library for image processing
// Reference to libraries
using IronPrint; // Library for printing functionalities
using IronPdf; // Library for PDF handling
using IronQr; // Library for QR code generation
using IronSoftware.Drawing; // Library for image processing
' Reference to libraries
Imports IronPrint ' Library for printing functionalities
Imports IronPdf ' Library for PDF handling
Imports IronQr ' Library for QR code generation
Imports IronSoftware.Drawing ' Library for image processing
步驟2:使用IronQR建立QR Code
在此步驟中,首先,我們將使用 生成QR Code 使用 IronQR,然後將其保存為圖像,如下程式碼範例所示:
// Code to create a QR code using IronQR
QrCode myQr = QrWriter.Write("Hello IronPrint!"); // Generate a QR code with the message
AnyBitmap qrImage = myQr.Save(); // Save QR code as an image
qrImage.SaveAs("assets/qr.png"); // Save the png image file to the "assets" folder
// Code to create a QR code using IronQR
QrCode myQr = QrWriter.Write("Hello IronPrint!"); // Generate a QR code with the message
AnyBitmap qrImage = myQr.Save(); // Save QR code as an image
qrImage.SaveAs("assets/qr.png"); // Save the png image file to the "assets" folder
' Code to create a QR code using IronQR
Dim myQr As QrCode = QrWriter.Write("Hello IronPrint!") ' Generate a QR code with the message
Dim qrImage As AnyBitmap = myQr.Save() ' Save QR code as an image
qrImage.SaveAs("assets/qr.png") ' Save the png image file to the "assets" folder
在此程式碼片段中:
- 使用 QrWriter 類別生成帶有訊息"Hello IronPrint!"的QR Code。 Write() 方法允許生成帶有訊息甚至是資料的QR Code。
- QR Code 然後被保存為一個 AnyBitmap 文件,是Iron Software提供的通用相容的C#Bitmap類。
- QR Code圖像被保存到名為"qr.png"的"assets"文件夾中。
這裡是QR Code輸出:

步驟3:使用IronPDF將QR圖轉換為PDF
接下來,我們將使用 將圖像轉換為PDF 利用 IronPDF。 PDF可以保護文件的格式,非常適合共享和列印。 這裡,每個圖像文件都將放置在單獨的PDF文件頁面上。
// 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");
// 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");
' Code to convert QR Image to PDF using IronPDF
Dim imageFiles = Directory.EnumerateFiles("assets").Where(Function(f) f.EndsWith(".jpg") OrElse 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。
這是輸出:

步驟4:使用IronPrint列印PDF
我們將最終使用 IronPrint - 一個多功能的列印程式庫,以 列印生成的PDF 並配置列印機設置。
// Code for Printing using IronPrint
// Get available printer names
List<string> 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 setting
printerSettings.PaperSize = PaperSize.A4; // Set paper size
Margins margins = new Margins(30,10); // Set paper margins
printerSettings.PaperMargins = margins; // Apply margins
Printer.Print("assets/composite.pdf", printerSettings); // Print the PDF
// Code for Printing using IronPrint
// Get available printer names
List<string> 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 setting
printerSettings.PaperSize = PaperSize.A4; // Set paper size
Margins margins = new Margins(30,10); // Set paper margins
printerSettings.PaperMargins = margins; // Apply margins
Printer.Print("assets/composite.pdf", printerSettings); // Print the PDF
' Code for Printing using IronPrint
' Get available printer names
Dim printerNames As List(Of String) = Printer.GetPrinterNames()
' Create print settings object
Dim printerSettings As New PrintSettings()
For Each printerName As String In printerNames
' Set desired printer name
If printerName.Equals("Microsoft Print to PDF") Then
printerSettings.PrinterName = printerName
End If
Next printerName
'Configure print setting
printerSettings.PaperSize = PaperSize.A4 ' Set paper size
Dim margins As New Margins(30,10) ' Set paper margins
printerSettings.PaperMargins = margins ' Apply margins
Printer.Print("assets/composite.pdf", printerSettings) ' Print the PDF
在此程式碼片段中:
- 使用
Printer.GetPrinterNames()擷取可用的列印機名稱。 - 設定所需的列印機名稱(在這裡,示範列印過程的"Microsoft Print to PDF")。 如果未指定,則將使用附加的預設列印機。
- 配置列印設置,指定紙張大小為A4並設置邊距。 邊距結構有多個重載,還提供像Top、Bottom、Left、Right這樣的字段來按需求設定邊距。
- 使用
Printer.Print()列印PDF。 第一個參數是要列印的文件路徑,第二個是如果指定的printerSettings。
這是列印文件的輸出。這顯示了圖像將如何被列印:
使用 IronPrint 列印到物理列印機要方便得多。 要在列印時獲得更多控制,您可以使用 ShowPrintDialog() 方法。 有關如何有效列印的更多資訊,請存取此 說明文件頁面。
Advantages of IronPrint for Printing in C
IronPrint 被特別設計為強大的.NET應用程式列印程式庫。 與 IronPDF 主要專注於處理PDF相關任務,和Microsoft列印作為一個通用列印機制不同,IronPrint 提供了一套專用的類別和方法,為列印過程提供精細控制。 使用 IronPrint,開發者可以使用:
1. 異步列印
IronPrint 提供異步函式,防止列印操作阻塞執行緒並提高性能。
2. 多功能列印
IronPrint中的專用 Printer 類別允許進行多功能列印各種文件型別,提供超越標準列印的靈活性。
3. 跨平臺支持
IronPrint支持多個平台,包括Windows、Android、iOS、macOS,使其適合多樣的應用程式環境。
4. 可自定義的列印設置
開發人員可以通過 PrintSettings 類細緻控制列印設置,包括紙張大小、方向、DPI、影印本數等。
結論
總而言之,IronPrint、IronQR 和 IronPDF 的結合,為在C#中建立、轉換和列印QR Code提供了強大的解決方案。 異步列印的優勢、多用途列印選擇和跨平台支持使 IronPrint 成為開發者的寶貴工具。 按照本指南中列出的詳細步驟進行操作,您可以生成QR Code條碼,並將其列印無縫整合在您的C#應用程式中,彌合物理和數字互動之間的鴻溝。
常見問題
如何在C#中生成QR碼?
要在C#中生成QR碼,使用IronQR程式庫。您可以使用IronQR的QrWriter.Write()方法來生成帶有所需資訊或資料的QR碼。
如何在C#中將QR碼圖像轉換為PDF?
您可以使用IronPDF的ImageToPdfConverter.ImageToPdf()方法在C#中將QR碼圖像轉換為PDF。該功能使您能夠輕鬆地將圖像文件轉換為PDF文件。
在C#中使用QR碼進行列印涉及哪些步驟?
這些步驟包括使用IronQR生成QR碼,使用IronPDF將其轉換為PDF,然後使用IronPrint的Printer.Print()方法和您指定的設定進行列印。
是否可以自訂C#中的QR碼列印設定?
是的,您可以使用IronPrint的PrintSettings類自訂列印設定。它允許您指定紙張大小、方向、DPI、邊距及其他設定以符合您的列印需求。
這種列印方法是否可以在不同操作系統上使用?
可以,IronPrint支持跨平台的列印功能,與各種操作系統相容,包括Windows、macOS、Android和iOS。
使用IronPrint列印QR碼有什麼好處?
IronPrint提供了諸如異步列印、支持各種文件型別、跨平台相容性以及能夠自訂列印設定等優勢,這使其成為C#列印任務的高效選擇。
如何解決在C#中列印QR碼時遇到的問題?
常見的故障排除步驟包括確保所有必要的程式庫均已正確安裝,檢查IronPrint中的列印設定,以及驗證QR碼生成和PDF轉換過程以確保沒有錯誤。
有沒有辦法在購買前試用IronPrint?
有的,IronPrint提供免費試用,使您能夠在做出購買決定之前探索其功能並評估其與您的C#應用程式的整合。


