Barcode Extraction - Ruby 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.rb

1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5require '../../../PDFNetC/Lib/PDFNetRuby'
6include PDFNetRuby
7require '../../LicenseKey/RUBY/LicenseKey'
8$stdout.sync = true
9# ---------------------------------------------------------------------------------------
10# The Barcode Module is an optional PDFNet add-on that can be used to extract
11# various types of barcodes from PDF documents.
12#
13# The Apryse SDK Barcode Module can be downloaded from http://dev.apryse.com/
14# --------------------------------------------------------------------------------------
15# Relative path to the folder containing test files.
16$input_path = "../../TestFiles/Barcode/"
17$output_path = "../../TestFiles/Output/"
18def main()
19 # The first step in every application using PDFNet is to initialize the
20 # library and set the path to common PDF resources. The library is usually
21 # initialized only once, but calling Initialize() multiple times is also fine.
22 PDFNet.Initialize(PDFTronLicense.Key)
23
24 # The location of the Barcode Module
25 PDFNet.AddResourceSearchPath("../../../PDFNetC/Lib/");
26
27 begin
28 if !BarcodeModule.IsModuleAvailable
29 puts 'Unable to run BarcodeTest: Apryse SDK Barcode Module not available.'
30 puts '---------------------------------------------------------------'
31 puts 'The Barcode Module is an optional add-on, available for download'
32 puts 'at https://dev.apryse.com/. If you have already downloaded this'
33 puts 'module, ensure that the SDK is able to find the required files'
34 puts 'using the PDFNet.AddResourceSearchPath() function.'
35 else
36 # Example 1) Detect and extract all barcodes from a PDF document into a JSON file
37 # --------------------------------------------------------------------------------
38 puts "Example 1: extracting barcodes from barcodes.pdf to barcodes.json"
39 # A) Open the .pdf document
40 doc = PDFDoc.new($input_path + "barcodes.pdf")
41
42 # B) Detect PDF barcodes with the default options
43 BarcodeModule.ExtractBarcodes(doc, $output_path + "barcodes.json")
44 doc.Close
45 # Example 2) Limit barcode extraction to a range of pages, and retrieve the JSON into a
46 # local string variable, which is then written to a file in a separate function call
47 # --------------------------------------------------------------------------------
48 puts "Example 2: extracting barcodes from pages 1-2 to barcodes_from_pages_1-2.json"
49 # A) Open the .pdf document
50 doc = PDFDoc.new($input_path + "barcodes.pdf")
51 # B) Detect PDF barcodes with custom options
52 options = BarcodeOptions.new
53 # Convert only the first two pages
54 options.SetPages("1-2")
55 json = BarcodeModule.ExtractBarcodesAsString(doc, options)
56 # C) Save JSON to file
57 File.open($output_path + "barcodes_from_pages_1-2.json", 'w') { |file| file.write(json) }
58 doc.Close
59 # Example 3) Narrow down barcode types and allow the detection of both horizontal
60 # and vertical barcodes
61 # --------------------------------------------------------------------------------
62 puts "Example 3: extracting basic horizontal and vertical barcodes"
63 # A) Open the .pdf document
64 doc = PDFDoc.new($input_path + "barcodes.pdf")
65 # B) Detect only basic 1D barcodes, both horizontal and vertical
66 options = BarcodeOptions.new
67 # Limit extraction to basic 1D barcode types, such as EAN 13, EAN 8, UPCA, UPCE,
68 # Code 3 of 9, Code 128, Code 2 of 5, Code 93, Code 11 and GS1 Databar.
69 options.SetBarcodeSearchTypes(BarcodeOptions::E_linear)
70 # Search for barcodes oriented horizontally and vertically
71 options.SetBarcodeOrientations(
72 BarcodeOptions::E_horizontal |
73 BarcodeOptions::E_vertical)
74 BarcodeModule.ExtractBarcodes(doc, $output_path + "barcodes_1D.json", options)
75 doc.Close
76 end
77 rescue => error
78 puts "Unable to extract barcodes, error: " + error.message
79 end
80 PDFNet.Terminate
81 puts "Done."
82end
83main()
84

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales