Extract QR Codes from PDF Files Using LEADTOOLS

Posted on 2024-01-24 Halah Villalobos

Barcode

QR codes have solidified themselves as an integral part of modern information exchange, offering a quick and efficient way to store and retrieve data. With LEADTOOLS, you can extract over 100 barcode types from any of our 165+ supported file formats! In this blog post, we will specifically show how to extract QR codes from a PDF file in C# using the LEADTOOLS Barcode SDK and then save the extracted codes as images.

Extracting QR codes from PDFs can be useful in many scenarios where the PDF documents containing QR codes need further analysis, such as automating document-heavy workflows and processes, inventory management, quality control, etc. Automating this process not only will save time but also limit manual intervention by streamlining workflows and allowing your users to process data swiftly. For those of you who know LEADTOOLS, we're all about helping you build intelligent, user-friendly apps quickly and easily!

Let's Get Coding!

Developers leveraging our comprehensive barcode toolkit gain access to over 100 supported barcode types including all major 1D and 2D barcodes. To extract a QR code from a file, you simply grab the BarcodeData Bounds to locate where the barcode is found on the page and send the coordinates to your CropCommand. From there, you are able to detect and extract the barcode data and print the information results to the console to view.

How to Extract a QR Code from a PDF in C#

Let's take a quick look at the code:


using (RasterCodecs codecs = new RasterCodecs())
{
   CropCommand crop = new CropCommand();
   RasterImage image = codecs.Load(@"path to input file");
   BarcodeEngine barcodeEngineInstance = new BarcodeEngine();
   BarcodeData[] dataArray = barcodeEngineInstance.Reader.ReadBarcodes(image, LeadRect.Empty, 0, null);
   
   StringBuilder sb = new StringBuilder();
   sb.AppendFormat("{0} barcode(s) found", dataArray.Length); 
   sb.AppendLine(); 

   for (int i = 0; i < dataArray.Length; i++)
   {
      RasterImage cloneImage = image.Clone();
      BarcodeData data = dataArray[i];
      crop.Rectangle = data.Bounds; //get bounds of found barcode
      crop.Run(cloneImage); // extract just the barcode
      codecs.Save(cloneImage, @"Output path to save into file", RasterImageFormat.Png, 0); // save to output file
      
      sb.AppendFormat("Symbology: {0}, Location: {1}, Data: {2}", data.Symbology.ToString(), data.Bounds.ToString(), data.Value); //append the extracted barcode information to the StringBuilder
      sb.AppendLine();
   }
   Console.WriteLine(sb.ToString()); //print the results of the extracted barcodes
}

And that’s it! A simple and effective way to extract QR codes from PDF files and export to a specified location. With an added line to trigger the action, you now have an automated solution set to grab each QR code and export automatically to allow time for more complex data analysis and processing. To expand this application to be used in any niche within your organization, be sure to utilize our extensive tutorials and robust documentation for additional complex technology and functions.

Try for Yourself with Our Free 60-Day Evaluation SDK!

We offer a FREE 60-day evaluation SDK that gives you access to not only our comprehensive barcode technology, but our entire suite of award-winning LEADTOOLS libraries, including: OCR/ICR, Document Viewer, Forms Recognition and Processing, and so much more! Best of all, we offer free technical support for all LEADTOOLS users via live chat and email to help you along your application development journey.

Be sure to stay tuned for more in-depth blogs that showcase how you too can decrease solution costs and time-to-revenue by leveraging LEADTOOLS!

LEADTOOLS Blog

LEADTOOLS Powered by Apryse,the Market Leading PDF SDK,All Rights Reserved