Barcode Extraction - Go 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.go

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult LICENSE.txt regarding license information.
4//---------------------------------------------------------------------------------------
5package main
6import (
7 "fmt"
8 "testing"
9 "os"
10 "flag"
11 . "github.com/pdftron/pdftron-go/v2"
12)
13var licenseKey string
14var modulePath string
15func init() {
16 flag.StringVar(&licenseKey, "license", "", "License key for Apryse SDK")
17 flag.StringVar(&modulePath, "modulePath", "", "Path for downloaded modules")
18}
19//---------------------------------------------------------------------------------------
20// The Barcode Module is an optional PDFNet add-on that can be used to extract
21// various types of barcodes from PDF documents.
22//
23// The Apryse SDK Barcode Module can be downloaded from http://dev.apryse.com/
24//---------------------------------------------------------------------------------------
25// Relative path to the folder containing test files.
26var inputPath = "../TestFiles/Barcode/"
27var outputPath = "../TestFiles/Output/"
28//---------------------------------------------------------------------------------------
29func WriteTextToFile(outputFile string, text string) {
30 f, err := os.Create(outputFile)
31 if err != nil {
32 fmt.Println(err)
33 }
34 defer f.Close()
35 _, err2 := f.WriteString(text)
36 if err2 != nil {
37 fmt.Println(err2)
38 }
39}
40//---------------------------------------------------------------------------------------
41func TestBarcode(t *testing.T) {
42 // The first step in every application using PDFNet is to initialize the
43 // library and set the path to common PDF resources. The library is usually
44 // initialized only once, but calling Initialize() multiple times is also fine.
45 PDFNetInitialize(licenseKey)
46 // The location of the Barcode Module
47 PDFNetAddResourceSearchPath(modulePath)
48 if ! BarcodeModuleIsModuleAvailable() {
49 fmt.Println("Unable to run BarcodeTest: Apryse SDK Barcode Module not available.\n" +
50 "---------------------------------------------------------------\n" +
51 "The Barcode Module is an optional add-on, available for download\n" +
52 "at https://dev.apryse.com/. If you have already downloaded this\n" +
53 "module, ensure that the SDK is able to find the required files\n" +
54 "using the PDFNetAddResourceSearchPath() function.")
55 } else {
56 // Example 1) Detect and extract all barcodes from a PDF document into a JSON file
57 // --------------------------------------------------------------------------------
58 fmt.Println("Example 1: extracting barcodes from barcodes.pdf to barcodes.json")
59 // A) Open the .pdf document
60 doc := NewPDFDoc(inputPath + "barcodes.pdf")
61 // B) Detect PDF barcodes with the default options
62 BarcodeModuleExtractBarcodes(doc, outputPath + "barcodes.json")
63 doc.Close()
64 // Example 2) Limit barcode extraction to a range of pages, and retrieve the JSON into a
65 // local string variable, which is then written to a file in a separate function call
66 // --------------------------------------------------------------------------------
67 fmt.Println("Example 2: extracting barcodes from pages 1-2 to barcodes_from_pages_1-2.json")
68 // A) Open the .pdf document
69 doc = NewPDFDoc(inputPath + "barcodes.pdf")
70 // B) Detect PDF barcodes with custom options
71 options := NewBarcodeOptions()
72 // Convert only the first two pages
73 options.SetPages("1-2")
74 json := BarcodeModuleExtractBarcodesAsString(doc, options)
75 // C) Save JSON to file
76 WriteTextToFile(outputPath + "barcodes_from_pages_1-2.json", json)
77 doc.Close()
78 // Example 3) Narrow down barcode types and allow the detection of both horizontal
79 // and vertical barcodes
80 // --------------------------------------------------------------------------------
81 fmt.Println("Example 3: extracting basic horizontal and vertical barcodes")
82 // A) Open the .pdf document
83 doc = NewPDFDoc(inputPath + "barcodes.pdf")
84 // B) Detect only basic 1D barcodes, both horizontal and vertical
85 options = NewBarcodeOptions()
86 // Limit extraction to basic 1D barcode types, such as EAN 13, EAN 8, UPCA, UPCE,
87 // Code 3 of 9, Code 128, Code 2 of 5, Code 93, Code 11 and GS1 Databar.
88 options.SetBarcodeSearchTypes(uint(BarcodeOptionsE_linear))
89 // Search for barcodes oriented horizontally and vertically
90 options.SetBarcodeOrientations(
91 uint(BarcodeOptionsE_horizontal) |
92 uint(BarcodeOptionsE_vertical))
93 BarcodeModuleExtractBarcodes(doc, outputPath + "barcodes_1D.json", options)
94 doc.Close()
95 }
96 PDFNetTerminate()
97 fmt.Println("Done.")
98}
99

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales