JDK 11+, no SDK required

OCR API Java: Extract Text and Data from PDFs and Images

Call DocuOCR from Java with the built-in HttpClient: POST a PDF or image, start extraction, and read structured JSON back with named fields, tables and confidence scores. No native OCR binary to install, no cloud SDK to wire.

The same endpoint reads digital and scanned PDFs, so one code path handles both. Copy the Java below and run it against your own document. Last updated July 2026.

  • Three HttpClient calls, JSON out
  • Fields, tables and confidence
  • Reads scans and native PDFs
  • Webhooks for batch at scale
Try it now, no signup

PDF, JPG, PNG, BMP, HEIC, TIFF

Upload a document to extract

See the JSON your HttpClient call would receive, right in the browser.

SOC 2 Type II
256-bit encryption
US data handling
REST and JSON
3 calls
upload, extract, get JSON
0 SDKs
built-in HttpClient is enough
1 endpoint
scans and digital PDFs alike
JSON
fields, tables, confidence
// The short answer

How to call an OCR API from Java

To use an OCR API in Java, you send a document to an HTTPS endpoint and read the recognized data back as JSON, all through the HttpClient that ships with the JDK from Java 11. There is no native OCR engine to install, no model to train, and with a REST API like DocuOCR no Maven SDK to add. You authenticate with a Bearer API key, POST the file as multipart form data to get a file_id, start an extraction job, then poll or receive a webhook for the result. Because the response is JSON, you map it onto a record or POJO with Jackson and get named fields, table rows and confidence scores you can drop straight into a database, an export or an accounting workflow. The same three calls work for a native PDF, a scanned PDF or a photo, so your code does not branch on how the document was made.

The three calls

  • Upload: POST the file, get a file_id.
  • Extract: POST the file_id, get a job hash.
  • Get: poll or webhook for the JSON.
// Copy and run

OCR a PDF in Java with the DocuOCR API

Three HttpClient calls, from a file on disk to structured JSON. Runs on JDK 11+ with no dependencies. Swap in your API key from the dashboard and point it at any PDF or image.

OcrPdf.java
import java.net.URI;
import java.net.http.*;
import java.nio.file.*;

// Multipart upload omitted for brevity; JSON parsed with your library of choice.
String apiKey = "YOUR_API_KEY";
String base = "https://docuocr.com/api/documents";
HttpClient http = HttpClient.newHttpClient();

// 1. Upload the PDF or image, get a file_id (multipart body built with a boundary)
HttpRequest upload = HttpRequest.newBuilder(URI.create(base + "/upload"))
    .header("Authorization", "Bearer " + apiKey)
    .header("Content-Type", "multipart/form-data; boundary=X")
    .POST(HttpRequest.BodyPublishers.ofByteArray(multipart("invoice.pdf")))
    .build();
String fileId = json(http.send(upload, HttpResponse.BodyHandlers.ofString()).body(), "file_id");

// 2. Start extraction, get a job hash
HttpRequest extract = HttpRequest.newBuilder(URI.create(base + "/extract"))
    .header("Authorization", "Bearer " + apiKey)
    .header("Content-Type", "application/json")
    .POST(HttpRequest.BodyPublishers.ofString("{\"file_id\":\"" + fileId + "\"}"))
    .build();
String hash = json(http.send(extract, HttpResponse.BodyHandlers.ofString()).body(), "extraction_hash");

// 3. Poll until the JSON is ready
HttpRequest get = HttpRequest.newBuilder(URI.create(base + "/extraction/" + hash))
    .header("Authorization", "Bearer " + apiKey).build();
String body;
do {
    Thread.sleep(2000);
    body = http.send(get, HttpResponse.BodyHandlers.ofString()).body();
} while (!json(body, "status").equals("completed"));

// body now holds fields, tables and confidence as JSON, ready to map with Jackson
System.out.println(body);

That is the whole integration. For a production batch, wrap each call in try or catch, add exponential backoff on HTTP 429, submit through an ExecutorService, and register a webhook instead of the polling loop so results arrive as each document finishes. The document OCR API reference covers the full request and response shape. Working in another language? The same flow in Python or Node.js reads the same JSON.

// Java support, compared

Every major OCR API's Java library

The cloud providers ship a Maven SDK; a REST API needs only the built-in HttpClient. What you get back differs more than the language does. Artifacts verified July 2026.

OCR API Java library (Maven) What it returns Best for
DocuOCR java.net.http.HttpClient (built into JDK 11+) Named fields, tables and confidence as JSON Fields plus tables from one call, scans included
Google Cloud Vision com.google.cloud:google-cloud-vision Text and bounding boxes, no named fields Raw text OCR on the Google stack
Google Document AI com.google.cloud:google-cloud-document-ai Text plus entities, needs a service account Google-native document pipelines
AWS Textract software.amazon.awssdk:textract Text, forms and tables as blocks AWS-stack teams on the v2 SDK
Azure AI Document Intelligence com.azure:azure-ai-documentintelligence Text, layout and prebuilt-model fields Azure-stack, prebuilt invoice or receipt models
Mistral Document AI No official Java SDK (call REST directly) Markdown plus structured output via REST LLM-based parsing of mixed content
Tesseract (tess4j) net.sourceforge.tess4j:tess4j Plain text only, needs the native binary Free, offline, low-volume plain text

The split that matters: raw OCR libraries like Google Cloud Vision and tess4j return text and coordinates, so you write the field mapping in Java yourself. A document extraction API returns named fields and table rows already, which is most of the code you would otherwise maintain. See the full OCR API pricing comparison for what each costs per 1,000 pages.

// How it works

From a file to a Java object in four steps

1. Authenticate

Generate a key in the dashboard and send it as a Bearer token in the Authorization header.

2. Upload

POST a PDF, PNG, JPG, TIFF or WEBP as multipart form data and read the file_id from the JSON.

3. Extract

POST the file_id to start an async job and get an extraction_hash back.

4. Map JSON

Poll or receive a webhook, then map the JSON to a record with Jackson: fields, tables, confidence.

// Where teams use it

What Java developers build on an OCR API

Spring Boot services

A service forwards each uploaded file to the OCR API, maps the JSON to a POJO, and persists clean fields with JPA.

Enterprise back office

A batch job on the JVM reads invoices and forms overnight and posts totals and line items into an ERP.

Accounts payable

A worker pulls invoices from a queue, extracts totals and line items, and routes them for approval with no manual entry.

Table capture

A data service turns scanned statements and reports into rows and columns for a warehouse or a report.

Message-driven pipelines

A Kafka consumer reads a document on arrival and publishes structured JSON downstream with no OCR engine to host.

Compliance and KYC

A verification flow reads IDs and forms, checks confidence scores, and routes low-confidence values to a human.

// Frequently asked

OCR API Java FAQ

How do I use an OCR API in Java?
You call it over HTTPS with java.net.http.HttpClient, which is built into the JDK since Java 11, so there is no client library to add. Send your API key as a Bearer token, POST the PDF or image to the upload endpoint, start extraction, then read the JSON back. With DocuOCR that is three short requests: upload returns a file_id, extract returns a job hash, and a GET returns named fields, tables and confidence scores as JSON your Java code parses into objects.
What is the best OCR library for Java?
It depends on what you need back. For plain text from a clean image on your own hardware, tess4j wraps the open-source Tesseract engine through JNA and is free. For structured fields and tables from real business documents, a hosted API returns cleaner data with no native binary to install: DocuOCR posts a document and returns named fields and table rows as JSON from one HttpClient call, while Google Cloud Vision, AWS Textract and Azure each ship a Maven SDK with more setup.
Is tess4j good enough for production OCR?
tess4j is a solid choice for plain text from clean scans and it runs offline for free, which no hosted API does. The tradeoffs are real: you ship and maintain the native Tesseract binary and tessdata files on every machine, accuracy drops on faint scans, unusual fonts and dense tables, and it returns a block of text, not the invoice number or total as a labeled value. Once you are writing regular expressions over the tess4j output to pull out fields, you have outgrown raw OCR and want a document extraction API.
Do I need a Maven dependency to call an OCR API?
No. A REST OCR API only needs an HTTPS client, and the JDK ships java.net.http.HttpClient from Java 11, so DocuOCR is called with nothing added to your pom.xml. The cloud providers publish Maven SDKs (com.google.cloud:google-cloud-vision, com.google.cloud:google-cloud-document-ai, software.amazon.awssdk:textract, com.azure:azure-ai-documentintelligence) that help if you are already on that cloud but add a dependency and its own auth model.
How do I handle rate limits and retries in Java?
Wrap each request in try or catch and add exponential backoff on HTTP 429 and 5xx responses, honoring any Retry-After header. For batches, submit documents through an ExecutorService or a bounded thread pool rather than a tight loop so you stay under the per-second limit. DocuOCR also supports webhooks, so at scale you register a callback URL instead of polling and let results arrive as each document finishes. HttpClient also offers sendAsync if you prefer non-blocking calls.
Can a Java OCR API read scanned PDFs and images?
Yes. A document OCR API reads native PDFs, scanned PDFs and image files (PNG, JPG, TIFF, WEBP) through one endpoint. For a digital PDF it reads the embedded text, and for a scan it runs OCR on the page image. DocuOCR returns the same JSON either way, so your Java code does not branch on how the document was created.
How do I OCR files from a Spring Boot service?
Inject an HttpClient bean or use a RestClient, forward the uploaded MultipartFile to the OCR API, start extraction and map the JSON response onto a record or POJO with Jackson. Keep the API key in configuration, never in code. DocuOCR returns named fields and table rows, so your service layer receives clean typed data rather than a text blob it has to parse, which fits neatly into a Spring pipeline that writes to a database or queue.
How much does an OCR API cost to run from Java?
You pay per page or per document, not per line of code, so the language does not change the price. Raw text OCR runs about $1.50 per 1,000 pages at Azure, AWS and Google, Mistral is $4 per 1,000, and structured extraction runs higher. DocuOCR bills per page on a plan, roughly $14 to $20 per 1,000 pages, and includes fields and tables in that rate. tess4j has no per-page fee because it runs on your own hardware, trading the cost for setup and accuracy.
What does a Java OCR API return?
A good document OCR API returns JSON, which you map to Java objects with Jackson or Gson. DocuOCR returns named fields (invoice number, date, totals), table rows with cells kept in order, and a confidence score on each value so you can flag anything below a threshold for review. Raw OCR APIs like Google Cloud Vision and tess4j return text and bounding boxes but not named fields, so you write the mapping yourself.

Ship OCR from Java this afternoon

Generate an API key, paste the three HttpClient calls above, and read structured JSON back in minutes. No native binary to install, no SDK to learn, and a free tier to test the integration before you pay per page.