AVAILABLE NOW: Spring 2025 Release
By Apryse | 2025 Mar 31
5 min
Tags
pdf extraction
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 Apryse, 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 Apryse 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 Apryse, we're all about helping you build intelligent, user-friendly apps quickly and easily!
Currently, Apryse barcode extraction operates server-side only. To get started with Server SDK, visit our documentation page including instructions for different platforms and frameworks.
Let's take a quick look at the code:
namespace Program
{
class Program
{
static void Main(string[] args)
{
PDFNet.Initialize("Place license key here");
PDFNet.AddResourceSearchPath("Path to BarcodeModule Lib");
if (!BarcodeModule.IsModuleAvailable())
{
Console.WriteLine("");
Console.WriteLine("Unable to run BarcodeTest: Apryse SDK Barcode Module not available.");
Console.WriteLine("---------------------------------------------------------------");
Console.WriteLine("The Barcode Module is an optional add-on, available for download");
Console.WriteLine("at https://dev.apryse.com/. If you have already downloaded this");
Console.WriteLine("module, ensure that the SDK is able to find the required files");
Console.WriteLine("using the PDFNet.AddResourceSearchPath() function.");
Console.WriteLine("");
return;
}
ExtractBarcodes();
}
static void ExtractBarcodes()
{
// Relative path to the folder containing test files.
string inputPath = "../TestFiles/Barcode/";
string outputPath = "../TestFiles/Barcode/output/";
using (PDFDoc doc = new PDFDoc(inputPath + "barcodes.pdf"))
{
// Extract the barcodes as a JSON String
string json = BarcodeModule.ExtractBarcodesAsString(doc);
PDFDoc newDoc = new PDFDoc();
JsonSerializerOptions options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull
};
Root? output = JsonSerializer.Deserialize<Root>(json, options);
int currentPage = 1;
foreach (Pages pages in output.Pages)
{
foreach (BarcodeData data in pages.Barcodes)
{
List<double> rect = data.Rect;
if (rect != null)
{
Page page = doc.GetPage(currentPage);
double pageHeight = page.GetCropBox().Height();
// add padding to improve the look of the barcode pages
double padding = 10;
// convert coordinates from top-left origin to bottom-left origin and add padding
double x1 = rect.ElementAt(0) - padding;
double y1 = pageHeight - rect.ElementAt(3) - padding;
double x2 = rect.ElementAt(2) + padding;
double y2 = pageHeight - rect.ElementAt(1) + padding;
Rect convertedRect = new Rect(x1, y1, x2, y2);
ArrayList pageList = new ArrayList { page };
doc.PagePushBack(page);
Page clonedPage = doc.GetPage(doc.GetPageCount());
clonedPage.SetCropBox(convertedRect);
newDoc.PagePushBack(clonedPage);
}
}
currentPage++;
}
newDoc.Save(outputPath + "cropped.pdf", pdftron.SDF.SDFDoc.SaveOptions.e_linearized);
}
}
}
//Classes needed to deserialize the json output into
public class BarcodeData
{
public double Angle { get; set; }
public List<double> Rect { get; set; }
public string Text { get; set; }
public string Type { get; set; }
public List<double> Vertices { get; set; }
}
public class Pages
{
public List<BarcodeData> Barcodes { get; set; }
}
public class Root
{
public List<Pages> Pages { get; set; }
}
}
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.
We offer a trial of all our SDKs that gives you access to not only our comprehensive barcode technology, but also all the core features of our SDKs. You can also find showcase demos of Apryse technology in our documentation.
If you have any questions, feel free to contact sales or reach out to us on discord!
Tags
pdf extraction
barcode
Apryse
Share this post
PRODUCTS
Platform Integrations
End User Applications
Popular Content