Jak wydrukować kod QR w języku C#
Kody QR, czyli kody Quick Response, stały się wszechobecne w erze cyfrowej. Informacje są przechowywane w matrycy czarnych kwadratów na białym tle i można je zeskanować za pomocą smartfona lub dedykowanego czytnika kodów QR. Te kody QR/kody BarCode wykorzystujące bibliotekę DLL BarCode .NET są używane do różnych celów, w tym do etykietowania produktów, płatności mobilnych i materiałów marketingowych. Drukowanie kodów QR ma kluczowe znaczenie dla płynnej integracji interakcji fizycznych i cyfrowych.
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.
How to Print a QR Code in C
- Utwórz projekt Visual Studio
- Zainstaluj biblioteki IronQR, IronPDF i bibliotekę IronPrint
- Create a QR Code using QrWriter.Write() method
- Save the generated QR Code as an image using the SaveAs method
- Utwórz dokument PDF za pomocą narzędzia ImageToPdfConverter firmy IronPDF
- Dostosuj ustawienia drukarki za pomocą IronPrint
- Drukuj za pomocą metody Printer.Print() biblioteki IronPrint
IronPrint - The C# Printing Library
IronPrint, opracowany przez Iron Software, to potężna biblioteka drukowania dla platformy .NET, oferująca wszechstronny zestaw narzędzi do obsługi zadań drukowania w języku C#. Obsługuje szeroki zakres środowisk, w tym Windows, macOS, Android i iOS. In this article, we'll explore how IronPrint, in conjunction with IronQR and IronPDF, can be leveraged to create QR codes, and convert, and print QR codes in a C# console application.
Features of IronPrint
IronPrint wyróżnia się dedykowanymi klasami i metodami dostosowanymi do funkcji związanych z drukowaniem. Najważniejsze cechy to:
- Comprehensive Print Settings: IronPrint allows developers to customize various aspects of the printing process, such as paper size, orientation, DPI, number of copies, printer name, margins, and grayscałe printing.
- Versatile Printing with Printer Class: The library introduces the Printer class, providing a comprehensive set of methods for printing various file types, including images and PDF documents.
- Cross-Platform Support: IronPrint supports printing across multiple platforms, making it suitable for a variety of applications.
Wymagania wstępne
Zanim przejdziesz do tworzenia aplikacji konsolowej w języku C# do drukowania kodów QR, upewnij się, że spełniasz następujące wymagania wstępne:
- Visual Studio: Zainstaluj Microsoft Visual Studio, potężne zintegrowane środowisko programistyczne dla języka C#. Można go pobrać z oficjalnej strony internetowej.
- IronQR Library: This library is essential for generating QR codes. Install it using the NuGet Console or directly from the official IronQR NuGet website.
- IronPDF Library: IronPDF will be used to convert the generated QR code barcode images to a PDF. Install it using the same NuGet installation method.
- Biblioteka IronPrint: Na koniec zainstaluj bibliotekę IronPrint, aby umożliwić płynne drukowanie w aplikacji napisanej w języku C#.
Create a C# Console Application in Visual Studio
Wykonaj poniższe kroki, aby skonfigurować aplikację konsolową C# w Visual Studio:
- Otwórz Visual Studio i utwórz nową aplikację konsolową w języku C#
-
Skonfiguruj projekt w następujący sposób, a następnie kliknij "Dalej"

- Następnie, aby uzyskać dodatkowe informacje, wybierz .NET Framework i kliknij "Utwórz".
Zainstaluj niezbędne biblioteki za pomocą menedżera pakietów NuGet
Wykonaj poniższe kroki, aby zainstalować niezbędne biblioteki:
- Otwórz konsolę NuGet Package Manager lub NuGet Package Manager for Solutions, korzystając z menu Tools lub Solution Explorer w projekcie Visual Studio.
-
Zainstaluj bibliotekę IronQR QR Code:
-
Za pomocą konsoli menedżera pakietów NuGet dodaj następujące polecenie:
Install-Package IronPrint
- Using the Manage NuGet Packages for Solutions: In the browse tab of NuGet, search for the "IronQR", QR Code library and click Install.

-
-
Zainstaluj bibliotekę IronPDF PDF:
-
W konsoli menedżera pakietów NuGet wpisz następujące polecenie:
Install-Package IronPrint
- Using the Manage NuGet Packages for Solutions: In the browse tab of NuGet, search for the "IronPDF" library and click Install.

-
-
Zainstaluj bibliotekę IronPrint:
-
Za pomocą konsoli menedżera pakietów NuGet dodaj następujące polecenie:
Install-Package IronPrint
- Using the Manage NuGet Packages for Solutions: In the browse tab of NuGet, search for the "IronPrint" library and click install.

-
Steps to Create, Convert, and Print QR Codes
Let's break down the process of creating QR Codes, converting QR codes to PDF, and finally printing the output of the QR code generator in a step-by-step procedure using IronQR, IronPDF, and IronPrint.
Step-by-Step Procedure to Print C# QR Code
Krok 1: Biblioteki referencyjne
W pliku Program.cs na początku umieścimy odwołania do wymagańych bibliotek. Dzięki temu biblioteki są zainstalowane i gotowe do użycia.
// 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
Krok 2: Utwórz kod QR za pomocą IronQR
In this step, firstly, we are going to generate a QR code using IronQR and then save it as an image, as shown in the following code example:
// 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
W tym fragmencie kodu:
- A QR code is generated with the message "Hello IronPrint!" using the QrWriter class. The Write() method allows for the generation of a QR Code with a message or even with numeric data.
- The QR code is then saved as an AnyBitmap file, a universally compatible C# Bitmap class provided by IronSoftware.
- The QR code image is saved to the "assets" folder with the name "qr.png".
Oto wynikowy kod QR:

Krok 3: Konwersja obrazu QR do formatu PDF za pomocą IronPDF
Next, we are going to convert the QR code image to a PDF using IronPDF. Pliki PDF zachowują format dokumentu i nadają się do udostępniania oraz drukowania. W tym przypadku każdy plik graficzny zostanie umieszczony na osobnej stronie pliku 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")
W tym fragmencie kodu:
- Firstly, it enumerates through image files in the "assets" folder with extensions ".jpg" or ".png" and then proceeds to read QR codes from the directory.
- Utilizes ImageToPdfConverter.ImageToPdf() method from IronPDF to convert the images to a PDF named "composite.pdf".
Oto wynik:

Krok 4: Drukowanie pliku PDF za pomocą IronPrint
Finally, we will use IronPrint - a versatile Printing library to print the generated PDF with Printer Settings.
// 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
W tym fragmencie kodu źródłowego:
- Fetches available printer names using
Printer.GetPrinterNames(). - Sets the desired printer name (in this case, "Microsoft Print to PDF" to demonstrate the printing process). If not specified, it will use the default printer attached.
- Configures print settings, specifying paper size as A4 and setting margins. Margins struct have multiple overloads and it also presents fields like Top, Bottom, Left, and Right to set the margin as needed.
- Prints the PDF using a
Printer.Print(). The first argument is the path to the file to be printed and the second is theprinterSettingsif specified.
Oto wynik pliku PRINT. Pokazuje on, jak obraz zostanie wydrukowany:

Drukowanie na fizycznej drukarce jest znacznie łatwiejsze dzięki IronPrint. To get more control while printing, you can use ShowPrintDialog() method. For more information on how to Print efficiently, please visit this documentation page.
Advantages of IronPrint for Printing in C
IronPrint został zaprojektowany specjalnie jako potężna biblioteka drukowania dla aplikacji .NET. Unlike IronPDF, which is primarily focused on handling PDF-related tasks, and Microsoft printing, which is a general-purpose printing mechanism, IronPrint provides a dedicated set of classes and methods tailored for fine-grained control over the printing process.
Dzięki IronPrint programiści mogą korzystać z:
1. Asynchronous Printing
IronPrint zapewnia funkcje asynchroniczne, zapobiegając blokowaniu wątków przez operacje drukowania i zwiększając wydajność.
2. Versatile Printing
Specjalna klasa Printer w IronPrint umożliwia wszechstronne drukowanie różnych typów plików, oferując elastyczność wykraczającą poza standardowe drukowanie.
3. Cross-Platform Support
IronPrint supports multiple platforms, including Windows, Android, iOS, and macOS, making it suitable for diverse application environments.
4. Customizable Print Settings
Developers can finely control print settings, including paper size, orientation, DPI, number of copies, and more, through the PrintSettings class.
Wnioski
In conclusion, the combination of IronPrint, IronQR, and IronPDF provides a robust solution for creating, converting, and printing QR codes in C#. Zalety drukowania asynchronicznego, wszechstronne opcje drukowania oraz obsługa wielu platform sprawiają, że IronPrint jest cennym narzędziem dla programistów. By following the detailed steps outlined in this guide, you can generate QR code barcodes and also seamlessly integrate their printing into your C# applications, bridging the gap between physical and digital interactions.
IronPrint offers a free-trial starting from $799. Pobierz bibliotekę stąd i wypróbuj ją.
Często Zadawane Pytania
Jak wygenerować kod QR w C#?
Aby wygenerować kod QR w języku C#, należy użyć biblioteki IronQR. Kod QR można utworzyć, korzystając z metody QrWriter.Write() z biblioteki IronQR, aby wygenerować kod QR z żądanym komunikatem lub danymi.
Jak przekonwertować obraz kodu QR na plik PDF w języku C#?
Możesz przekonwertować obraz kodu QR na plik PDF w języku C#, korzystając z metody ImageToPdfConverter.ImageToPdf() biblioteki IronPDF. Ta funkcja pozwala bez wysiłku przekształcić pliki graficzne w dokument PDF.
Jakie kroki trzeba wykonać, żeby wydrukować kod QR w języku C#?
Kroki obejmują wygenerowanie kodu QR za pomocą IronQR, konwersję go do formatu PDF za pomocą IronPDF, a na koniec wydrukowanie dokumentu przy użyciu metody Printer.Print() biblioteki IronPrint z określonymi ustawieniami.
Czy w języku C# można dostosować ustawienia drukowania kodów QR?
Tak, można dostosować ustawienia drukowania za pomocą klasy PrintSettings biblioteki IronPrint. Pozwala ona określić rozmiar papieru, orientację, rozdzielczość (DPI), marginesy i inne ustawienia zgodnie z potrzebami drukowania.
Czy mogę używać tej metody drukowania w różnych systemach operacyjnych?
Tak, IronPrint obsługuje drukowanie międzyplatformowe, dzięki czemu jest kompatybilny z różnymi systemami operacyjnymi, w tym Windows, macOS, Android i iOS.
Jakie są zalety korzystania z IronPrint do drukowania kodów QR?
IronPrint oferuje takie korzyści, jak drukowanie asynchroniczne, obsługę różnych typów plików, kompatybilność międzyplatformową oraz możliwość dostosowania ustawień drukowania, co czyni go wydajnym wyborem do zadań drukowania w języku C#.
Jak rozwiązać problemy związane z drukowaniem kodów QR w języku C#?
Typowe kroki rozwiązywania problemów obejmują upewnienie się, że wszystkie niezbędne biblioteki są poprawnie zainstalowane, sprawdzenie ustawień drukowania w IronPrint oraz weryfikację procesów generowania kodów QR i konwersji do formatu PDF pod kątem ewentualnych błędów.
Czy istnieje możliwość wypróbowania IronPrint przed zakupem?
Tak, IronPrint oferuje bezpłatną wersję próbną, która pozwala zapoznać się z jego funkcjami i ocenić integrację z aplikacjami C# przed podjęciem decyzji o zakupie.



