Barcode Extraction - C# (.Net) Sample Code

This sample shows how to use the Apryse Barcode Module to detect and extract barcodes from PDF documents; provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby, Go and VB.

BarcodeTest.cs

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5using System;
6using pdftron;
7using pdftron.Common;
8using pdftron.PDF;
9namespace BarcodeTestCS
10{
11
12 /// <summary>
13 //---------------------------------------------------------------------------------------
14 // The Barcode Module is an optional PDFNet add-on that can be used to extract
15 // various types of barcodes from PDF documents.
16 //
17 // The Apryse SDK Barcode Module can be downloaded from https://dev.apryse.com/
18 //---------------------------------------------------------------------------------------
19 /// </summary>
20 class Class1
21 {
22 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
23 static Class1() {}
24
25 /// <summary>
26 /// The main entry point for the application.
27 /// </summary>
28 static void Main(string[] args)
29 {
30 // The first step in every application using PDFNet is to initialize the
31 // library and set the path to common PDF resources. The library is usually
32 // initialized only once, but calling Initialize() multiple times is also fine.
33 PDFNet.Initialize(PDFTronLicense.Key);
34 // Can optionally set path to the Barcode module
35 PDFNet.AddResourceSearchPath("../../../Lib/");
36 // Test if the add-on is installed
37 if (!BarcodeModule.IsModuleAvailable())
38 {
39 Console.WriteLine("");
40 Console.WriteLine("Unable to run BarcodeTest: Apryse SDK Barcode Module not available.");
41 Console.WriteLine("---------------------------------------------------------------");
42 Console.WriteLine("The Barcode Module is an optional add-on, available for download");
43 Console.WriteLine("at https://dev.apryse.com/. If you have already downloaded this");
44 Console.WriteLine("module, ensure that the SDK is able to find the required files");
45 Console.WriteLine("using the PDFNet.AddResourceSearchPath() function.");
46 Console.WriteLine("");
47 return;
48 }
49 // Relative path to the folder containing test files.
50 string input_path = "../../TestFiles/Barcode/";
51 string output_path = "../../TestFiles/Output/";
52 //--------------------------------------------------------------------------------
53 // Example 1) Detect and extract all barcodes from a PDF document into a JSON file
54 try
55 {
56 Console.WriteLine("Example 1: extracting barcodes from barcodes.pdf to barcodes.json");
57 // A) Open the .pdf document
58 using (PDFDoc doc = new PDFDoc(input_path + "barcodes.pdf"))
59 {
60 // B) Detect PDF barcodes with the default options
61 BarcodeModule.ExtractBarcodes(doc, output_path + "barcodes.json");
62 }
63 }
64 catch (PDFNetException e)
65 {
66 Console.WriteLine(e.Message);
67 }
68 //--------------------------------------------------------------------------------
69 // Example 2) Limit barcode extraction to a range of pages, and retrieve the JSON into a
70 // local string variable, which is then written to a file in a separate function call
71 try
72 {
73 Console.WriteLine("Example 2: extracting barcodes from pages 1-2 to barcodes_from_pages_1-2.json");
74 // A) Open the .pdf document
75 using (PDFDoc doc = new PDFDoc(input_path + "barcodes.pdf"))
76 {
77 // B) Detect PDF barcodes with custom options
78 BarcodeOptions options = new BarcodeOptions();
79 // Convert only the first two pages
80 options.SetPages("1-2");
81 string json = BarcodeModule.ExtractBarcodesAsString(doc, options);
82 // C) Save JSON to file
83 System.IO.File.WriteAllText(output_path + "barcodes_from_pages_1-2.json", json);
84 }
85 }
86 catch (PDFNetException e)
87 {
88 Console.WriteLine(e.Message);
89 }
90 //--------------------------------------------------------------------------------
91 // Example 3) Narrow down barcode types and allow the detection of both horizontal
92 // and vertical barcodes
93 try
94 {
95 Console.WriteLine("Example 3: extracting basic horizontal and vertical barcodes");
96 // A) Open the .pdf document
97 using (PDFDoc doc = new PDFDoc(input_path + "barcodes.pdf"))
98 {
99 // B) Detect only basic 1D barcodes, both horizontal and vertical
100 BarcodeOptions options = new BarcodeOptions();
101 // Limit extraction to basic 1D barcode types, such as EAN 13, EAN 8, UPCA, UPCE,
102 // Code 3 of 9, Code 128, Code 2 of 5, Code 93, Code 11 and GS1 Databar.
103 options.SetBarcodeSearchTypes(BarcodeOptions.BarcodeTypeGroup.e_linear);
104 // Search for barcodes oriented horizontally and vertically
105 options.SetBarcodeOrientations(
106 BarcodeOptions.BarcodeOrientation.e_horizontal |
107 BarcodeOptions.BarcodeOrientation.e_vertical);
108 BarcodeModule.ExtractBarcodes(doc, output_path + "barcodes_1D.json", options);
109 }
110 }
111 catch (PDFNetException e)
112 {
113 Console.WriteLine(e.Message);
114 }
115 Console.WriteLine("Done.");
116 PDFNet.Terminate();
117 }
118 }
119}
120

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales