跳至頁尾內容
USING IRONPRINT

如何在 C# 中列印 QR 程式碼

在C#中生成QR碼使用IronQR,透過IronPDF轉換為PDF,並使用IronPrint程式庫列印 - 橋接實體與數位互動的完整方案,透過程式化的QR碼列印實現。

QR Code,或稱快速反應碼,已成為我們數位時代中無處不在的存在。 它們儲存資訊在一個由黑色方塊組成的矩陣中,背景為白色,可以使用智能手機或專用的QR Code閱讀器進行掃描。 使用 .NET barcode DLL 的這些QR Code/條碼被用於各種用途,包括產品標籤、移動支付和行銷材料。 列印QR Code 對於無縫整合實體和數位互動至關重要。

在本文中,我們將首先使用IronQR這個QR碼生成程式庫在C#中生成一個QR碼,然後使用IronPDF將它轉換為PDF,最後使用C# IronPrint程式庫列印出具像素級精準的文件,包含QR碼圖像。

如何在C#中列印QR碼?

  1. 建立一個Visual Studio專案
  2. 安裝 IronQRIronPDFIronPrint 程式庫
  3. 使用QrWriter.Write()方法建立QR碼
  4. 使用SaveAs方法將生成的QR碼儲存為圖像
  5. 使用IronPDF的ImageToPdfConverter建立PDF文件
  6. 使用IronPrint調整PrinterSettings
  7. 使用IronPrint的 Printer.Print() 方法列印

什麼是IronPrint及為何在C#列印中使用它?

IronPrint,由 Iron Software 開發,是一個強大的.NET列印程式庫,提供了一套多功能的工具來處理C#中的列印任務。 它支持廣泛的環境,包括Windows、macOS、Android和iOS。 這種跨平台特性使其成為需要隨著規模擴展的初創公司的理想選擇。 此程式庫的功能包括從基本文件列印到高級列印控制選項,成為滿足任何列印需求的綜合解決方案。

在本文中,我們將探討如何利用IronPrint與IronQR和IronPDF一起在C#控制台應用程式中生成QR碼、轉換並列印QR碼。 這些程式庫的組合提供了一個完整的端到端解決方案,既具備成本效益,又能快速部署 - 完美適合MVP開發。

IronPrint有哪些主要功能?

IronPrint 以專用的用於列印相關功能的類別和方法為特點。 主要功能包括:

  • 全面的列印設定:自訂紙張大小、方向、DPI、份數、印表機名稱、邊界和灰階選項。 應用自訂設置以符合您的需求。
  • 多功能列印與Printer類:**Printer**類提供方法列印不同檔案型別,包括圖片和PDF。
  • 跨平台支持:從Web應用至行動解決方案多平台運行。
  • 印表機訊息獲取:獲取印表機資訊並高效管理跨裝置本地印表機。

開始之前我需要哪些先決條件?

在深入瞭解如何建立C#控制台應用程式以列印QR Code之前,請確保您已具備以下必要條件:

  1. Visual Studio: 安裝Microsoft Visual Studio,一個強大的C#整合開發環境。 您可以從其官方 網站下載。
  2. IronQR程式庫:生成QR碼所必需。 通過NuGet控制台或從IronQR的NuGet官網安裝。
  3. IronPDF程式庫:IronPDF將生成的QR碼圖像轉換為PDF。 使用相同的NuGet方法安裝。
  4. IronPrint程式庫:安裝IronPrint以啟用無縫列印。 檢查初創公司的授權選項

如何在Visual Studio中建立C#控制台應用程式?

按照以下步驟在Visual Studio中設置一個C#控制台應用程式:

  1. 打開Visual Studio並建立一個新的C#控制台應用程式
  2. 配置專案如下,然後點擊"下一步"

    Visual Studio專案配置對話框,顯示一個名為'IronPrint'的新C#控制台應用程式的設置,包含已填寫的專案位置和解決方案名稱欄位。

  3. 接下來,選擇 .NET Framework 以獲取附加資訊,然後點擊 "Create"。

如何透過NuGet套件管理器安裝必要的程式庫?

按照步驟安裝必要的程式庫:

  1. 在Visual Studio專案中,使用工具選單或解決方案總覽打開NuGet套件管理器控制台或解決方案的NuGet套件管理器。
  2. 安裝 IronQR QR Code程式庫:

    • 使用NuGet套件管理器控制台執行以下命令:
    Install-Package IronPrint
    • 使用專案的NuGet套件管理器:在NuGet的瀏覽標籤中搜尋"IronQR",QR碼程式庫並點擊安裝。

    Visual Studio中的NuGet套件管理器視窗顯示'IronQR'的搜尋結果,列出了各種Iron Software套件,包括IronQR、IronOCR和語言包。

  3. 安裝 IronPDF PDF程式庫:

    • 在NuGet套件管理器控制台中輸入以下命令:

      Install-Package IronPrint
      • 使用專案的NuGet套件管理器:在NuGet的瀏覽標籤中搜尋"IronPDF"程式庫並點擊安裝。

Visual Studio中NuGet套件管理器顯示IronPDF套件的搜尋結果,已選擇主要的IronPDF套件進行安裝

  1. 安裝 IronPrint 列印程式庫:

    • 在NuGet套件管理器控制台中,新增以下命令:

      Install-Package IronPrint
      • 使用專案的NuGet套件管理器:在NuGet的瀏覽標籤中搜尋"IronPrint"程式庫並點擊安裝。

Visual Studio中NuGet套件管理器顯示'IronPrint'套件的搜尋結果,已選擇主要的IronPrint套件進行安裝。

建立、轉換和列印QR碼的步驟是什麼?

讓我們分步說明使用IronQR、IronPDF和IronPrint建立QR碼、將QR碼轉換為PDF,然後列印QR碼生成器的輸出的過程。 此工作流程設計為快速實施,非常適合要求縮短市場時間的初創公司環境。 整個過程可在30分鐘內設置並運行,讓您快速將QR碼列印整合到MVP中。

如何實施完整的QR碼列印過程?

步驟1:引用程式庫

在Program.cs檔案的頂部,我們將包含所需程式庫的引用。 這確保程式庫被安裝並準備好使用。 這些匯入提供生成QR、PDF轉換和列印操作所需的全部功能。

// 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
$vbLabelText   $csharpLabel

步驟2:使用IronQR建立QR Code

在這一步,我們首先使用生成QR碼IronQR,然後將其儲存為圖像,如以下程式碼範例所示。 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

// For more advanced QR codes, you can include URLs, contact info, or WiFi credentials
// Example: QrCode urlQr = QrWriter.Write("___PROTECTED_URL_52___");
// Example: QrCode wifiQr = QrWriter.Write("WIFI:T:WPA;S:NetworkName;P:Password;;");
// 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

// For more advanced QR codes, you can include URLs, contact info, or WiFi credentials
// Example: QrCode urlQr = QrWriter.Write("___PROTECTED_URL_52___");
// Example: QrCode wifiQr = QrWriter.Write("WIFI:T:WPA;S:NetworkName;P:Password;;");
' 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

' For more advanced QR codes, you can include URLs, contact info, or WiFi credentials
' Example: Dim urlQr As QrCode = QrWriter.Write("___PROTECTED_URL_52___")
' Example: Dim wifiQr As QrCode = QrWriter.Write("WIFI:T:WPA;S:NetworkName;P:Password;;")
$vbLabelText   $csharpLabel

在此程式碼片段中:

  • 使用QrWriter生成一個帶有訊息"Hello IronPrint!"的QR碼。
  • QR碼儲存為AnyBitmap文件。
  • 圖像儲存到"assets"資料夾為"qr.png"。
  • 您可以針對公司URL或產品資訊進行自訂。

這裡是QR Code輸出:

以C#程式碼生成的黑白QR碼,顯示三個角落定位標記的方形矩陣條碼圖案。

對於希望實現QR碼以用於各種用例,如庫存追蹤、客戶互動或支付處理的初創公司,IronQR提供了廣泛的自訂選項。 您可以調整錯誤校正級別、尺寸,甚至新增徽標到您的QR碼。

步驟3:使用IronPDF將QR圖轉換為PDF

接下來,我們會使用將圖像轉換為PDF使用IronPDF。 PDF可以保護文件的格式,非常適合共享和列印。 這裡,每個圖像文件都將放置在單獨的PDF文件頁面上。 當您需要為批次列印生成多個QR碼時,這種方法特別有用:

// 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");

// For batch processing multiple QR codes
// You can also add custom headers, footers, or watermarks to your PDFs
// This is useful for branding or adding tracking information
// 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");

// For batch processing multiple QR codes
// You can also add custom headers, footers, or watermarks to your PDFs
// This is useful for branding or adding tracking information
Imports System.IO
Imports System.Linq

' 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")

' For batch processing multiple QR codes
' You can also add custom headers, footers, or watermarks to your PDFs
' This is useful for branding or adding tracking information
$vbLabelText   $csharpLabel

在此程式碼片段中:

  • 在"assets"資料夾中枚舉.jpg或.png擴展名的圖像文件。
  • 使用ImageToPdf()將圖像轉換為PDF。
  • 將結果資儲存為"composite.pdf"。
  • 對於批量處理多個QR碼非常高效。

這是輸出:

Adobe Acrobat介面顯示了一個居中QR碼在頁面的PDF文件

步驟4:使用IronPrint列印PDF

最後,我們會使用IronPrint - 一個多功能的列印程式庫,與列印設定列印生成的PDF。 IronPrint提供了廣泛的列印過程控制,允許您配置紙張大小至邊界的所有內容。 您可以探索列印設置教程列印設置教程以獲得更高級的配置:

// 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 settings
printerSettings.PaperSize = PaperSize.A4;                    // Set paper size
Margins margins = new Margins(30, 10);                       // Set paper margins
printerSettings.PaperMargins = margins;                      // Apply margins

// Additional settings for production environments
printerSettings.Grayscale = false;                           // Color printing for better QR code contrast
printerSettings.NumberOfCopies = 1;                          // Set number of copies
printerSettings.Dpi = 300;                                   // High DPI for clear QR codes

// Print the PDF
Printer.Print("assets/composite.pdf", printerSettings);      // Print the PDF

// For interactive printing with user dialog
// Printer.ShowPrintDialog("assets/composite.pdf", printerSettings);
// 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 settings
printerSettings.PaperSize = PaperSize.A4;                    // Set paper size
Margins margins = new Margins(30, 10);                       // Set paper margins
printerSettings.PaperMargins = margins;                      // Apply margins

// Additional settings for production environments
printerSettings.Grayscale = false;                           // Color printing for better QR code contrast
printerSettings.NumberOfCopies = 1;                          // Set number of copies
printerSettings.Dpi = 300;                                   // High DPI for clear QR codes

// Print the PDF
Printer.Print("assets/composite.pdf", printerSettings);      // Print the PDF

// For interactive printing with user dialog
// Printer.ShowPrintDialog("assets/composite.pdf", printerSettings);
Imports System.Collections.Generic

' 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

' Configure print settings
printerSettings.PaperSize = PaperSize.A4                    ' Set paper size
Dim margins As New Margins(30, 10)                          ' Set paper margins
printerSettings.PaperMargins = margins                      ' Apply margins

' Additional settings for production environments
printerSettings.Grayscale = False                           ' Color printing for better QR code contrast
printerSettings.NumberOfCopies = 1                          ' Set number of copies
printerSettings.Dpi = 300                                   ' High DPI for clear QR codes

' Print the PDF
Printer.Print("assets/composite.pdf", printerSettings)      ' Print the PDF

' For interactive printing with user dialog
' Printer.ShowPrintDialog("assets/composite.pdf", printerSettings)
$vbLabelText   $csharpLabel

在此程式碼片段中:

  • 使用Printer.GetPrinterNames()獲取印表機名稱。
  • 為演示設置印表機名稱為"Microsoft Print to PDF"。
  • 配置A4紙張大小和30/10邊界。
  • 將DPI設置為300以獲得清晰的QR碼。
  • 使用配置的設置透過Printer.Print()進行列印。

這是列印文件的輸出。這顯示了圖像將如何被列印:

Adobe Acrobat顯示一個黑白QR碼的PDF文件由C#程式碼生成。 使用 IronPrint 列印到物理列印機要方便得多。 為了在列印時獲得更多控制,您可以使用ShowPrintDialog()方法,如列印對話框範例中所展示的。 如需瞭解更多有關如何高效列印的資訊,請參閱文件頁面或查看完整的列印文件教程

為什麼選擇IronPrint而不是其他C#列印解決方案?

IronPrint 被特別設計為強大的.NET應用程式列印程式庫。 與主要專注於PDF相關任務的IronPDF以及作為通用列印機制的Microsoft列印不同,IronPrint提供了針對列印過程細粒度控制的專用類別和方法。 API API 參考提供了所有可用類別和方法的綜合文件。

使用IronPrint,開發者可以利用進階功能簡化開發過程並縮短上市時間 - 這些都是初創成功的關鍵因素。 此程式庫架構處理從簡單文件列印到複雜批量操作的一切,僅需極少的程式碼。

為什麼非同步列印重要?

IronPrint 提供異步函式,防止列印操作阻塞執行緒並提高性能。 這對於需要高響應能力的Web應用程式特別重要。 ASP.NET Web App Framework指南ASP.NET Web App Framework指南展示如何在Web環境中實施非同步列印。

多功能列印如何造福我的應用程式?

IronPrint中的專用 Printer 類別允許進行多功能列印各種文件型別,提供超越標準列印的靈活性。 您可以於多種格式列印您的文件,無需為每種文件型別使用不同的程式庫。

IronPrint支持哪些平台?

IronPrint支援Windows、Android、iOS和macOS,使其適合多樣化的應用程式環境。 這種跨平台的能力意味着您可以一次構建,隨處部署,顯著降低開發成本。

我可以自訂什麼列印設置?

開發者可以透過PrintSettings類精細控制紙張大小、方向、DPI、份數等。 列印設置範例列印設置範例展示了所有自訂選項。

C#中QR碼列印的關鍵要點是什麼?

IronPrintIronQR,和IronPDF的結合提供了一個鞏固的解決方案,用於在C#中建立、轉換和列印QR碼。 異步列印的優勢、多用途列印選擇和跨平台支持使 IronPrint 成為開發者的寶貴工具。 遵循本指南中的詳細步驟,您可以生成QR碼條形碼並將其列印無縫整合到您的C#應用程式中,橋接實體與數位互動之間的差距。

對於希望快速實施QR碼解決方案的初創公司,此技術堆疊提供多項優勢:

  • 快速實施:在一小時內啟動并運行您的QR碼列印
  • 具成本效益:從免費試用開始,並透過彈性授權擴展
  • 適合生產:內建錯誤處理和高效能架構
  • 未來規範:定期更新和出色的支持確保長期可行性

IronPrint提供了一個免費試用從$999開始,使其便於初創公司在承諾之前進行評估。 您可以探索不同的授權選項,包括擴展升級,隨著您的業務成長。 從這裡下載該程式庫並試用。

如需額外支持,請查看故障排除指南或了解如何在專案中妥善配置授權金鑰。 如果您正在處理Web應用程式,設定授權金鑰的指南在Web.config中設置授權金鑰指南將幫助您快速入門。

為了獲得最新功能更新和改進,請定期檢查更新日誌。 IronPrint團隊不斷根據使用者反饋新增新功能,確保程式庫隨您的需求不斷發展。

常見問題

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 Console中使用'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擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

Iron 支援團隊

我們線上24小時,每週5天。
聊天
電子郵件
給我打電話