How to read QR code PDF using Java

Introduction

数百枚もの PDF 請求書、出荷ラベル、在庫文書からバーコード情報を抽出する必要はありませんか? 手作業でページをスキャンするのは手間がかかり、ミスも起きやすいです。自動文書処理システムを構築する場合でも、製品の真正性を検証する場合でも、PDF 内のバーコードを効率的に見つけることは難しいことがあります。

このガイドでは、GroupDocs.Signature API を使用して read QR code PDF 文書を効率的に読み取る方法を学びます。この強力な API を使えば、手作業で何時間もかかる作業が数行のコードで完了します。文書全体をスキャンし、特定のバーコードタイプ(QR コードや Code128 など)を検出し、データを自動的に抽出できます。

What You’ll Learn:

  • GroupDocs.Signature for Java の数分でのセットアップ
  • PDF 文書内のバーコード署名の検索
  • 正確でターゲットを絞った結果を得るための検索オプション設定
  • 異なるバーコードタイプ(QR コード、EAN、Code128 など)の取り扱い
  • よくある問題のトラブルシューティングとパフォーマンス最適化

さあ、始めましょう!

Quick Answers

  • Can GroupDocs.Signature read QR codes from PDFs? Yes, it detects QR, Data Matrix, PDF417, and many 1D barcodes.
  • Do I need a license for production use? A commercial license is required; a free trial is available for evaluation.
  • Which Java version is required? Java 8+ (Java 11+ recommended).
  • How do I limit the search to specific pages? Use BarcodeSearchOptions.setAllPages(false) and set setPageNumber().
  • Is the API thread‑safe for batch processing? Yes, when you create a separate Signature instance per thread.

Why Search Barcodes in PDFs?

技術的な話に入る前に、実務でこの機能がなぜ重要かを見てみましょう。

Common Business Scenarios

  • Invoice Processing – ベンダー請求書から注文番号や追跡コードを自動抽出。
  • Inventory Management – 製品カタログをスキャンし、SKU バーコードをデータベースに更新。
  • Shipping & Logistics – 出荷明細書でパッケージの追跡コードを検証。
  • Document Authentication – 埋め込まれたセキュリティバーコードで署名済み文書を検証。
  • Healthcare Records – 医療文書から患者 ID や処方コードを抽出。

GroupDocs.Signature API が重い処理をすべて担ってくれるので、画像処理やバーコードデコードアルゴリズム、PDF レンダリングの複雑さを意識する必要はありません。すべて組み込み済みです。

Prerequisites

このチュートリアルを始める前に、以下を用意してください。

Required Libraries and Dependencies

Java プロジェクトに GroupDocs.Signature ライブラリを組み込む必要があります。Maven または Gradle での追加方法は次の通りです。

Maven:

<dependency>
    <groupId>com.groupdocs</groupId>
    <artifactId>groupdocs-signature</artifactId>
    <version>23.12</version>
</dependency>

Gradle:

implementation 'com.groupdocs:groupdocs-signature:23.12'

Note: 常に最新バージョンを GroupDocs.Signature for Java releases で確認してください。最新バージョンを使用するとバグ修正や新機能が利用できます。

Environment Setup

  • JDK 8 or higher – GroupDocs.Signature は最低 Java 8 が必要です(パフォーマンス向上のため Java 11+ 推奨)。
  • IDE – 任意のテキストエディタでも構いませんが、IntelliJ IDEA や Eclipse を使うとオートコンプリートやデバッグが楽になります。
  • PDF Document – バーコードが含まれたテスト用 PDF を用意してください(請求書、出荷ラベル、製品カタログなどが最適です)。

Knowledge Prerequisites

以下に慣れていることが望ましいです。

  • 基本的な Java 文法とオブジェクト指向概念
  • try‑catch ブロックによる例外処理
  • IDE で外部ライブラリを扱う方法

サードパーティの Java ライブラリが初めてでも心配いりません。ステップバイステップで解説します。

Setting Up GroupDocs.Signature for Java

GroupDocs.Signature の開始は数分で完了します。以下が全体のセットアップ手順です。

Step 1: Add the Dependency

Maven または Gradle でライブラリを追加します(上記コード参照)。依存関係を追加したら、プロジェクトをリフレッシュして JAR をダウンロードしてください。

Step 2: License Acquisition

GroupDocs には複数のライセンス形態があります。

  • Free Trial – テストに最適です。 GroupDocs releases からダウンロードしてください。
  • Temporary LicenseTemporary License Page で 30 日間のフルアクセスが取得できます。
  • Commercial License – 本番環境で使用する場合は、GroupDocs Purchase でライセンスを購入してください。

Pro Tip: まずは無料トライアルで概念実証を行い、API が要件に合致したら商用ライセンスにアップグレードしましょう。

Step 3: Basic Initialization

PDF を扱うための Signature オブジェクト作成例です。

import com.groupdocs.signature.Signature;

String filePath = "YOUR_DOCUMENT_DIRECTORY/sample_signed.pdf"; // Replace with your PDF path
Signature signature = new Signature(filePath);

Signature クラスがエントリーポイントになります。PDF をメモリに読み込み、検索・検証・署名データ(バーコード含む)の抽出メソッドを提供します。

Important: ファイルパスが正しく、PDF が存在することを確認してください。初心者が陥りやすいミスは、Windows のバックスラッシュをエスケープせずに書くことです(例: C:\\Documents\\file.pdf は OK、C:\Documents\file.pdf は NG)。

Implementation Guide

さあ、実際に PDF からバーコードを検索するコードを書いてみましょう。

Searching for Barcode Signatures in a Document

このセクションでは、PDF をスキャンしてすべてのバーコード署名を検出する手順を解説します。各ステップごとに説明を加えます。

Step 1: Initialize the Signature Object

PDF 文書を読み込みます。

import com.groupdocs.signature.Signature;

String filePath = "YOUR_DOCUMENT_DIRECTORY/sample_signed.pdf"; // Replace with actual file path
Signature signature = new Signature(filePath);

What’s Happening Here
Signature クラスが PDF を開き、処理の準備をします。テキストエディタでファイルを開くイメージです—文書がメモリにロードされ、操作可能になります。

Real‑World Note
ユーザーがアップロードした PDF を処理する場合は、Signature オブジェクトを作成する前に必ずファイルパスの検証と存在確認を行いましょう。これにより、後で発生する暗号的なエラーを防げます。

Step 2: Create BarcodeSearchOptions

バーコード検索の設定を行います。

import com.groupdocs.signature.options.search.BarcodeSearchOptions;

// Configure options for searching barcodes
BarcodeSearchOptions options = new BarcodeSearchOptions();
options.setAllPages(true); // Search every page in the document

Key Configuration Options

  • setAllPages(true): すべてのページをスキャンします。特定のページだけを対象にしたい場合は false にし、setPageNumber() でページ番号を指定します。
  • Why This Matters: 請求書でバーコードが常に 1 ページ目にある場合、全ページ検索はリソースの無駄です。複数ページにわたる出荷明細書では setAllPages(true) が必要になります。

Pro Tip: バーコードタイプでフィルタリングも可能です(下記 Supported Barcode Types セクション参照)。検索対象フォーマットが分かっている場合、処理速度が向上します。

Step 3: Execute Search and Handle Results

検索を実行し、結果を処理します。

import com.groupdocs.signature.domain.signatures.BarcodeSignature;
import java.util.List;

try {
    // Execute the barcode search
    List<BarcodeSignature> signatures = signature.search(BarcodeSignature.class, options);
    
    // Check if any barcodes were found
    if (signatures.isEmpty()) {
        System.out.println("No barcode signatures found in the document.");
    } else {
        System.out.println("Found " + signatures.size() + " barcode signature(s):\n");
        
        // Loop through each barcode and display details
        for (BarcodeSignature barcodeSignature : signatures) {
            System.out.println("----------------------------------------");
            System.out.println("Barcode Type: " + barcodeSignature.getEncodeType().getTypeName());
            System.out.println("Barcode Text: " + barcodeSignature.getText());
            System.out.println("Page Number: " + barcodeSignature.getPageNumber());
            System.out.println("Position: X=" + barcodeSignature.getLeft() + 
                             ", Y=" + barcodeSignature.getTop());
            System.out.println("Size: Width=" + barcodeSignature.getWidth() + 
                             ", Height=" + barcodeSignature.getHeight());
            System.out.println("----------------------------------------\n");
        }
    }
} catch (Exception e) {
    System.err.println("Error searching for barcodes: " + e.getMessage());
    e.printStackTrace();
} finally {
    // Always dispose of the signature object to free resources
    if (signature != null) {
        signature.dispose();
    }
}

What’s Happening in This Code

  1. Search Executionsignature.search() が PDF を走査し、BarcodeSignature オブジェクトのリストを返します。
  2. Empty Check – バーコードが見つからなかった場合に備えて null ポインタ例外を防止します。
  3. Data Extraction – 各バーコードから以下を取得します。
    • Type – バーコード形式(QR Code、Code128、EAN13 など)
    • Text – デコードされたデータ(注文番号、追跡コード、SKU など)
    • Location – ページ番号と X/Y 座標
    • Dimensions – 幅と高さ(検証に有用)
  4. Error Handlingtry‑catch により、PDF が破損している、ファイルが見つからないなどの例外でクラッシュしないようにします。
  5. Resource Cleanupfinally ブロックで Signature オブジェクトを適切に破棄し、メモリを解放します。

Real‑World Application
例えば出荷ラベルを処理する場合、getText() で取得した追跡番号をデータベースに保存します。ページ番号は、バッチ処理時にどのラベルがどの出荷に対応するかを判別するのに役立ちます。

Filtering by Barcode Type

検索対象のバーコードタイプを指定して、処理速度を上げることができます。

import com.groupdocs.signature.domain.barcodes.BarcodeTypes;

BarcodeSearchOptions options = new BarcodeSearchOptions();
options.setEncodeType(BarcodeTypes.QR); // Only search for QR codes
options.setAllPages(true);

When to Filter
請求書に Code128 バーコードしか含まれないと分かっている場合、タイプで絞り込むだけで大規模文書の処理時間が 30‑50 % 短縮できます。

Supported Barcode Types

GroupDocs.Signature は幅広いバーコード形式を検出できます。検索可能なフォーマットは以下の通りです。

1D Barcodes (Linear)

  • Code128 – 出荷やパッケージで一般的
  • Code39 – 自動車・防衛産業で使用
  • EAN13/EAN8 – 小売商品のバーコード(すべての製品に見られます)
  • UPC‑A/UPC‑E – 北米小売標準
  • Interleaved2of5 – 倉庫・流通で利用

2D Barcodes (Matrix)

  • QR Code – URL、Wi‑Fi パスワード、決済情報などで最もポピュラー
  • Data Matrix – 小型部品(電子部品など)向けのコンパクト形式
  • PDF417 – 政府 ID、搭乗券、運転免許証など
  • Aztec Code – 交通チケットで使用

Filtering by Type(前述の例)を活用すれば、必要なフォーマットだけに絞って検索できます。

Real‑World Use Cases

開発者が実際にバーコード検索を活用している事例をご紹介します。

1. Automated Invoice Processing

Scenario – 経理部門が 1 日に 500 件以上のベンダー請求書を PDF で受領。
Solution – 請求書内の Code39 バーコード(請求番号)をスキャンし、ERP システムの購買注文と自動照合。手作業のデータ入力が不要になり、エラーが大幅に減少。

// Pseudo-code workflow
for (PDF invoice : invoiceBatch) {
    List<BarcodeSignature> barcodes = searchBarcodes(invoice);
    String invoiceNumber = barcodes.get(0).getText();
    updateERPSystem(invoiceNumber, invoice);
}

2. Warehouse Inventory Updates

Scenario – 倉庫が PDF 形式の梱包リストに EAN13 バーコードで記載された SKU を受領。
Solution – 梱包リストからすべてのバーコードを抽出し、在庫数を自動更新。差異があれば自動でフラグを立て、レビューを促します。

3. Document Authentication

Scenario – 法的文書に QR コードで暗号署名が埋め込まれており、真正性の検証が必要。
Solution – 署名済み契約書から QR コードを検索し、デコードした署名データを信頼できる認証局と照合。改ざんがないことを保証します。

4. Healthcare Records Management

Scenario – 病院の患者ファイルに PDF 形式の検体レポートがあり、Code128 バーコードで検体 ID が付与されている。
Solution – 検体 ID を自動抽出し、検査結果を患者情報システム(HIS)に紐付け。手作業入力の手間とミスを削減します。

Common Issues and Solutions

以下は遭遇しやすい問題とその対処法です。

Issue 1: “No Barcodes Found” (But You Know They Exist)

Possible Causes

  • バーコード画像の解像度が低すぎる(ぼやけている、ピクセル化)
  • PDF が画像ベースだがバーコードが小さすぎる
  • 誤ったバーコードタイプで検索している

Solutions

  1. Check Image Resolution – 信頼できる検出には最低 200 DPI が必要です。スキャンする場合は 300 DPI 以上を推奨します。
  2. Remove Type Filtering – まずは setEncodeType() を設定せずに全タイプ検索し、どのフォーマットが含まれているか確認します。
  3. Verify Barcode Quality – Adobe Acrobat で拡大表示し、バーコードが鮮明に見えるか確認してください。見えにくい場合、API でも検出が困難です。

Issue 2: OutOfMemoryError with Large PDFs

Cause – 500 ページ以上の高解像度 PDF を一括でロードするとメモリ消費が激しくなります。

Solution

  1. Process Pages in BatchessetAllPages(true) の代わりに、50 ページずつ処理します。
for (int startPage = 1; startPage <= totalPages; startPage += 50) {
    BarcodeSearchOptions options = new BarcodeSearchOptions();
    options.setPageNumber(startPage);
    options.setPagesSetup(new PagesSetup());
    options.getPagesSetup().setLastPage(Math.min(startPage + 49, totalPages));
    
    List<BarcodeSignature> batchResults = signature.search(BarcodeSignature.class, options);
    // Process results...
}
  1. Increase JVM Heap Size – Java 起動オプションに -Xmx4g を追加し、ヒープを 4 GB に拡張(必要に応じて調整)します。

Issue 3: Slow Performance on Multi‑Page Documents

Cause – 全ページを順次検索すると、特に PDF417 など複雑なバーコードの場合に時間がかかります。

Solutions

  1. Parallel Processing – バーコードが常に特定ページ(例: 請求書の 1 ページ目)にある場合は、そのページだけを検索します。
  2. Cache Results – 同一文書を複数回処理する場合は、検索結果をキャッシュして再スキャンを回避します。
  3. Use SSDs – 大容量 PDF の読み込みは I/O がボトルネックになることが多いです。SSD を使用するとロード時間が 60‑70 % 短縮されます。

Issue 4: False Positives (Detecting Random Patterns as Barcodes)

Cause – テーブルや格子状の線がバーコードと誤認識されることがあります。

Solution – デコードされたテキストの長さやフォーマットをチェックして、妥当性を検証します。

for (BarcodeSignature barcode : signatures) {
    String text = barcode.getText();
    
    // Example: Invoice numbers are always 10 digits
    if (text.matches("\\d{10}")) {
        // Valid invoice number
        processBarcode(barcode);
    } else {
        System.out.println("Skipping invalid barcode: " + text);
    }
}

Performance Tips for Large Documents

数千件の PDF を処理しますか? 以下の最適化手法をご活用ください。

1. Batch Processing Strategy

ファイルを 1 件ずつ処理するのではなく、スレッドプールで同時に複数の PDF を処理します。

ExecutorService executor = Executors.newFixedThreadPool(4); // 4 parallel threads

for (String pdfPath : pdfFiles) {
    executor.submit(() -> {
        try (Signature sig = new Signature(pdfPath)) {
            List<BarcodeSignature> results = sig.search(BarcodeSignature.class, options);
            // Process results...
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
}

executor.shutdown();
executor.awaitTermination(1, TimeUnit.HOURS);

Performance Gain – 1,000 ファイルの処理時間が約 2 時間から 30 分に短縮されます(クアッドコアマシンの場合)。

2. Reduce Search Scope

ビジネスロジックで許容できるなら、検索範囲を限定します。

// Only search the top‑right corner where barcodes are typically placed
options.setRectangle(new Rectangle(400, 50, 150, 150)); // X, Y, Width, Height

Performance Gain – バーコード位置が一定の場合、40‑60 % の高速化が期待できます。

3. Monitor Memory Usage

長時間実行するバッチ処理ではヒープ使用量を監視し、必要に応じて明示的にガベージコレクションを促します。

Runtime runtime = Runtime.getRuntime();
long usedMemory = runtime.totalMemory() - runtime.freeMemory();

if (usedMemory > (runtime.maxMemory() * 0.8)) {
    System.gc(); // Suggest garbage collection
}

Best Practices

本番環境向けコードを書く際の指針です。

1. Always Dispose of Signature Objects

Java 7 以降の try‑with‑resources を使ってリソースを自動的にクローズします。

try (Signature signature = new Signature(filePath)) {
    // Your search code here...
} // Automatically disposed

2. Validate Input Files

処理前にファイルの存在と有効な PDF かどうかをチェックします。

File pdfFile = new File(filePath);
if (!pdfFile.exists() || !pdfFile.canRead()) {
    throw new FileNotFoundException("PDF not found or not readable: " + filePath);
}

// Optional: Verify it's actually a PDF (check magic bytes)

3. Log Barcode Detection Results

デバッグや監査のために、検出結果をログに残しましょう。

Logger logger = Logger.getLogger(BarcodeSearcher.class.getName());

for (BarcodeSignature barcode : signatures) {
    logger.info(String.format("Detected %s barcode '%s' on page %d at (%.2f, %.2f)",
        barcode.getEncodeType().getTypeName(),
        barcode.getText(),
        barcode.getPageNumber(),
        barcode.getLeft(),
        barcode.getTop()));
}

4. Handle Different Barcode Formats

業界ごとに使用される標準が異なるため、コードは柔軟に設計します。

switch (barcode.getEncodeType().getTypeName()) {
    case "QR":
        // QR codes might contain URLs or JSON data
        processQRCode(barcode.getText());
        break;
    case "Code128":
        // Code128 typically contains alphanumeric order/tracking numbers
        processTrackingNumber(barcode.getText());
        break;
    default:
        logger.warning("Unexpected barcode type: " + barcode.getEncodeType());
}

5. Test with Real‑World Documents

完璧なサンプル PDF だけでなく、実際の運用環境から取得した文書でテストしてください。

  • コーヒーのシミが付いたスキャン請求書
  • ノイズが入ったファックス出荷ラベル
  • モバイル端末で撮影し PDF 変換した低解像度画像

これにより、デモでは見つからないエッジケースを事前に把握できます。

Frequently Asked Questions

Q: Can I read QR code PDF files without a license?
A: A free trial lets you read QR code PDF files for evaluation, but a commercial license is required for production deployments.

Q: Does the API support password‑protected PDFs?
A: Yes. You can pass the password when creating the Signature object: new Signature(filePath, "password").

Q: How do I improve detection on low‑resolution scans?
A: Increase the DPI of the source scan (minimum 200 DPI) and consider filtering by barcode type to reduce false positives.

Q: Is the search thread‑safe for parallel processing?
A: Each thread should use its own Signature instance. The API itself is thread‑safe when used this way.

Q: What version of GroupDocs.Signature is tested with this tutorial?
A: The code was validated with GroupDocs.Signature 23.12.

Conclusion

You’ve just learned how to read QR code PDF documents using Java and the GroupDocs.Signature API. Here’s what we covered:

Setup – Adding GroupDocs.Signature to your project and licensing options
Implementation – Complete code to search, extract, and process barcode data
Barcode Types – Understanding which formats are supported (1D and 2D)
Real‑World Use Cases – Invoice processing, inventory management, document authentication, healthcare records
Troubleshooting – Solving common issues like memory errors and false positives
Performance – Optimizing searches for large‑scale document processing

The GroupDocs.Signature API handles the complexity of PDF parsing and barcode detection, letting you focus on building your business logic. Whether you’re automating invoice processing, verifying shipping labels, or extracting inventory data, you now have a robust solution.


Last Updated: 2026-03-01
Tested With: GroupDocs.Signature 23.12
Author: GroupDocs