Barcode Extraction - Python 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.py

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

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales