USING IRONPDF

How to Convert QR Code to PDF

Updated November 12, 2024
Share:

Quick Response codes, known as QR codes, are two-dimensional barcodes capable of storing various information, including URLs, contact details, or plain text. They are widely used in marketing, payment systems, inventory management, printing, and more. As their popularity grows, developers increasingly need to handle QR codes within applications, such as reading and embedding them into documents like PDFs as well as ways to generate QR code and QR code generation.

In this article, I’ll show you how easy it is to create and convert a QR Code image to PDF using the IronQR and IronPDF libraries.

How to Convert QR Code to PDF

  1. Install C# QR code and PDF library to create and convert QR Code to PDF

  2. Generate a simple QR object using QrWriter.Write method from IronQR

  3. Save the QR Code object as a Bitmap

  4. Save the QR Code Bitmap as a File using `SaveAs` method

  5. Set the QR code image path in a variable

  6. Use File.Exists method to check if a QR code Image exists before converting

  7. Utilize ImageToPdfConverter.ImageToPdf method from IronPDF to load the Image and convert it to a PDF object

  8. Save the PDF using SaveAs method

Introduction to IronQR

IronQR is an easy-to-use C# library by IronSoftware that allows developers to generate static QR codes, style, and read QR codes in .NET applications. Its simplicity and robust features make it an ideal tool for integrating QR code functionality into desktop, web, and mobile apps. One of its key strengths is the ability to handle QR codes across a wide range of platforms, including Windows, Linux, macOS, Android, iOS, and cloud environments like Azure and AWS.

The IronQR library provides a robust solution for generating QR codes, allowing developers to create static and dynamic QR codes for various applications easily. With its advanced features, IronQR facilitates reading QR codes and integrates seamlessly with PDF documents, enabling users to generate, print, and embed QR codes directly into their PDFs.

Features of IronQR

IronQR offers a range of features that prioritize accuracy, speed, and ease of use:

  • Cross-platform compatibility with .NET (Core, Standard, Framework), supporting various environments, including web, desktop, and mobile apps.

  • Machine learning-powered QR code detection for reading even complex QR codes.

  • Supports various image formats (jpg, png, gif, bmp, etc.).

  • Advanced customization options for styling QR codes, such as resizing, adding logos, and adjusting error correction levels.

  • Output formats include images, streams, and PDF stamping.

To learn more about IronQR and its exciting features, please follow this documentation page.

Create a Visual Studio Project

To begin, let's create a new project in Visual Studio:

  1. Open Visual Studio and click on Create a new project.

    Qr Code To Pdf Tutorial 1 related to Create a Visual Studio Project

  2. Select Console App (.NET C#) project type.

    Qr Code To Pdf Tutorial 2 related to Create a Visual Studio Project

  3. Choose a name for your project (e.g., QRCodeToPDF) and set the location where it should be saved.

    Qr Code To Pdf Tutorial 3 related to Create a Visual Studio Project

  4. In Additional Information, select the latest version of .NET Framework. IronPDF supports the latest version of .NET.

    Qr Code To Pdf Tutorial 4 related to Create a Visual Studio Project

  5. Click Create.

Install IronQR and IronPDF Library via NuGet Package Manager

To work with IronQR and IronPDF, you need to download and install the packages using the NuGet Package Manager:

  1. In Microsoft Visual Studio, right-click on your project in the Solution Explorer.

  2. Select Manage NuGet Packages.

  3. In the Browse tab, search for IronQR.

    Qr Code To Pdf Tutorial 5 related to Install IronQR and IronPDF Library via NuGet Package Manager

  4. Select the package from the list and click Install.

  5. Accept the license terms to complete the installation.

  6. Similarly, search for IronPDF and install it.

    Qr Code To Pdf Tutorial 6 related to Install IronQR and IronPDF Library via NuGet Package Manager

Generate a QR Code using IronQR Library

To convert a QR code image into a PDF, we'll first need a QR code image. You can use any QR code generator library or online tool to create a QR code. IronSoftware provides a dedicated QR code library named "IronQR" to create QR codes, and I'm going to use it here to generate a simple QR code.

The following code example will allow us to create a QR code with the text Hello World:

using IronQr;
using IronSoftware.Drawing;

License.LicenseKey = “YOUR-LICENSE-KEY-HERE”;

// Create a QR Code object
QrCode myQr = QrWriter.Write("hello world");

// Save QR Code as a Bitmap
AnyBitmap qrImage = myQr.Save();

// Save QR Code Bitmap as File
qrImage.SaveAs("qr.png");
using IronQr;
using IronSoftware.Drawing;

License.LicenseKey = “YOUR-LICENSE-KEY-HERE”;

// Create a QR Code object
QrCode myQr = QrWriter.Write("hello world");

// Save QR Code as a Bitmap
AnyBitmap qrImage = myQr.Save();

// Save QR Code Bitmap as File
qrImage.SaveAs("qr.png");
Imports IronQr
Imports IronSoftware.Drawing

'INSTANT VB TODO TASK: The following line uses invalid syntax:
'License.LicenseKey = "YOUR-LICENSE-KEY-HERE”; QrCode myQr = QrWriter.Write("hello world"); AnyBitmap qrImage = myQr.Save(); qrImage.SaveAs("qr.png");
VB   C#

Code Explanation

  • QrWriter.Write("hello world"): This method generates a QR code that encodes the string "hello world." The result is an instance of the QrCode class, representing the generated QR code.

  • myQr.Save(): This method converts the QR code object into a bitmap image format. The Save() method returns an instance of AnyBitmap, a flexible image representation supporting various formats.

  • AnyBitmap qrImage: This variable holds the bitmap image of the generated QR code.

  • qrImage.SaveAs("qr.png"): This method saves the QR code bitmap image to a file named qr.png in the current working directory. The file format is determined by the file extension, in this case, PNG.

After running the application, we get our QR code as follows:

Qr Code To Pdf Tutorial 7 related to Code Explanation

We will load this QR Code image and use the ImageToPdfConverter class provided by IronPDF

Introduction to IronPDF

IronPDF is a robust .NET C# library from IronSoftware that easily creates, manipulates, and converts PDF documents in .NET applications. With IronPDF, developers can easily embed images (including QR codes) into a PDF document template, making it perfect for tasks like converting QR code images into a document-ready PDF.

IronPDF provides HTML to PDF conversion, which allows developers to directly embed QR code images in an HTML template and then generate PDF documents seamlessly. The embedded QR code formatting is preserved in the document, allowing error-free scanning of the QR codes.

Qr Code To Pdf Tutorial 8 related to Introduction to IronPDF

Features of IronPDF

IronPDF offers a wide range of PDF manipulation tools, including:

  • Cross-platform compatibility: Supports .NET Core, .NET Framework, and .NET Standard, running on Windows, Linux, macOS, docker, Azure, and AWS.

  • Image-to-PDF conversion: Effortlessly converts image files, such as JPEGs or PNGs, Bitmap into PDFs.

  • HTML and CSS support: For creating customizable PDFs from web pages.

  • Security features: Includes password protection and encryption for securing sensitive PDF documents.

  • PDF editing capabilities: Merging, splitting, and adding watermarks are made simple with IronPDF.

To learn more about IronPDF's exciting features, please follow this documentation page.

Convert the QR Code Image to a PDF File

Now, with everything set up perfectly, the following code example will help you convert a QR code image to a PDF using IronPDF:

using IronPdf;
using System.IO;
using System.Linq;

License.LicenseKey = “YOUR-LICENSE-KEY-HERE”;

var qrImagePath = "assets/sample_qr_code.png";
// To ensure that the image file exists before proceeding
if (File.Exists(qrImagePath))
{
    // Convert the image to a PDF and save it
    ImageToPdfConverter.ImageToPdf(new[] { qrImagePath }).SaveAs("QRCodeImageToPDF.pdf");
    Console.WriteLine("QR Code image has been successfully converted to a PDF.");
}
else
{
    Console.WriteLine("QR Code image not found. Please check the file path.");
}
using IronPdf;
using System.IO;
using System.Linq;

License.LicenseKey = “YOUR-LICENSE-KEY-HERE”;

var qrImagePath = "assets/sample_qr_code.png";
// To ensure that the image file exists before proceeding
if (File.Exists(qrImagePath))
{
    // Convert the image to a PDF and save it
    ImageToPdfConverter.ImageToPdf(new[] { qrImagePath }).SaveAs("QRCodeImageToPDF.pdf");
    Console.WriteLine("QR Code image has been successfully converted to a PDF.");
}
else
{
    Console.WriteLine("QR Code image not found. Please check the file path.");
}
Imports IronPdf
Imports System.IO
Imports System.Linq

'INSTANT VB TODO TASK: The following line uses invalid syntax:
'License.LicenseKey = "YOUR-LICENSE-KEY-HERE”; var qrImagePath = "assets/sample_qr_code.png"; if (File.Exists(qrImagePath)) { ImageToPdfConverter.ImageToPdf(new[] { qrImagePath }).SaveAs("QRCodeImageToPDF.pdf"); Console.WriteLine("QR Code image has been successfully converted @to a PDF."); } else { Console.WriteLine("QR Code image @not found.Please check the file path."); }
VB   C#

Code Explanation:

  • File.Exists(qrImagePath): Verifies if the QR code image exists at the specified path before proceeding.

  • ImageToPdfConverter.ImageToPdf(new[] { qrImagePath }): Converts the QR code image to a PDF using IronPDF’s image-to-PDF conversion method.

  • SaveAs("QRCodeImageToPDF.pdf"): Saves the generated PDF as QRCodeImageToPDF.pdf.

To convert multiple QR code images into a single PDF, please visit this Images to PDF page. For more code samples, please visit this code example page here.

Run the Application

Now that the code is in place, it's time to run the application and see the conversion in action. Follow these steps:

  1. Ensure that the QR code image (sample_qr_code.jpg) is correctly placed in the specified folder (e.g., the assets folder).

  2. Build and run the project in Visual Studio by pressing F5 or clicking Start.

  3. The application will convert the QR code image into a PDF if the image exists at the specified location.

  4. The generated PDF will be saved in the root of your project directory with the filename QRCodeImageToPDF.pdf.

  5. Check the PDF file to ensure the QR code image has been successfully embedded.

You should now have a PDF containing your QR code image, which can be shared, printed, or archived. Here is the output PDF with the QR code image we used:

Code Output

Conclusion

Using IronQR and IronPDF, creating and converting a QR code image to a PDF is simple and efficient. By following the steps outlined above, you can easily create a QR code image, convert it, and save it as a PDF in just a few lines of code. Whether for business or personal use, this approach ensures that your QR code is document-ready in PDF format and can be shared over the internet without any data or pixel loss.

IronPDF provides a free trial so you can explore its features and capabilities for yourself. For those ready to unlock the full potential of the library, licenses start at $749, offering comprehensive access to all functionalities. Don't miss the opportunity to enhance your PDF generation—try IronPDF today by downloading it!

< PREVIOUS
How to Add Images to PDF in VB .NET
NEXT >
How to Read PDF Table in C#

Ready to get started? Version: 2024.12 just released

Free NuGet Download Total downloads: 11,622,374 View Licenses >