Utility_Apps/Shell/AddOCR/AddOCR.bat

83 lines
1.5 KiB
Batchfile

@echo off
setlocal enabledelayedexpansion
if "%~1"=="" (
echo No file selected.
pause
exit /b
)
set INPUT="%~1"
set OUTPUT="%~dpn1_ocr.pdf"
set OCRMYPDF="C:\Python312\Scripts\ocrmypdf.exe"
set TMPERR="%TEMP%\ocr_err.txt"
echo Running OCRmyPDF on:
echo %INPUT%
echo.
REM --- Run OCR and capture stderr ---
%OCRMYPDF% %INPUT% %OUTPUT% 2> %TMPERR%
set EXITCODE=%ERRORLEVEL%
REM --- Detect TaggedPDFError by reading stderr content ---
findstr /C:"TaggedPDFError" %TMPERR% >nul
if %ERRORLEVEL%==0 goto tagged_error
REM --- Detect PriorOcrFoundError by exitcode 6 ---
if %EXITCODE%==6 goto prior_ocr
echo OCR completed successfully.
goto end
:tagged_error
echo.
echo *** TaggedPDFError detected ***
echo This PDF has real text, but you may still add an OCR layer.
echo.
goto prompt
:prior_ocr
echo.
echo *** Prior OCR detected ***
echo This PDF already contains text.
echo.
goto prompt
:prompt
echo Choose how to proceed:
echo R = Use --redo-ocr (raster areas only)
echo F = Use --force-ocr (overwrite all text)
echo S = Skip OCR
echo.
set /p CHOICE=Enter choice (R/F/S):
if /I "%CHOICE%"=="R" (
echo.
echo Re-running with --redo-ocr ...
%OCRMYPDF% --redo-ocr %INPUT% %OUTPUT%
goto end
)
if /I "%CHOICE%"=="F" (
echo.
echo Re-running with --force-ocr ...
%OCRMYPDF% --force-ocr %INPUT% %OUTPUT%
goto end
)
echo Skipping OCR re-run.
goto end
:end
echo.
echo OCR layer added to:
echo %OUTPUT%
echo.
del %TMPERR% >nul 2>&1
pause