在生產環境中測試,無水印。
在任何需要的地方都能運行。
獲得 30 天的全功能產品。
在幾分鐘內上手運行。
試用產品期間完全訪問我們的支援工程團隊
QR碼或快速反應碼在我們的數位時代已變得無處不在。 它們將資訊存儲在白色背景上的黑色方塊矩陣中,可以使用智能手機或專用的 QR 碼讀取器掃描。 這些使用 .NET 條碼 DLL 的 QR 碼/條碼用於各種目的,包括產品標籤、移動支付和行銷材料。 列印 QR 碼 對於無縫整合實體和數位互動至關重要。
在這篇文章中,我們首先使用IronQR,QR碼生成器庫在C#中生成QR碼,然後使用IronPDF**將其轉換為PDF,最後使用C#的[IronPrint**](/csharp/print/)庫列印具有QR碼圖像的像素完美文檔。
創建一個 Visual Studio 專案
安裝IronQR、IronPDF和IronPrint庫
使用 QrWriter.Write() 方法創建一個 QR 碼
使用SaveAs方法將生成的 QR Code 儲存為圖像
使用 IronPDF 的 ImageToPdfConverter 創建 PDF 文檔
使用 IronPrint 調整PrinterSettings
IronPrint,由Iron Software開發,是一個功能強大的 .NET 列印庫,提供多種處理 C# 列印任務的工具。 它支持多種環境,包括 Windows、macOS、Android 和 iOS。 在本文中,我們將探討如何利用IronPrint結合IronQR和IronPDF,在C#控制台應用程式中創建QR碼,以及轉換和列印QR碼。
IronPrint 以專門為列印相關功能設計的類別和方法而著稱。 主要功能包括:
在開始進行使用 C# 創建主控台應用程式來列印 QR 碼的步驟之前,請確保您具備以下先決條件:
Visual Studio:安裝 Microsoft Visual Studio,一個強大的 C# 集成開發環境。 您可以從其官方網站下載。
IronQR 庫:此庫對於生成 QR 碼至關重要。 使用 NuGet 控制台安裝它,或直接從官方 IronQR NuGet 網站。
IronPDF Library: IronPDF 將被用來將生成的 QR code 條碼圖像轉換為 PDF。 使用相同的 NuGet 安裝方法安裝它。
遵循以下步驟在 Visual Studio 中設置 C# 控制台應用程式:
打開 Visual Studio 並創建一個新的 C# 控制台應用程式
按以下方式配置專案,然後點擊“下一步”
![如何在 C# 中打印 QR 代码:图 1 - 通过指定项目名称、位置和解决方案名称来配置您的新 C# 控制台应用程序项目。 然後點擊「下一步」按鈕。
按照以下步驟安裝所需的庫:
在 Visual Studio 專案中,使用工具選單或方案總管開啟 NuGet 套件管理器主控台或 NuGet 套件管理器。
安裝IronQR二維碼庫:
:ProductInstall
使用管理解決方案的 NuGet 套件:在 NuGet 的瀏覽標籤中,搜尋「IronQR」這個 QR 碼函式庫並點選安裝。
安裝IronPDF PDF 庫:
:ProductInstall
使用管理 NuGet 套件管理工具:在 NuGet 的瀏覽標籤中,搜尋 "IronPDF" 程式庫並點擊安裝。
安裝IronPrint打印庫:
:ProductInstall
使用管理方案的 NuGet 套件:在 NuGet 的瀏覽標籤中,搜尋 "IronPrint" 資料庫並點擊安裝。
讓我們分步解析使用 IronQR、IronPDF 和 IronPrint 來創建 QR Code,將 QR Code 轉換為 PDF,最後列印 QR Code 生成器輸出的過程。
在 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
在此步驟中,我們將首先使用IronQR生成一個 QR 碼,然後將其保存為圖像,如以下代碼範例所示:
// 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
在此程式碼片段中:
QR碼圖片已儲存到“assets”資料夾,檔名為“qr.png”。
這是 QR 碼輸出:
接下來,我們將使用IronPDF將QR碼圖片轉換為PDF。 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");
IRON VB CONVERTER ERROR developers@ironsoftware.com
在此程式碼片段中:
利用 IronPDF 的 ImageToPdfConverter.ImageToPdf() 方法將圖像轉換為名為「composite.pdf」的 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.Print(). 列印 PDF。第一個參數是要列印的檔案路徑,第二個參數是 printerSettings,如果有指定的話。
以下是列印檔案的輸出。這顯示了圖像將如何被列印:
使用 IronPrint 打印到實體印表機變得更容易。 為了在列印時獲得更多控制,您可以使用ShowPrintDialog()方法。 如需了解如何有效列印的更多資訊,請造訪此文件頁面。
IronPrint 專為 .NET 應用程序設計成強大的打印庫。 與主要專注於處理 PDF 相關任務的 IronPDF 以及作為通用列印機制的 Microsoft 列印不同,IronPrint 提供了一套專用的類別和方法,專門為列印過程中的精細控制而設計。
使用IronPrint,開發者可以利用:
IronPrint 提供非同步功能,防止列印操作阻塞執行緒並提升效能。
IronPrint中的專用Printer類別允許多樣化地列印各種檔案類型,提供超越標準列印的靈活性。
IronPrint 支援多個平台,包括 Windows、Android、iOS 和 macOS,使其適合各種應用環境。
開發人員可以通過PrintSettings類來細緻地控制列印設置,包括紙張大小、方向、DPI、副本數量等。
總之,IronPrint、IronQR 和 IronPDF 的組合為在 C# 中創建、轉換和列印 QR 碼提供了一個強大的解決方案。 非同步列印的優勢、多樣的列印選項以及跨平台支援使IronPrint成為開發者的一個有價值的工具。 遵循本指南中列出的詳細步驟,您可以生成 QR 碼條形碼,並將其列印無縫整合到您的 C# 應用程式中,搭建起實體與數位互動之間的橋樑。