From 5c92501eefd7bdf33ee20a384ae6f0f23903e9a8 Mon Sep 17 00:00:00 2001 From: Igor Date: Fri, 4 Sep 2026 16:45:05 +0200 Subject: [PATCH 1/2] Add OCRModule::PageNeedsOCR usage sample (Example 7) to OCRTest for Python, Ruby, PHP, Go Adds a new usage example to the OCRTest sample apps demonstrating OCRModule::PageNeedsOCR, mirroring the C/C++/Java/ObjC/.NET samples added in ApryseSDK/PDFTronCore. The example opens german_kids_song.pdf, checks whether page 1 needs OCR, and conditionally runs OCR only if needed, saving to a *_conditional output file. No .i interface file changes are required: PDFNetPHP.i, PDFNetPython.i, PDFNetRuby.i, and PDFTronGo/pdftron.i all %include PDF/OCRModule.h unmodified, so the new PageNeedsOCR binding is picked up automatically once built against an updated PDFNetC header/binary. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- Samples/OCRTest/GO/OCR_test.go | 35 +++++++++++++++++++++++++++++++ Samples/OCRTest/PHP/OCRTest.php | 30 ++++++++++++++++++++++++++ Samples/OCRTest/PYTHON/OCRTest.py | 29 +++++++++++++++++++++++++ Samples/OCRTest/RUBY/OCRTest.rb | 29 +++++++++++++++++++++++++ 4 files changed, 123 insertions(+) diff --git a/Samples/OCRTest/GO/OCR_test.go b/Samples/OCRTest/GO/OCR_test.go index ddfdb1b1..96c803e7 100644 --- a/Samples/OCRTest/GO/OCR_test.go +++ b/Samples/OCRTest/GO/OCR_test.go @@ -233,6 +233,41 @@ func TestOCR(t *testing.T) { doc.Save(outputPath+"physics.pdf", uint(0)) fmt.Println("Example 6: extracting and applying OCR XML from physics.tif") + // Example 7) Check whether a page needs OCR before running it, to avoid unnecessary OCR + // processing on pages that already contain extractable text + // -------------------------------------------------------------------------------- + + // A) Open the .pdf document + + doc = NewPDFDoc(inputPath + "german_kids_song.pdf") + + // B) Grab the page to be checked + + page := doc.GetPage(1) + + // C) Ask OCRModule whether the page needs OCR. Passing process_invisible_text as true + // means an invisible text layer (e.g. from a prior OCR pass) is enough to consider the + // page as not needing OCR, while false ignores invisible text and only considers visible content. + + needsOCR := OCRModulePageNeedsOCR(doc, page, false) + if needsOCR { + fmt.Println("Example 7: page 1 of german_kids_song.pdf needs OCR") + } else { + fmt.Println("Example 7: page 1 of german_kids_song.pdf does not need OCR") + } + + // D) Only run OCR if it is actually needed + + if needsOCR { + opts = NewOCROptions() + if use_iris { + opts.SetOCREngine("iris") + } + opts.AddLang("deu") + OCRModuleProcessPDF(doc, opts) + doc.Save(outputPath+"german_kids_song_conditional.pdf", uint(0)) + } + PDFNetTerminate() } } diff --git a/Samples/OCRTest/PHP/OCRTest.php b/Samples/OCRTest/PHP/OCRTest.php index 101e2d56..16f53a5f 100644 --- a/Samples/OCRTest/PHP/OCRTest.php +++ b/Samples/OCRTest/PHP/OCRTest.php @@ -224,6 +224,36 @@ echo "Example 6: extracting and applying OCR XML from physics.tif \n"; + //-------------------------------------------------------------------------------- + // Example 7) Check whether a page needs OCR before running it, to avoid unnecessary OCR processing on pages that already contain extractable text + + // A) Open the .pdf document + + $doc = new PDFDoc($input_path."german_kids_song.pdf"); + + // B) Grab the page to be checked + + $page = $doc->GetPage(1); + + // C) Ask OCRModule whether the page needs OCR. Passing process_invisible_text as true + // means an invisible text layer (e.g. from a prior OCR pass) is enough to consider the + // page as not needing OCR, while false ignores invisible text and only considers visible content. + + $needs_ocr = OCRModule::PageNeedsOCR($doc, $page, false); + echo "Example 7: page 1 of german_kids_song.pdf ".($needs_ocr ? "needs" : "does not need")." OCR \n"; + + // D) Only run OCR if it is actually needed + + if ($needs_ocr) { + $opts = new OCROptions(); + if ($use_iris) { + $opts->SetOCREngine("iris"); + } + $opts->AddLang("deu"); + OCRModule::ProcessPDF($doc, $opts); + $doc->Save($output_path."german_kids_song_conditional.pdf", 0); + } + echo "Done. \n"; } diff --git a/Samples/OCRTest/PYTHON/OCRTest.py b/Samples/OCRTest/PYTHON/OCRTest.py index 397471e1..dee9f2bc 100644 --- a/Samples/OCRTest/PYTHON/OCRTest.py +++ b/Samples/OCRTest/PYTHON/OCRTest.py @@ -222,6 +222,35 @@ def main(): doc.Save(output_path + "physics.pdf", 0) print("Example 6: extracting and applying OCR XML from physics.tif") + # Example 7) Check whether a page needs OCR before running it, to avoid unnecessary OCR + # processing on pages that already contain extractable text + # -------------------------------------------------------------------------------- + + # A) Open the .pdf document + + doc = PDFDoc(input_path + "german_kids_song.pdf") + + # B) Grab the page to be checked + + page = doc.GetPage(1) + + # C) Ask OCRModule whether the page needs OCR. Passing process_invisible_text as True + # means an invisible text layer (e.g. from a prior OCR pass) is enough to consider the + # page as not needing OCR, while False ignores invisible text and only considers visible content. + + needs_ocr = OCRModule.PageNeedsOCR(doc, page, False) + print("Example 7: page 1 of german_kids_song.pdf " + ("needs" if needs_ocr else "does not need") + " OCR") + + # D) Only run OCR if it is actually needed + + if needs_ocr: + opts = OCROptions() + if use_iris: + opts.SetOCREngine("iris") + opts.AddLang("deu") + OCRModule.ProcessPDF(doc, opts) + doc.Save(output_path + "german_kids_song_conditional.pdf", 0) + PDFNet.Terminate() diff --git a/Samples/OCRTest/RUBY/OCRTest.rb b/Samples/OCRTest/RUBY/OCRTest.rb index 1226f617..dcd9cbd6 100644 --- a/Samples/OCRTest/RUBY/OCRTest.rb +++ b/Samples/OCRTest/RUBY/OCRTest.rb @@ -249,6 +249,35 @@ doc.Close + # Example 7) Check whether a page needs OCR before running it, to avoid unnecessary OCR + # processing on pages that already contain extractable text + # -------------------------------------------------------------------------------- + + # A) Open the .pdf document + doc = PDFDoc.new(input_path + "german_kids_song.pdf") + + # B) Grab the page to be checked + page = doc.GetPage(1) + + # C) Ask OCRModule whether the page needs OCR. Passing process_invisible_text as true + # means an invisible text layer (e.g. from a prior OCR pass) is enough to consider the + # page as not needing OCR, while false ignores invisible text and only considers visible content. + needs_ocr = OCRModule.PageNeedsOCR(doc, page, false) + puts "Example 7: page 1 of german_kids_song.pdf #{needs_ocr ? 'needs' : 'does not need'} OCR" + + # D) Only run OCR if it is actually needed + if needs_ocr + opts = OCROptions.new + if use_iris + opts.SetOCREngine("iris") + end + opts.AddLang("deu") + OCRModule.ProcessPDF(doc, opts) + doc.Save(output_path + "german_kids_song_conditional.pdf", 0) + end + + doc.Close + end rescue Exception=>e puts e From 14ecd326f817c26d813c085e71bdba72b9e13969 Mon Sep 17 00:00:00 2001 From: igor-pdftron <109149823+igor-pdftron@users.noreply.github.com> Date: Fri, 4 Sep 2026 18:45:56 +0200 Subject: [PATCH 2/2] Fix misleading process_invisible_text comment in OCRTest samples Corrected the Example 7 comment to accurately describe passing process_invisible_text as false, matching the same fix applied to the PDFTronCore OCRTest samples after Copilot code review flagged the same true/false mismatch. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- Samples/OCRTest/GO/OCR_test.go | 7 ++++--- Samples/OCRTest/PHP/OCRTest.php | 7 ++++--- Samples/OCRTest/PYTHON/OCRTest.py | 7 ++++--- Samples/OCRTest/RUBY/OCRTest.rb | 7 ++++--- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/Samples/OCRTest/GO/OCR_test.go b/Samples/OCRTest/GO/OCR_test.go index 96c803e7..2b9e4266 100644 --- a/Samples/OCRTest/GO/OCR_test.go +++ b/Samples/OCRTest/GO/OCR_test.go @@ -245,9 +245,10 @@ func TestOCR(t *testing.T) { page := doc.GetPage(1) - // C) Ask OCRModule whether the page needs OCR. Passing process_invisible_text as true - // means an invisible text layer (e.g. from a prior OCR pass) is enough to consider the - // page as not needing OCR, while false ignores invisible text and only considers visible content. + // C) Ask OCRModule whether the page needs OCR. Passing process_invisible_text as false + // means invisible text (e.g. from a prior OCR pass) is ignored and only visible content + // is considered, while true would treat an existing invisible text layer as sufficient + // to consider the page as not needing OCR. needsOCR := OCRModulePageNeedsOCR(doc, page, false) if needsOCR { diff --git a/Samples/OCRTest/PHP/OCRTest.php b/Samples/OCRTest/PHP/OCRTest.php index 16f53a5f..95ce005f 100644 --- a/Samples/OCRTest/PHP/OCRTest.php +++ b/Samples/OCRTest/PHP/OCRTest.php @@ -235,9 +235,10 @@ $page = $doc->GetPage(1); - // C) Ask OCRModule whether the page needs OCR. Passing process_invisible_text as true - // means an invisible text layer (e.g. from a prior OCR pass) is enough to consider the - // page as not needing OCR, while false ignores invisible text and only considers visible content. + // C) Ask OCRModule whether the page needs OCR. Passing process_invisible_text as false + // means invisible text (e.g. from a prior OCR pass) is ignored and only visible content + // is considered, while true would treat an existing invisible text layer as sufficient + // to consider the page as not needing OCR. $needs_ocr = OCRModule::PageNeedsOCR($doc, $page, false); echo "Example 7: page 1 of german_kids_song.pdf ".($needs_ocr ? "needs" : "does not need")." OCR \n"; diff --git a/Samples/OCRTest/PYTHON/OCRTest.py b/Samples/OCRTest/PYTHON/OCRTest.py index dee9f2bc..0036557e 100644 --- a/Samples/OCRTest/PYTHON/OCRTest.py +++ b/Samples/OCRTest/PYTHON/OCRTest.py @@ -234,9 +234,10 @@ def main(): page = doc.GetPage(1) - # C) Ask OCRModule whether the page needs OCR. Passing process_invisible_text as True - # means an invisible text layer (e.g. from a prior OCR pass) is enough to consider the - # page as not needing OCR, while False ignores invisible text and only considers visible content. + # C) Ask OCRModule whether the page needs OCR. Passing process_invisible_text as False + # means invisible text (e.g. from a prior OCR pass) is ignored and only visible content + # is considered, while True would treat an existing invisible text layer as sufficient + # to consider the page as not needing OCR. needs_ocr = OCRModule.PageNeedsOCR(doc, page, False) print("Example 7: page 1 of german_kids_song.pdf " + ("needs" if needs_ocr else "does not need") + " OCR") diff --git a/Samples/OCRTest/RUBY/OCRTest.rb b/Samples/OCRTest/RUBY/OCRTest.rb index dcd9cbd6..3001defd 100644 --- a/Samples/OCRTest/RUBY/OCRTest.rb +++ b/Samples/OCRTest/RUBY/OCRTest.rb @@ -259,9 +259,10 @@ # B) Grab the page to be checked page = doc.GetPage(1) - # C) Ask OCRModule whether the page needs OCR. Passing process_invisible_text as true - # means an invisible text layer (e.g. from a prior OCR pass) is enough to consider the - # page as not needing OCR, while false ignores invisible text and only considers visible content. + # C) Ask OCRModule whether the page needs OCR. Passing process_invisible_text as false + # means invisible text (e.g. from a prior OCR pass) is ignored and only visible content + # is considered, while true would treat an existing invisible text layer as sufficient + # to consider the page as not needing OCR. needs_ocr = OCRModule.PageNeedsOCR(doc, page, false) puts "Example 7: page 1 of german_kids_song.pdf #{needs_ocr ? 'needs' : 'does not need'} OCR"