Skip to content

OCR integration #13313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 59 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
a51e3b0
Initial implementation using tess4j
Kaan0029 Jun 12, 2025
aca504a
merge
Siedlerchr Jun 12, 2025
48ffb06
add brew to jna path
Siedlerchr Jun 12, 2025
5a256ae
Adapted Exception handling and configured tessdata variable
Kaan0029 Jun 19, 2025
f80cec8
addressed mentor code feedback after 19.06.25
Kaan0029 Jul 3, 2025
db1f577
Merge branch 'upstream-main' into gsoc-ocr-tess4j-initial-implementation
Kaan0029 Jul 10, 2025
4020d3a
fix java modules
Siedlerchr Jul 10, 2025
4987978
fix tessdata path
Siedlerchr Jul 10, 2025
9842dd4
fix tessdata path amd module infor for lept4j
Siedlerchr Jul 10, 2025
8b133e6
fix tessdata path amd module infor for lept4j
Siedlerchr Jul 10, 2025
42704ea
fix module access
Siedlerchr Jul 10, 2025
a64e1ea
fix module access
Siedlerchr Jul 10, 2025
6069bc1
fix module access
Siedlerchr Jul 10, 2025
5734252
Avoid throwing exception in configureTessdata when tessdata is missing
Kaan0029 Jul 10, 2025
ab98a3a
Use Path.of instead of Paths.get for modern Java style
Kaan0029 Jul 10, 2025
62dce25
Add OCR tessdata path setting to AI preferences tab and add abstracti…
Kaan0029 Jul 10, 2025
0949300
Merge remote-tracking branch 'origin/gsoc-ocr-tess4j-initial-implemen…
Kaan0029 Jul 10, 2025
e4e45f3
fix(ocr): restore deleted files
InAnYan Jul 10, 2025
15272a6
fix(ocr): restore deleted IconThemes
InAnYan Jul 10, 2025
14332af
Update gradle to 9.1.0-jabref
koppor Jul 11, 2025
5488deb
fix(submodules): fix submodules
InAnYan Jul 11, 2025
26a8500
Update gradle
koppor Jul 11, 2025
e0da447
Merge branch 'gsoc-ocr-tess4j-initial-implementation' of https://gith…
koppor Jul 11, 2025
f1a06ac
Update gradle
koppor Jul 11, 2025
42d34ca
Workaround for gradle bug
calixtus Jul 12, 2025
3ffa1c1
Merge remote-tracking branch 'upstream/main' into gsoc-ocr-tess4j-ini…
calixtus Jul 12, 2025
59d87a2
Workaround for gradle bug
calixtus Jul 12, 2025
bc36a94
Fix submodules
calixtus Jul 12, 2025
e7a043d
Added Debugging Testing file for jna
Kaan0029 Jul 13, 2025
caa48f1
Attempt to enforce specific jna version
Kaan0029 Jul 13, 2025
9b51b23
Merge remote-tracking branch 'origin/gsoc-ocr-tess4j-initial-implemen…
Kaan0029 Jul 13, 2025
c115803
Fix jna include
calixtus Jul 13, 2025
f16c1f4
Add jna-jpms
koppor Jul 13, 2025
8c51016
fix arm64 crash
Kaan0029 Jul 15, 2025
5843c69
fix tessdata path issue
Kaan0029 Jul 15, 2025
1bd2e5b
revert previous tessdata change
Kaan0029 Jul 15, 2025
c7fcc6b
fixed lifecycle
Kaan0029 Jul 16, 2025
a8bab25
fixed bug related to life cylce issue
Kaan0029 Jul 16, 2025
9b9c7b7
Deleted unnecessary comments
Kaan0029 Jul 16, 2025
edc0343
use StringUtil.isBlank
Kaan0029 Jul 16, 2025
695e2b4
Use @NonNull
Kaan0029 Jul 16, 2025
e5e651e
Added TODO comment
Kaan0029 Jul 16, 2025
9ffdbff
delete /tessdata from .gitignore
Kaan0029 Jul 16, 2025
facb463
delete TO-DO comments in FilePreferences
Kaan0029 Jul 16, 2025
265a312
Delete unnecessary comment in OcrProvider
Kaan0029 Jul 16, 2025
e4d16c2
Adjust AI preferences and make them fit with jabref conventions
Kaan0029 Jul 17, 2025
cd52669
Merge remote-tracking branch 'upstream/main' into gsoc-ocr-tess4j-ini…
calixtus Jul 17, 2025
2eb2df2
Fix submodules
calixtus Jul 17, 2025
809e5a1
Remove error_log.txt
calixtus Jul 17, 2025
d0a20e7
Fix gradle issues
calixtus Jul 17, 2025
ce0328d
Resolve uncaught exception issue in JabRefGUI.stop()
Kaan0029 Jul 21, 2025
da6b8c8
fix tessdata path issue by removing getParent call
Kaan0029 Jul 21, 2025
a4e5d20
delete OcrException and return optional or result (OcrResult) instead
Kaan0029 Jul 21, 2025
af085b9
adjust commenting style to be in line with JabRef conventions
Kaan0029 Jul 21, 2025
dd8d211
implemented string constant for 'tessdata' as it it used more than once
Kaan0029 Jul 21, 2025
a3a90a8
separate final and non-final fields
Kaan0029 Jul 21, 2025
e7dba5e
store whole exception object instead of only exception message
Kaan0029 Jul 21, 2025
d13ea24
implement OcrBackgroundTask class
Kaan0029 Jul 21, 2025
14cf72d
add method for getting property in filepreferences
Kaan0029 Jul 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -581,3 +581,5 @@ CHANGELOG.html

# some strange gradle/IntelliJ extension
extension 'reporting' property 'baseDirectory'

tessdata/
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,8 @@ private void handleItemMouseClick(LinkedFileViewModel linkedFile, MouseEvent eve
bibEntry,
viewModel,
contextCommandFactory,
multiContextCommandFactory
multiContextCommandFactory,
taskExecutor
);

ContextMenu contextMenu = contextMenuFactory.createForSelection(listView.getSelectionModel().getSelectedItems());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import javafx.collections.ObservableList;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;

import org.jabref.gui.DialogService;
Expand All @@ -10,7 +11,10 @@
import org.jabref.gui.copyfiles.CopySingleFileAction;
import org.jabref.gui.fieldeditors.LinkedFileViewModel;
import org.jabref.gui.fieldeditors.LinkedFilesEditorViewModel;
import org.jabref.gui.linkedfile.OcrAction;
import org.jabref.gui.preferences.GuiPreferences;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.TaskExecutor;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;

Expand All @@ -25,21 +29,24 @@ public class ContextMenuFactory {
private final LinkedFilesEditorViewModel viewModel;
private final SingleContextCommandFactory singleCommandFactory;
private final MultiContextCommandFactory multiCommandFactory;
private final TaskExecutor taskExecutor;

public ContextMenuFactory(DialogService dialogService,
GuiPreferences preferences,
BibDatabaseContext databaseContext,
ObservableOptionalValue<BibEntry> bibEntry,
LinkedFilesEditorViewModel viewModel,
SingleContextCommandFactory singleCommandFactory,
MultiContextCommandFactory multiCommandFactory) {
MultiContextCommandFactory multiCommandFactory,
TaskExecutor taskExecutor) {
this.dialogService = dialogService;
this.preferences = preferences;
this.databaseContext = databaseContext;
this.bibEntry = bibEntry;
this.viewModel = viewModel;
this.singleCommandFactory = singleCommandFactory;
this.multiCommandFactory = multiCommandFactory;
this.taskExecutor = taskExecutor;
}

public ContextMenu createForSelection(ObservableList<LinkedFileViewModel> selectedFiles) {
Expand Down Expand Up @@ -86,9 +93,45 @@ private ContextMenu createContextMenuForFile(LinkedFileViewModel linkedFile) {
factory.createMenuItem(StandardActions.DELETE_FILE, singleCommandFactory.build(StandardActions.DELETE_FILE, linkedFile))
);

// Add OCR menu item for PDF files
if (linkedFile.getFile().getFileType().equalsIgnoreCase("pdf")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to search a method in class FilesUtil (or FileUtil) like isPDF

menu.getItems().add(new SeparatorMenuItem());

MenuItem ocrItem = createOcrMenuItem(linkedFile);
menu.getItems().add(ocrItem);
}

return menu;
}

/**
* Creates the OCR menu item for a PDF file.
* The menu item is only enabled if the PDF file exists on disk.
*
* @param linkedFile The linked PDF file
* @return MenuItem configured for OCR action
*/
private MenuItem createOcrMenuItem(LinkedFileViewModel linkedFile) {
MenuItem ocrItem = new MenuItem(Localization.lang("Extract text (OCR)"));

// Create the OCR action
OcrAction ocrAction = new OcrAction(
Comment on lines +121 to +122
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment is trivial and does not add any new information beyond what is clearly visible in the code. It simply restates what the code is doing.

linkedFile.getFile(),
databaseContext,
dialogService,
preferences.getFilePreferences(),
taskExecutor
);

// Set the action to execute when clicked
ocrItem.setOnAction(event -> ocrAction.execute());
Comment on lines +131 to +132
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment is redundant and merely describes what the code does without providing additional context or reasoning. The code is self-explanatory.


// Disable if the action is not executable (file doesn't exist)
ocrItem.disableProperty().bind(ocrAction.executableProperty().not());
Comment on lines +134 to +135
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment restates what is evident from the code itself without providing additional insight or explanation about the underlying logic or design decision.


return ocrItem;
}

@FunctionalInterface
public interface SingleContextCommandFactory {
ContextAction build(StandardActions action, LinkedFileViewModel file);
Expand Down
106 changes: 106 additions & 0 deletions jabgui/src/main/java/org/jabref/gui/linkedfile/OcrAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package org.jabref.gui.linkedfile;

import org.jabref.gui.DialogService;
import org.jabref.gui.StateManager;
import org.jabref.gui.actions.Action;
import org.jabref.gui.actions.ActionHelper;
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.logic.util.BackgroundTask;
import org.jabref.logic.util.TaskExecutor;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.ocr.OcrService;
import org.jabref.logic.ocr.OcrException;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.LinkedFile;
import org.jabref.logic.FilePreferences;

import java.nio.file.Path;
import java.util.Optional;

/**
* Action for performing OCR (Optical Character Recognition) on linked PDF files.
* <p>
* This action extracts text content from PDF files that are attached to BibTeX entries.
* It runs the OCR process in a background thread to keep the UI responsive and provides
* user feedback through dialogs and notifications.
* <p>
* The action follows JabRef's command pattern and can be triggered from context menus.
* It includes built-in validation to ensure it's only enabled for PDF files that exist on disk.
*
* @see OcrService
* @see org.jabref.gui.actions.SimpleCommand
*/

// Personal Note: Add more doc in between later

public class OcrAction extends SimpleCommand {

private final LinkedFile linkedFile;
private final BibDatabaseContext databaseContext;
private final DialogService dialogService;
private final FilePreferences filePreferences;
private final TaskExecutor taskExecutor;

public OcrAction(LinkedFile linkedFile,
BibDatabaseContext databaseContext,
DialogService dialogService,
FilePreferences filePreferences,
TaskExecutor taskExecutor) {
this.linkedFile = linkedFile;
this.databaseContext = databaseContext;
this.dialogService = dialogService;
this.filePreferences = filePreferences;
this.taskExecutor = taskExecutor;

// Only executable for existing PDF files
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment merely restates what the code does without providing additional insight or reasoning, violating the principle that comments should add new information.

this.executable.set(
linkedFile.getFileType().equalsIgnoreCase("pdf") &&
linkedFile.findIn(databaseContext, filePreferences).isPresent()
);
}

@Override
public void execute() {
Optional<Path> filePath = linkedFile.findIn(databaseContext, filePreferences);

if (filePath.isEmpty()) {
dialogService.showErrorDialogAndWait(
Localization.lang("File not found"),
Localization.lang("Could not locate the PDF file on disk.")
);
return;
}

dialogService.notify(Localization.lang("Performing OCR..."));

BackgroundTask.wrap(() -> {
OcrService ocrService = new OcrService();
return ocrService.performOcr(filePath.get());
})
.onSuccess(extractedText -> {
if (extractedText.isEmpty()) {
dialogService.showInformationDialogAndWait(
Localization.lang("OCR Complete"),
Localization.lang("No text was found in the PDF.")
);
} else {
// For now, just show preview
String preview = extractedText.length() > 1000
? extractedText.substring(0, 1000) + "..."
: extractedText;

dialogService.showInformationDialogAndWait(
Localization.lang("OCR Result"),
preview
);
}
})
.onFailure(exception -> {
dialogService.showErrorDialogAndWait(
Localization.lang("OCR failed"),
exception.getMessage()
);
})
.executeWith(taskExecutor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.jabref.gui.fieldeditors.LinkedFileViewModel;
import org.jabref.gui.fieldeditors.LinkedFilesEditorViewModel;
import org.jabref.gui.preferences.GuiPreferences;
import org.jabref.logic.util.TaskExecutor;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.LinkedFile;
Expand Down Expand Up @@ -41,6 +42,7 @@ public class ContextMenuFactoryTest {
private ContextMenuFactory factory;
private ContextMenuFactory.SingleContextCommandFactory singleCommandFactory;
private ContextMenuFactory.MultiContextCommandFactory multiCommandFactory;
private TaskExecutor taskExecutor;

@BeforeAll
public static void initToolkit() {
Expand Down Expand Up @@ -78,7 +80,8 @@ public void setUp() {
bibEntry,
viewModel,
singleCommandFactory,
multiCommandFactory
multiCommandFactory,
taskExecutor
);
}

Expand Down
3 changes: 3 additions & 0 deletions jablib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ dependencies {
// Required for LocalizationConsistencyTest
testImplementation("org.testfx:testfx-core:4.0.16-alpha")
testImplementation("org.testfx:testfx-junit5:4.0.16-alpha")

// OCR support
implementation("net.sourceforge.tess4j:tess4j:5.15.0")
}
/*
jacoco {
Expand Down
2 changes: 2 additions & 0 deletions jablib/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
exports org.jabref.logic.crawler;
exports org.jabref.logic.git;
exports org.jabref.logic.pseudonymization;
exports org.jabref.logic.ocr;
exports org.jabref.logic.citation.repository;

requires java.base;
Expand Down Expand Up @@ -252,5 +253,6 @@
requires mslinks;
requires org.antlr.antlr4.runtime;
requires org.libreoffice.uno;
requires tess4j;
// endregion
}
28 changes: 28 additions & 0 deletions jablib/src/main/java/org/jabref/logic/ocr/OcrException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.jabref.logic.ocr;

/**
* Exception thrown when OCR operations fail.
* This exception wraps lower-level OCR engine exceptions to provide
* a consistent interface for error handling throughout JabRef.
*/
public class OcrException extends Exception {

/**
* Constructs an OcrException with a message and underlying cause.
*
* @param message Descriptive error message
* @param cause The underlying exception that caused this error
*/
public OcrException(String message, Throwable cause) {
super(message, cause);
}

/**
* Constructs an OcrException with only a message.
*
* @param message Descriptive error message
*/
public OcrException(String message) {
super(message);
}
}
78 changes: 78 additions & 0 deletions jablib/src/main/java/org/jabref/logic/ocr/OcrService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.jabref.logic.ocr;

import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.jabref.model.strings.StringUtil; // JabRef utility class
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.nio.file.Path;

/**
* Service for performing Optical Character Recognition (OCR) on PDF files.
* This class provides a high-level interface to OCR functionality,
* abstracting away the specific OCR engine implementation details.
*/
public class OcrService {
private static final Logger LOGGER = LoggerFactory.getLogger(OcrService.class);

// The OCR engine instance
private final Tesseract tesseract;

/**
* Constructs a new OcrService with default settings.
* Currently uses Tesseract with English language support.
*/
public OcrService() {
this.tesseract = new Tesseract();

// Configure Tesseract
tesseract.setLanguage("eng");

// TODO: This path needs to be configurable and bundled properly
// For now, we'll use a relative path that works during development
tesseract.setDatapath("tessdata");

LOGGER.debug("Initialized OcrService with Tesseract");
}

/**
* Performs OCR on a PDF file and returns the extracted text.
*
* @param pdfPath Path to the PDF file to process
* @return The extracted text, or empty string if no text found
* @throws OcrException if OCR processing fails
*/
public String performOcr(Path pdfPath) throws OcrException {
// Validate input
if (pdfPath == null) {
throw new OcrException("PDF path cannot be null");
}

File pdfFile = pdfPath.toFile();
if (!pdfFile.exists()) {
throw new OcrException("PDF file does not exist: " + pdfPath);
}

try {
LOGGER.info("Starting OCR for file: {}", pdfFile.getName());

// Perform OCR
String result = tesseract.doOCR(pdfFile);

// Clean up the result (remove extra whitespace, etc.)
result = StringUtil.isBlank(result) ? "" : result.trim();

LOGGER.info("OCR completed successfully. Extracted {} characters", result.length());
return result;

} catch (TesseractException e) {
LOGGER.error("OCR failed for file: {}", pdfFile.getName(), e);
throw new OcrException(
"Failed to perform OCR on file: " + pdfFile.getName() +
". Error: " + e.getMessage(), e
);
}
}
}
8 changes: 8 additions & 0 deletions jablib/src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,14 @@ Extract\ references\ from\ file\ (online)=Extract references from file (online)
Extract\ References\ (offline)=Extract References (offline)
Extract\ References\ (online)=Extract References (online)

Extract\ text\ (OCR)=Extract text (OCR)
Performing\ OCR...=Performing OCR...
OCR\ Complete=OCR Complete
OCR\ Result=OCR Result
OCR\ failed=OCR failed
No\ text\ was\ found\ in\ the\ PDF.=No text was found in the PDF.
Could\ not\ locate\ the\ PDF\ file\ on\ disk.=Could not locate the PDF file on disk.

Processing...=Processing...
Processing\ "%0"...=Processing "%0"...
Processing\ PDF(s)=Processing PDF(s)
Expand Down
Loading