Barcode Extraction - Java 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.java

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

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales