Barcode

This sample shows how to use the Apryse Barcode Module to detect and extract barcodes from PDF documents.

BarcodeTest.cpp

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

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales