Skip to main content
Skip table of contents

pdfOCR: PaddleOCR model support

The release of pdfOCR 5.0.0 introduced support for pretrained ONNX PaddleOCR and EasyOCR models, adding to the docTR models already supported.

The following code sample shows how to generate a searchable PDF by running OCR with a PaddleOCR model (converted to ONNX format) through pdfOCR’s ONNX-based OCR engine.

After loading the specified input image, it builds a detection predictor and a recognition predictor from the PaddleOCR ONNX model files (inference.onnx) and their accompanying configuration files (inference.yml), and then creates a output PDF.

Check the comments in the example for more details.

Compatible PaddleOCR/EasyOCR models already converted to ONNX format are available from our Hugging Face repository.

Java

JAVA
package com.itextpdf.samples.sandbox.pdfocr.onnx;

import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.pdfocr.OcrPdfCreator;
import com.itextpdf.pdfocr.onnx.OnnxOcrEngine;
import com.itextpdf.pdfocr.onnx.detection.IDetectionPredictor;
import com.itextpdf.pdfocr.onnx.detection.OnnxDetectionPredictor;
import com.itextpdf.pdfocr.onnx.recognition.IRecognitionPredictor;
import com.itextpdf.pdfocr.onnx.recognition.OnnxRecognitionPredictor;

import java.io.File;
import java.util.Collections;

/**
 * PdfOcrOnnxPaddleOcrExample.java
 *
 * <p>
 * This example demonstrates how to perform OCR using {@link OnnxOcrEngine} and PaddleOCR ML-models
 * for the given list of input images and save output to a PDF file using provided path.
 *
 * <p>
 * PaddleOCR models converted to ONNX format can be found at
 * <a href="https://huggingface.co/itextresearch">iText Research Hugging Face page</a>.
 *
 * <p>
 * Required software: iText 9.6.0, pdfOCR-Onnx 5.0.0
 * (pdfocr-onnx-cpu dependency to execute ONNX models on CPU or 
 * pdfocr-onnx-abstract and onnxruntime_gpu dependencies to execute ONNX models on GPU).
 */
public class PdfOcrOnnxPaddleOcrExample {
    public static final String DEST = "./target/sandbox/pdfocr/onnx/PdfOcrOnnxPaddleOcrExample/result.pdf";

    private static final String BASIC_IMAGE = "./src/main/resources/img/ocrExample.png";

    private static final String MODELS = "./src/main/resources/models/paddleocr/";
    private static final String DETECTION = MODELS + "PP-OCRv5_mobile_det_infer";
    private static final String RECOGNITION = MODELS + "PP-OCRv5_mobile_rec_infer";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new PdfOcrOnnxPaddleOcrExample().manipulate(DEST);
    }

    protected void manipulate(String destination) throws Exception {
        IDetectionPredictor detectionPredictor =
                OnnxDetectionPredictor.paddleOcr(DETECTION + "/inference.onnx", DETECTION + "/inference.yml");
        IRecognitionPredictor recognitionPredictor =
                OnnxRecognitionPredictor.paddleOcr(RECOGNITION + "/inference.onnx", RECOGNITION + "/inference.yml");

        // OnnxOcrEngine shall be closed after usage to avoid native allocations leak.
        // It will also close all predictors used for its creation.
        try (OnnxOcrEngine ocrEngine = new OnnxOcrEngine(detectionPredictor, recognitionPredictor)) {
            OcrPdfCreator pdfCreator = new OcrPdfCreator(ocrEngine);
            pdfCreator.createPdf(Collections.singletonList(new File(BASIC_IMAGE)), new PdfWriter(destination)).close();
        }
    }
}

C#

C#
using System;
using System.Collections.Generic;
using System.IO;
using iText.Kernel.Pdf;
using iText.Pdfocr;
using iText.Pdfocr.Onnx;
using iText.Pdfocr.Onnx.Detection;
using iText.Pdfocr.Onnx.Recognition;

namespace iText.Samples.Sandbox.Pdfocr.Onnx {
    /// <summary>PdfOcrOnnxPaddleOcrExample.cs</summary>
    /// <remarks>
    /// PdfOcrOnnxPaddleOcrExample.cs
    /// <para />
    /// This example demonstrates how to perform OCR using
    /// <see cref="iText.Pdfocr.Onnx.OnnxOcrEngine"/>
    /// and PaddleOCR ML-models
    /// for the given list of input images and save output to a PDF file using provided path.
    /// <para />
    /// PaddleOCR models converted to ONNX format can be found at
    /// <a href="https://huggingface.co/itextresearch">iText Research Hugging Face page</a>.
    /// <para />
    /// Required software: iText 9.6.0, pdfOCR-Onnx 5.0.0
    /// (itext.pdfocr.onnx.cpu dependency to execute ONNX models on CPU or
    /// itext.pdfocr.onnx.abstract and Microsoft.ML.OnnxRuntime.Gpu dependencies to execute ONNX models on GPU).
    /// </remarks>
    public class PdfOcrOnnxPaddleOcrExample {
        public const String DEST = "results/sandbox/pdfocr/onnx/PdfOcrOnnxPaddleOcrExample/result.pdf";

        private const String BASIC_IMAGE = "../../../resources/img/ocrExample.png";

        private const String MODELS = "../../../resources/models/paddleocr/";

        private const String DETECTION = MODELS + "PP-OCRv5_mobile_det_infer";

        private const String RECOGNITION = MODELS + "PP-OCRv5_mobile_rec_infer";

        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new PdfOcrOnnxPaddleOcrExample().Manipulate(DEST);
        }

        protected internal virtual void Manipulate(String destination) {
            IDetectionPredictor detectionPredictor = OnnxDetectionPredictor.PaddleOcr(
                DETECTION + "/inference.onnx", DETECTION + "/inference.yml");
            IRecognitionPredictor recognitionPredictor = OnnxRecognitionPredictor.PaddleOcr(
                RECOGNITION + "/inference.onnx", RECOGNITION + "/inference.yml");

            // OnnxOcrEngine shall be closed after usage to avoid native allocations leak.
            // It will also close all predictors used for its creation.
            using (OnnxOcrEngine ocrEngine = new OnnxOcrEngine(detectionPredictor, recognitionPredictor)) {
                OcrPdfCreator pdfCreator = new OcrPdfCreator(ocrEngine);
                pdfCreator.CreatePdf(new List<FileInfo> { new FileInfo(BASIC_IMAGE) }, new PdfWriter(destination))
                    .Close();
            }
        }
    }
}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.