Generating PDF with QR Code Using iTextSharp in ASP.NET

Creating PDFs dynamically in web applications is a common requirement. One popular library for generating PDFs in the .NET environment is iTextSharp. This powerful tool allows for extensive PDF manipulations, including the addition of QR codes. However, developers might encounter issues when working with QR codes, especially regarding format recognition. In this blog post, we'll discuss how to generate PDFs with QR codes using iTextSharp in ASP.NET and address a specific issue we recently encountered and resolved.

The Problem

Recently, while working on a project that required embedding QR codes in PDFs, we ran into an issue where the QR code format was unrecognized. This issue disrupted our workflow as the QR code, essential for our application, couldn't be read or processed correctly.

The Solution

After some investigation and troubleshooting, we identified that the problem stemmed from the way the QR code was being handled in the PDF. The solution was to convert the QR code into an image format before embedding it into the PDF. This approach ensured that the QR code retained its integrity and could be recognized correctly.

Here’s a step-by-step guide on how we implemented this solution using iTextSharp's BarcodeQRCode class.

Step-by-Step Guide

  1. Install iTextSharp


First, ensure that you have the iTextSharp library installed in your ASP.NET project. You can install it via NuGet Package Manager with the following command:


Install-Package itextsharp

  1. Generate the QR Code


Use the BarcodeQRCode class to generate the QR code. Convert this QR code into an image format to avoid recognition issues.
using iTextSharp.text;

using iTextSharp.text.pdf;

// URL or text to be encoded in the QR code

string caseQRUrl = “https://example.com/your-case-url”;

// Create the QR code

BarcodeQRCode qrcode = new BarcodeQRCode(caseQRUrl, 1, 1, null);

// Convert the QR code to an image

Image img = qrcode.GetImage();

  1. Create the PDF and Embed the QR Code


With the QR code image ready, you can now create a PDF document and embed the QR code into it.
// Create a Document object

Document document = new Document();

// Set the file path where the PDF will be saved

string pdfPath = Server.MapPath(“~/GeneratedPDFs/QRCodePDF.pdf”);

// Create a PdfWriter instance

PdfWriter.GetInstance(document, new FileStream(pdfPath, FileMode.Create));

// Open the document for writing

document.Open();

// Add the QR code image to the document

document.Add(img);

// Close the document

document.Close();

  1. Handle the Response


If you need to send the PDF as a response to the client's browser, you can modify the code to stream the PDF directly.
Response.ContentType = “application/pdf”;

Response.AddHeader(“Content-Disposition”, “attachment; filename=QRCodePDF.pdf”);

Response.OutputStream.Write(File.ReadAllBytes(pdfPath), 0, File.ReadAllBytes(pdfPath).Length);

Response.Flush();

Response.End();

Conclusion

Generating PDFs with QR codes in ASP.NET using iTextSharp can be straightforward. However, you might encounter issues with QR code recognition if not handled correctly. By converting the QR code to an image format using the BarcodeQRCode class, you can ensure that the QR code is embedded correctly and remains readable.

This approach resolved our issue and can help you avoid similar problems in your projects. Happy coding!

WordPress 5.5 Version Is Here. Things To Watch Out!

Fans of WordPress have a reason to celebrate with the recent release of popular PHP based Content Management System (Version 5.5). There are some major improvements the WordPress team has made into the latest release that makes it easier to manage a website. Project Gutenberg has contributed to a great editing experience with the latest 5.5 version. With the blocks, you can create awesome pages quickly. Each block is an independent space into which you can add additional components.

 

Things To Watch Out Before Migration

If you have an existing Website built with WordPress, please make sure that you take the latest backup before going for an upgrade to the newest version. Chances are that your plugins or theme may have issues with the new release. If something goes wrong, you should be able to revert to the old code. 

One of the major issues in the latest release is the lack of support for the jQuery Migrate library. Many themes and plugins depend on this library to avoid conflicts with jQuery versions. If you encounter such issues, contact the plugin theme developers for the latest release addressing those issues. It is a good idea to update all your plugins and themes to their latest version.

Overall, the 5.5 release will make WordPress one of the best Content Management Systems available in the market.

Facebook Campaign

Facebook Ads is a very attractive proposition to small businesses and companies with limited resources for marketing. There are several advantages of a Facebook campaign compared to the traditional advertising campaigns.

One of the best things about the Facebook campaign is that you can create and manage at one place, all aspects of a marketing campaign (the ad creation, the audience specification, budget, reports, etc.). The Facebook ad creation tool brings a lot of resources together to create an advertisement on the fly. Unlike their comparatively dry, text-based PPC cousins (ex. Google Ads), Facebook ads can be made powerfully visual. The very best Facebook ads blend in seamlessly with user generated content like posts, videos, images, and other visual contents.

Facebook Ads allow the users to target their audience based on geographic and demographic attributes. Facebook analytics (Pixel), provides useful data about how the advertisement performed on the target audience.  This info can help you to identify the reaches of your business and fine-tune your future campaigns for better results.

Facebook Ads are remarkably affordable. With a budget as low as $5/day, you will find your ad reaching thousands of  Facebook users in a day! From our experience. a Facebook campaign consistently yields more results than that of a Google ad. because it shows advertisements where people are mostly engaged like in Instagram, Facebook posts etc. 

Want to try out a Facebook Ad campaign? Contact Us.  We can set you one up quickly!