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
BarcodeTest.cs
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5using System;
6using pdftron;
7using pdftron.Common;
8using pdftron.PDF;
9namespace BarcodeTestCS
10{
11
12 /// <summary>
13 //---------------------------------------------------------------------------------------
14 // The Barcode Module is an optional PDFNet add-on that can be used to extract
15 // various types of barcodes from PDF documents.
16 //
17 // The Apryse SDK Barcode Module can be downloaded from https://dev.apryse.com/
18 //---------------------------------------------------------------------------------------
19 /// </summary>
20 class Class1
21 {
22 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
23 static Class1() {}
24
25 /// <summary>
26 /// The main entry point for the application.
27 /// </summary>
28 static void Main(string[] args)
29 {
30 // The first step in every application using PDFNet is to initialize the
31 // library and set the path to common PDF resources. The library is usually
32 // initialized only once, but calling Initialize() multiple times is also fine.
33 PDFNet.Initialize(PDFTronLicense.Key);
34 // Can optionally set path to the Barcode module
35 PDFNet.AddResourceSearchPath("../../../Lib/");
36 // Test if the add-on is installed
37 if (!BarcodeModule.IsModuleAvailable())
38 {
39 Console.WriteLine("");
40 Console.WriteLine("Unable to run BarcodeTest: Apryse SDK Barcode Module not available.");
41 Console.WriteLine("---------------------------------------------------------------");
42 Console.WriteLine("The Barcode Module is an optional add-on, available for download");
43 Console.WriteLine("at https://dev.apryse.com/. If you have already downloaded this");
44 Console.WriteLine("module, ensure that the SDK is able to find the required files");
45 Console.WriteLine("using the PDFNet.AddResourceSearchPath() function.");
46 Console.WriteLine("");
47 return;
48 }
49 // Relative path to the folder containing test files.
50 string input_path = "../../TestFiles/Barcode/";
51 string output_path = "../../TestFiles/Output/";
52 //--------------------------------------------------------------------------------
53 // Example 1) Detect and extract all barcodes from a PDF document into a JSON file
54 try
55 {
56 Console.WriteLine("Example 1: extracting barcodes from barcodes.pdf to barcodes.json");
57 // A) Open the .pdf document
58 using (PDFDoc doc = new PDFDoc(input_path + "barcodes.pdf"))
59 {
60 // B) Detect PDF barcodes with the default options
61 BarcodeModule.ExtractBarcodes(doc, output_path + "barcodes.json");
62 }
63 }
64 catch (PDFNetException e)
65 {
66 Console.WriteLine(e.Message);
67 }
68 //--------------------------------------------------------------------------------
69 // Example 2) Limit barcode extraction to a range of pages, and retrieve the JSON into a
70 // local string variable, which is then written to a file in a separate function call
71 try
72 {
73 Console.WriteLine("Example 2: extracting barcodes from pages 1-2 to barcodes_from_pages_1-2.json");
74 // A) Open the .pdf document
75 using (PDFDoc doc = new PDFDoc(input_path + "barcodes.pdf"))
76 {
77 // B) Detect PDF barcodes with custom options
78 BarcodeOptions options = new BarcodeOptions();
79 // Convert only the first two pages
80 options.SetPages("1-2");
81 string json = BarcodeModule.ExtractBarcodesAsString(doc, options);
82 // C) Save JSON to file
83 System.IO.File.WriteAllText(output_path + "barcodes_from_pages_1-2.json", json);
84 }
85 }
86 catch (PDFNetException e)
87 {
88 Console.WriteLine(e.Message);
89 }
90 //--------------------------------------------------------------------------------
91 // Example 3) Narrow down barcode types and allow the detection of both horizontal
92 // and vertical barcodes
93 try
94 {
95 Console.WriteLine("Example 3: extracting basic horizontal and vertical barcodes");
96 // A) Open the .pdf document
97 using (PDFDoc doc = new PDFDoc(input_path + "barcodes.pdf"))
98 {
99 // B) Detect only basic 1D barcodes, both horizontal and vertical
100 BarcodeOptions options = new BarcodeOptions();
101 // Limit extraction to basic 1D barcode types, such as EAN 13, EAN 8, UPCA, UPCE,
102 // Code 3 of 9, Code 128, Code 2 of 5, Code 93, Code 11 and GS1 Databar.
103 options.SetBarcodeSearchTypes(BarcodeOptions.BarcodeTypeGroup.e_linear);
104 // Search for barcodes oriented horizontally and vertically
105 options.SetBarcodeOrientations(
106 BarcodeOptions.BarcodeOrientation.e_horizontal |
107 BarcodeOptions.BarcodeOrientation.e_vertical);
108 BarcodeModule.ExtractBarcodes(doc, output_path + "barcodes_1D.json", options);
109 }
110 }
111 catch (PDFNetException e)
112 {
113 Console.WriteLine(e.Message);
114 }
115 Console.WriteLine("Done.");
116 PDFNet.Terminate();
117 }
118 }
119}
120
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
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
BarcodeTest.js
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5const fs = require('fs');
6const { PDFNet } = require('../../../lib/pdfnet.js');
7const PDFTronLicense = require('../../LicenseKey/NODEJS/LicenseKey');
8((exports) => {
9 'use strict';
10 //---------------------------------------------------------------------------------------
11 // The Barcode Module is an optional PDFNet add-on that can be used to extract
12 // various types of barcodes from PDF documents.
13 //
14 // The Apryse SDK Barcode Module can be downloaded from https://dev.apryse.com/
15 //---------------------------------------------------------------------------------------
16 exports.runBarcodeTest = () => {
17 const main = async () => {
18 PDFNet.addResourceSearchPath('../../../lib/');
19 if (!(await PDFNet.BarcodeModule.isModuleAvailable())) {
20 console.log('\nUnable to run BarcodeTest: Apryse SDK Barcode Module not available.');
21 console.log('---------------------------------------------------------------');
22 console.log('The Barcode Module is an optional add-on, available for download');
23 console.log('at https://dev.apryse.com/. If you have already downloaded this');
24 console.log('module, ensure that the SDK is able to find the required files');
25 console.log('using the PDFNet.addResourceSearchPath() function.\n');
26 return;
27 }
28 // Relative path to the folder containing test files.
29 const input_path = '../../TestFiles/Barcode/';
30 const output_path = '../../TestFiles/Output/';
31 //--------------------------------------------------------------------------------
32 // Example 1) Detect and extract all barcodes from a PDF document into a JSON file
33 try {
34 console.log('Example 1: extracting barcodes from barcodes.pdf to barcodes.json');
35 // A) Open the .pdf document
36 const doc = await PDFNet.PDFDoc.createFromFilePath(input_path + 'barcodes.pdf');
37 // B) Detect PDF barcodes with the default options
38 await PDFNet.BarcodeModule.extractBarcodes(doc, output_path + 'barcodes.json');
39 } catch (err) {
40 console.log(err);
41 }
42 //--------------------------------------------------------------------------------
43 // Example 2) Limit barcode extraction to a range of pages, and retrieve the JSON into a
44 // local string variable, which is then written to a file in a separate function call
45 try {
46 console.log('Example 2: extracting barcodes from pages 1-2 to barcodes_from_pages_1-2.json');
47 // A) Open the .pdf document
48 const doc = await PDFNet.PDFDoc.createFromFilePath(input_path + 'barcodes.pdf');
49 // B) Detect PDF barcodes with custom options
50 const options = new PDFNet.BarcodeModule.BarcodeOptions();
51 // Convert only the first two pages
52 options.setPages('1-2');
53 const json = await PDFNet.BarcodeModule.extractBarcodesAsString(doc, options);
54 // C) Save JSON to file
55 fs.writeFileSync(output_path + 'barcodes_from_pages_1-2.json', json);
56 } catch (err) {
57 console.log(err);
58 }
59 //--------------------------------------------------------------------------------
60 // Example 3) Narrow down barcode types and allow the detection of both horizontal
61 // and vertical barcodes
62 try {
63 console.log('Example 3: extracting basic horizontal and vertical barcodes');
64 // A) Open the .pdf document
65 const doc = await PDFNet.PDFDoc.createFromFilePath(input_path + 'barcodes.pdf');
66 // B) Detect only basic 1D barcodes, both horizontal and vertical
67 const options = new PDFNet.BarcodeModule.BarcodeOptions();
68 // Limit extraction to basic 1D barcode types, such as EAN 13, EAN 8, UPCA, UPCE,
69 // Code 3 of 9, Code 128, Code 2 of 5, Code 93, Code 11 and GS1 Databar.
70 options.setBarcodeSearchTypes(PDFNet.BarcodeModule.BarcodeOptions.BarcodeTypeGroup.e_linear);
71 // Search for barcodes oriented horizontally and vertically
72 options.setBarcodeOrientations(
73 PDFNet.BarcodeModule.BarcodeOptions.BarcodeOrientation.e_horizontal |
74 PDFNet.BarcodeModule.BarcodeOptions.BarcodeOrientation.e_vertical);
75 await PDFNet.BarcodeModule.extractBarcodes(doc, output_path + 'barcodes_1D.json', options);
76 } catch (err) {
77 console.log(err);
78 }
79 //////////////////////////////////////////////////////////////////////////
80 console.log('Done.');
81 };
82 PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function(error) {
83 console.log('Error: ' + JSON.stringify(error));
84 }).then(function(){ return PDFNet.shutdown(); });
85 };
86 exports.runBarcodeTest();
87})(exports);
88// eslint-disable-next-line spaced-comment
89//# sourceURL=BarcodeTest.js
BarcodeTest.php
1<?php
2//---------------------------------------------------------------------------------------
3// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
4// Consult LICENSE.txt regarding license information.
5//---------------------------------------------------------------------------------------
6if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
7include("../../../PDFNetC/Lib/PDFNetPHP.php");
8include("../../LicenseKey/PHP/LicenseKey.php");
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//---------------------------------------------------------------------------------------
15function WriteTextToFile($outputFile, $text)
16{
17 $outfile = fopen($outputFile, "w");
18 fwrite($outfile, $text);
19 fclose($outfile);
20}
21function main()
22{
23 // Relative path to the folder containing the test files.
24 $input_path = getcwd()."/../../TestFiles/Barcode/";
25 $output_path = getcwd()."/../../TestFiles/Output/";
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 global $LicenseKey;
30 PDFNet::Initialize($LicenseKey);
31 PDFNet::GetSystemFontList(); // Wait for fonts to be loaded if they haven't already. This is done because PHP can run into errors when shutting down if font loading is still in progress.
32 // The location of the Barcode Module
33 PDFNet::AddResourceSearchPath("../../../PDFNetC/Lib/");
34 if (!BarcodeModule::IsModuleAvailable()) {
35 echo(nl2br("\n"));
36 echo(nl2br("Unable to run BarcodeTest: Apryse SDK Barcode Module not available.\n"));
37 echo(nl2br("---------------------------------------------------------------\n"));
38 echo(nl2br("The Barcode Module is an optional add-on, available for download\n"));
39 echo(nl2br("at https://dev.apryse.com/. If you have already downloaded this\n"));
40 echo(nl2br("module, ensure that the SDK is able to find the required files\n"));
41 echo(nl2br("using the PDFNet::AddResourceSearchPath() function.\n"));
42 echo(nl2br("\n"));
43 }
44 else {
45 try {
46 //--------------------------------------------------------------------------------
47 // Example 1) Detect and extract all barcodes from a PDF document into a JSON file
48 echo(nl2br("Example 1: extracting barcodes from barcodes.pdf to barcodes.json\n"));
49 // A) Open the .pdf document
50 $doc = new PDFDoc($input_path."barcodes.pdf");
51 // B) Detect PDF barcodes with the default options
52 BarcodeModule::ExtractBarcodes($doc, $output_path."barcodes.json");
53 $doc->Close();
54 //--------------------------------------------------------------------------------
55 // Example 2) Limit barcode extraction to a range of pages, and retrieve the JSON into a
56 // local string variable, which is then written to a file in a separate function call
57 echo(nl2br("Example 2: extracting barcodes from pages 1-2 to barcodes_from_pages_1-2.json\n"));
58 // A) Open the .pdf document
59 $doc = new PDFDoc($input_path."barcodes.pdf");
60 // B) Detect PDF barcodes with custom options
61 $options = new BarcodeOptions();
62 // Convert only the first two pages
63 $options->SetPages("1-2");
64 $json = BarcodeModule::ExtractBarcodesAsString($doc, $options);
65 // C) Save JSON to file
66 WriteTextToFile($output_path."barcodes_from_pages_1-2.json", $json);
67 $doc->Close();
68 //--------------------------------------------------------------------------------
69 // Example 3) Narrow down barcode types and allow the detection of both horizontal
70 // and vertical barcodes
71 echo(nl2br("Example 3: extracting basic horizontal and vertical barcodes\n"));
72 // A) Open the .pdf document
73 $doc = new PDFDoc($input_path."barcodes.pdf");
74 // B) Detect only basic 1D barcodes, both horizontal and vertical
75 $options = new BarcodeOptions();
76 // Limit extraction to basic 1D barcode types, such as EAN 13, EAN 8, UPCA, UPCE,
77 // Code 3 of 9, Code 128, Code 2 of 5, Code 93, Code 11 and GS1 Databar.
78 $options->SetBarcodeSearchTypes(BarcodeOptions::e_linear);
79 // Search for barcodes oriented horizontally and vertically
80 $options->SetBarcodeOrientations(
81 BarcodeOptions::e_horizontal |
82 BarcodeOptions::e_vertical);
83 BarcodeModule::ExtractBarcodes($doc, $output_path."barcodes_1D.json", $options);
84 $doc->Close();
85 }
86 catch (Exception $e) {
87 echo(nl2br("Unable to extract form fields data, error: " . $e->getMessage() . "\n"));
88 }
89 }
90 PDFNet::Terminate();
91 echo(nl2br("Done.\n"));
92}
93main();
94?>
95
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
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
BarcodeTest.vb
1'---------------------------------------------------------------------------------------
2' Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3' Consult legal.txt regarding legal and license information.
4'---------------------------------------------------------------------------------------
5Imports System
6Imports pdftron
7Imports pdftron.Common
8Imports pdftron.PDF
9' <summary>
10'---------------------------------------------------------------------------------------
11' The Barcode Module is an optional PDFNet add-on that can be used to extract
12' various types of barcodes from PDF documents.
13'
14' The Apryse SDK Barcode Module can be downloaded from https://dev.apryse.com/
15'---------------------------------------------------------------------------------------
16' </summary>
17Module BarcodeTestVB
18 Dim pdfNetLoader As PDFNetLoader
19 Sub New()
20 pdfNetLoader = pdftron.PDFNetLoader.Instance()
21 End Sub
22 ' The main entry point for the application.
23 Sub Main()
24 ' The first step in every application using PDFNet is to initialize the
25 ' library and set the path to common PDF resources. The library is usually
26 ' initialized only once, but calling Initialize() multiple times is also fine.
27 PDFNet.Initialize(PDFTronLicense.Key)
28 ' Can optionally set path to the Barcode module
29 PDFNet.AddResourceSearchPath("../../../../../Lib/")
30 ' Test if the add-on is installed
31 If Not BarcodeModule.IsModuleAvailable() Then
32 Console.WriteLine("")
33 Console.WriteLine("Unable to run BarcodeTest: Apryse SDK Barcode Module not available.")
34 Console.WriteLine("---------------------------------------------------------------")
35 Console.WriteLine("The Barcode Module is an optional add-on, available for download")
36 Console.WriteLine("at https://dev.apryse.com/. If you have already downloaded this")
37 Console.WriteLine("module, ensure that the SDK is able to find the required files")
38 Console.WriteLine("using the PDFNet.AddResourceSearchPath() function.")
39 Console.WriteLine("")
40 Return
41 End If
42 ' Relative path to the folder containing test files.
43 Dim input_path As String = "../../../../TestFiles/Barcode/"
44 Dim output_path As String = "../../../../TestFiles/Output/"
45 '--------------------------------------------------------------------------------
46 ' Example 1) Detect and extract all barcodes from a PDF document into a JSON file
47 Try
48 Console.WriteLine("Example 1: extracting barcodes from barcodes.pdf to barcodes.json")
49 ' A) Open the .pdf document
50 Using doc As PDFDoc = New PDFDoc(input_path & "barcodes.pdf")
51 ' B) Detect PDF barcodes with the default options
52 BarcodeModule.ExtractBarcodes(doc, output_path & "barcodes.json")
53 End Using
54 Catch e As PDFNetException
55 Console.WriteLine(e.Message)
56 End Try
57 '--------------------------------------------------------------------------------
58 ' Example 2) Limit barcode extraction to a range of pages, and retrieve the JSON into a
59 ' local string variable, which is then written to a file in a separate function call
60 Try
61 Console.WriteLine("Example 2: extracting barcodes from pages 1-2 to barcodes_from_pages_1-2.json")
62 ' A) Open the .pdf document
63 Using doc As PDFDoc = New PDFDoc(input_path & "barcodes.pdf")
64 ' B) Detect PDF barcodes with custom options
65 Dim options As BarcodeOptions = New BarcodeOptions()
66 ' Convert only the first two pages
67 options.SetPages("1-2")
68 Dim json As String = BarcodeModule.ExtractBarcodesAsString(doc, options)
69 ' C) Save JSON to file
70 System.IO.File.WriteAllText(output_path & "barcodes_from_pages_1-2.json", json)
71 End Using
72 Catch e As PDFNetException
73 Console.WriteLine(e.Message)
74 End Try
75 '--------------------------------------------------------------------------------
76 ' Example 3) Narrow down barcode types and allow the detection of both horizontal
77 ' and vertical barcodes
78 Try
79 Console.WriteLine("Example 3: extracting basic horizontal and vertical barcodes")
80 ' A) Open the .pdf document
81 Using doc As PDFDoc = New PDFDoc(input_path & "barcodes.pdf")
82 ' B) Detect only basic 1D barcodes, both horizontal and vertical
83 Dim options As BarcodeOptions = New BarcodeOptions()
84 ' Limit extraction to basic 1D barcode types, such as EAN 13, EAN 8, UPCA, UPCE,
85 ' Code 3 of 9, Code 128, Code 2 of 5, Code 93, Code 11 and GS1 Databar.
86 options.SetBarcodeSearchTypes(BarcodeOptions.BarcodeTypeGroup.e_linear)
87 ' Search for barcodes oriented horizontally and vertically
88 options.SetBarcodeOrientations(BarcodeOptions.BarcodeOrientation.e_horizontal Or BarcodeOptions.BarcodeOrientation.e_vertical)
89 BarcodeModule.ExtractBarcodes(doc, output_path & "barcodes_1D.json", options)
90 End Using
91 Catch e As PDFNetException
92 Console.WriteLine(e.Message)
93 End Try
94 Console.WriteLine("Done.")
95 PDFNet.Terminate()
96 End Sub
97End Module
98
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales