From 5c92501eefd7bdf33ee20a384ae6f0f23903e9a8 Mon Sep 17 00:00:00 2001 From: Igor Date: Fri, 4 Sep 2026 16:45:05 +0200 Subject: [PATCH 1/6] 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/6] 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" From a899e5982c589ac348e138fbd903669f83a12864 Mon Sep 17 00:00:00 2001 From: igor-pdftron <109149823+igor-pdftron@users.noreply.github.com> Date: Mon, 7 Sep 2026 15:24:53 +0200 Subject: [PATCH 3/6] Update PageNeedsOCR sample calls to match refactored single-argument signature OCRModule::PageNeedsOCR no longer takes a separate PDFDoc argument since the document is derived internally from the Page. Updated the Python, Ruby, PHP, and Go OCRTest samples to call PageNeedsOCR(page, process_invisible_text) accordingly. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- Samples/OCRTest/GO/OCR_test.go | 2 +- Samples/OCRTest/PHP/OCRTest.php | 2 +- Samples/OCRTest/PYTHON/OCRTest.py | 2 +- Samples/OCRTest/RUBY/OCRTest.rb | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Samples/OCRTest/GO/OCR_test.go b/Samples/OCRTest/GO/OCR_test.go index 2b9e4266..333e6e17 100644 --- a/Samples/OCRTest/GO/OCR_test.go +++ b/Samples/OCRTest/GO/OCR_test.go @@ -250,7 +250,7 @@ func TestOCR(t *testing.T) { // 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) + needsOCR := OCRModulePageNeedsOCR(page, false) if needsOCR { fmt.Println("Example 7: page 1 of german_kids_song.pdf needs OCR") } else { diff --git a/Samples/OCRTest/PHP/OCRTest.php b/Samples/OCRTest/PHP/OCRTest.php index 95ce005f..b888edd0 100644 --- a/Samples/OCRTest/PHP/OCRTest.php +++ b/Samples/OCRTest/PHP/OCRTest.php @@ -240,7 +240,7 @@ // 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); + $needs_ocr = OCRModule::PageNeedsOCR($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 diff --git a/Samples/OCRTest/PYTHON/OCRTest.py b/Samples/OCRTest/PYTHON/OCRTest.py index 0036557e..5c02fe10 100644 --- a/Samples/OCRTest/PYTHON/OCRTest.py +++ b/Samples/OCRTest/PYTHON/OCRTest.py @@ -239,7 +239,7 @@ def main(): # 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) + needs_ocr = OCRModule.PageNeedsOCR(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 diff --git a/Samples/OCRTest/RUBY/OCRTest.rb b/Samples/OCRTest/RUBY/OCRTest.rb index 3001defd..379e32f5 100644 --- a/Samples/OCRTest/RUBY/OCRTest.rb +++ b/Samples/OCRTest/RUBY/OCRTest.rb @@ -263,7 +263,7 @@ # 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) + needs_ocr = OCRModule.PageNeedsOCR(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 From 4a0d7115139885cf473176476578874dd81fde20 Mon Sep 17 00:00:00 2001 From: igor-pdftron <109149823+igor-pdftron@users.noreply.github.com> Date: Tue, 8 Sep 2026 15:00:18 +0200 Subject: [PATCH 4/6] Update PageNeedsOCR sample to use lorem_ipsum.pdf/newsletter.pdf as test fixtures Use lorem_ipsum.pdf (needs OCR) as the positive test case and newsletter.pdf (has extractable text) as the negative test case in the PageNeedsOCR sample (Example 7) across Python, Ruby, PHP, and Go. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- Samples/OCRTest/GO/OCR_test.go | 53 ++++++++++++++++++++++--------- Samples/OCRTest/PHP/OCRTest.php | 45 ++++++++++++++++++-------- Samples/OCRTest/PYTHON/OCRTest.py | 43 +++++++++++++++++-------- Samples/OCRTest/RUBY/OCRTest.rb | 46 +++++++++++++++++++-------- 4 files changed, 130 insertions(+), 57 deletions(-) diff --git a/Samples/OCRTest/GO/OCR_test.go b/Samples/OCRTest/GO/OCR_test.go index 333e6e17..4061b928 100644 --- a/Samples/OCRTest/GO/OCR_test.go +++ b/Samples/OCRTest/GO/OCR_test.go @@ -237,36 +237,59 @@ func TestOCR(t *testing.T) { // processing on pages that already contain extractable text // -------------------------------------------------------------------------------- - // A) Open the .pdf document - - doc = NewPDFDoc(inputPath + "german_kids_song.pdf") + // A) Positive test: lorem_ipsum.pdf is a scanned document with no extractable text, so it should need OCR. - // B) Grab the page to be checked + scannedDoc := NewPDFDoc(inputPath + "lorem_ipsum.pdf") - page := doc.GetPage(1) + scannedPage := scannedDoc.GetPage(1) - // C) Ask OCRModule whether the page needs OCR. Passing process_invisible_text as false + // 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(page, false) - if needsOCR { - fmt.Println("Example 7: page 1 of german_kids_song.pdf needs OCR") + scannedNeedsOCR := OCRModulePageNeedsOCR(scannedPage, false) + if scannedNeedsOCR { + fmt.Println("Example 7 (positive test): page 1 of lorem_ipsum.pdf needs OCR") + } else { + fmt.Println("Example 7 (positive test): page 1 of lorem_ipsum.pdf does not need OCR") + } + + // Only run OCR if it is actually needed + + if scannedNeedsOCR { + opts = NewOCROptions() + if use_iris { + opts.SetOCREngine("iris") + } + opts.AddLang("eng") + OCRModuleProcessPDF(scannedDoc, opts) + scannedDoc.Save(outputPath+"lorem_ipsum_conditional.pdf", uint(0)) + } + + // B) Negative test: newsletter.pdf already contains extractable text, so it should not need OCR. + + newsletterDoc := NewPDFDoc(inputPath + "newsletter.pdf") + + newsletterPage := newsletterDoc.GetPage(1) + + newsletterNeedsOCR := OCRModulePageNeedsOCR(newsletterPage, false) + if newsletterNeedsOCR { + fmt.Println("Example 7 (negative test): page 1 of newsletter.pdf needs OCR") } else { - fmt.Println("Example 7: page 1 of german_kids_song.pdf does not need OCR") + fmt.Println("Example 7 (negative test): page 1 of newsletter.pdf does not need OCR") } - // D) Only run OCR if it is actually needed + // Only run OCR if it is actually needed - if needsOCR { + if newsletterNeedsOCR { opts = NewOCROptions() if use_iris { opts.SetOCREngine("iris") } - opts.AddLang("deu") - OCRModuleProcessPDF(doc, opts) - doc.Save(outputPath+"german_kids_song_conditional.pdf", uint(0)) + opts.AddLang("eng") + OCRModuleProcessPDF(newsletterDoc, opts) + newsletterDoc.Save(outputPath+"newsletter_conditional.pdf", uint(0)) } PDFNetTerminate() diff --git a/Samples/OCRTest/PHP/OCRTest.php b/Samples/OCRTest/PHP/OCRTest.php index b888edd0..1a698899 100644 --- a/Samples/OCRTest/PHP/OCRTest.php +++ b/Samples/OCRTest/PHP/OCRTest.php @@ -227,32 +227,49 @@ //-------------------------------------------------------------------------------- // 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 + // A) Positive test: lorem_ipsum.pdf is a scanned document with no extractable text, so it should need OCR. - $page = $doc->GetPage(1); + $scanned_doc = new PDFDoc($input_path."lorem_ipsum.pdf"); + $scanned_page = $scanned_doc->GetPage(1); - // C) Ask OCRModule whether the page needs OCR. Passing process_invisible_text as false + // 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($page, false); - echo "Example 7: page 1 of german_kids_song.pdf ".($needs_ocr ? "needs" : "does not need")." OCR \n"; + $scanned_needs_ocr = OCRModule::PageNeedsOCR($scanned_page, false); + echo "Example 7 (positive test): page 1 of lorem_ipsum.pdf ".($scanned_needs_ocr ? "needs" : "does not need")." OCR \n"; + + // Only run OCR if it is actually needed + + if ($scanned_needs_ocr) { + $opts = new OCROptions(); + if ($use_iris) { + $opts->SetOCREngine("iris"); + } + $opts->AddLang("eng"); + OCRModule::ProcessPDF($scanned_doc, $opts); + $scanned_doc->Save($output_path."lorem_ipsum_conditional.pdf", 0); + } + + // B) Negative test: newsletter.pdf already contains extractable text, so it should not need OCR. + + $newsletter_doc = new PDFDoc($input_path."newsletter.pdf"); + $newsletter_page = $newsletter_doc->GetPage(1); + + $newsletter_needs_ocr = OCRModule::PageNeedsOCR($newsletter_page, false); + echo "Example 7 (negative test): page 1 of newsletter.pdf ".($newsletter_needs_ocr ? "needs" : "does not need")." OCR \n"; - // D) Only run OCR if it is actually needed + // Only run OCR if it is actually needed - if ($needs_ocr) { + if ($newsletter_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); + $opts->AddLang("eng"); + OCRModule::ProcessPDF($newsletter_doc, $opts); + $newsletter_doc->Save($output_path."newsletter_conditional.pdf", 0); } echo "Done. \n"; diff --git a/Samples/OCRTest/PYTHON/OCRTest.py b/Samples/OCRTest/PYTHON/OCRTest.py index 5c02fe10..108d978d 100644 --- a/Samples/OCRTest/PYTHON/OCRTest.py +++ b/Samples/OCRTest/PYTHON/OCRTest.py @@ -226,31 +226,46 @@ def main(): # processing on pages that already contain extractable text # -------------------------------------------------------------------------------- - # A) Open the .pdf document - - doc = PDFDoc(input_path + "german_kids_song.pdf") + # A) Positive test: lorem_ipsum.pdf is a scanned document with no extractable text, so it should need OCR. - # B) Grab the page to be checked + scanned_doc = PDFDoc(input_path + "lorem_ipsum.pdf") + scanned_page = scanned_doc.GetPage(1) - page = doc.GetPage(1) - - # C) Ask OCRModule whether the page needs OCR. Passing process_invisible_text as False + # 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(page, False) - print("Example 7: page 1 of german_kids_song.pdf " + ("needs" if needs_ocr else "does not need") + " OCR") + scanned_needs_ocr = OCRModule.PageNeedsOCR(scanned_page, False) + print("Example 7 (positive test): page 1 of lorem_ipsum.pdf " + ("needs" if scanned_needs_ocr else "does not need") + " OCR") + + # Only run OCR if it is actually needed + + if scanned_needs_ocr: + opts = OCROptions() + if use_iris: + opts.SetOCREngine("iris") + opts.AddLang("eng") + OCRModule.ProcessPDF(scanned_doc, opts) + scanned_doc.Save(output_path + "lorem_ipsum_conditional.pdf", 0) + + # B) Negative test: newsletter.pdf already contains extractable text, so it should not need OCR. + + newsletter_doc = PDFDoc(input_path + "newsletter.pdf") + newsletter_page = newsletter_doc.GetPage(1) + + newsletter_needs_ocr = OCRModule.PageNeedsOCR(newsletter_page, False) + print("Example 7 (negative test): page 1 of newsletter.pdf " + ("needs" if newsletter_needs_ocr else "does not need") + " OCR") - # D) Only run OCR if it is actually needed + # Only run OCR if it is actually needed - if needs_ocr: + if newsletter_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) + opts.AddLang("eng") + OCRModule.ProcessPDF(newsletter_doc, opts) + newsletter_doc.Save(output_path + "newsletter_conditional.pdf", 0) PDFNet.Terminate() diff --git a/Samples/OCRTest/RUBY/OCRTest.rb b/Samples/OCRTest/RUBY/OCRTest.rb index 379e32f5..3f3a4e13 100644 --- a/Samples/OCRTest/RUBY/OCRTest.rb +++ b/Samples/OCRTest/RUBY/OCRTest.rb @@ -253,31 +253,49 @@ # processing on pages that already contain extractable text # -------------------------------------------------------------------------------- - # A) Open the .pdf document - doc = PDFDoc.new(input_path + "german_kids_song.pdf") + # A) Positive test: lorem_ipsum.pdf is a scanned document with no extractable text, so it should need OCR. + scanned_doc = PDFDoc.new(input_path + "lorem_ipsum.pdf") + scanned_page = scanned_doc.GetPage(1) - # B) Grab the page to be checked - page = doc.GetPage(1) - - # C) Ask OCRModule whether the page needs OCR. Passing process_invisible_text as false + # 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(page, false) - puts "Example 7: page 1 of german_kids_song.pdf #{needs_ocr ? 'needs' : 'does not need'} OCR" + scanned_needs_ocr = OCRModule.PageNeedsOCR(scanned_page, false) + puts "Example 7 (positive test): page 1 of lorem_ipsum.pdf #{scanned_needs_ocr ? 'needs' : 'does not need'} OCR" - # D) Only run OCR if it is actually needed - if needs_ocr + # Only run OCR if it is actually needed + if scanned_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) + opts.AddLang("eng") + OCRModule.ProcessPDF(scanned_doc, opts) + scanned_doc.Save(output_path + "lorem_ipsum_conditional.pdf", 0) end - doc.Close + scanned_doc.Close + + # B) Negative test: newsletter.pdf already contains extractable text, so it should not need OCR. + newsletter_doc = PDFDoc.new(input_path + "newsletter.pdf") + newsletter_page = newsletter_doc.GetPage(1) + + newsletter_needs_ocr = OCRModule.PageNeedsOCR(newsletter_page, false) + puts "Example 7 (negative test): page 1 of newsletter.pdf #{newsletter_needs_ocr ? 'needs' : 'does not need'} OCR" + + # Only run OCR if it is actually needed + if newsletter_needs_ocr + opts = OCROptions.new + if use_iris + opts.SetOCREngine("iris") + end + opts.AddLang("eng") + OCRModule.ProcessPDF(newsletter_doc, opts) + newsletter_doc.Save(output_path + "newsletter_conditional.pdf", 0) + end + + newsletter_doc.Close end rescue Exception=>e From f57085fb0a51dcb016634e842814980201e9edcd Mon Sep 17 00:00:00 2001 From: igor-pdftron <109149823+igor-pdftron@users.noreply.github.com> Date: Wed, 16 Sep 2026 19:05:04 +0200 Subject: [PATCH 5/6] Rename OCRModule::PageNeedsOCR parameter process_invisible_text to accept_invisible_text in sample comments Updates the PageNeedsOCR sample comments in Go, PHP, Python, and Ruby to match the renamed parameter in the underlying PDFNetC/PDFNet API. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- Samples/OCRTest/GO/OCR_test.go | 2 +- Samples/OCRTest/PHP/OCRTest.php | 2 +- Samples/OCRTest/PYTHON/OCRTest.py | 2 +- Samples/OCRTest/RUBY/OCRTest.rb | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Samples/OCRTest/GO/OCR_test.go b/Samples/OCRTest/GO/OCR_test.go index 4061b928..d5e66955 100644 --- a/Samples/OCRTest/GO/OCR_test.go +++ b/Samples/OCRTest/GO/OCR_test.go @@ -243,7 +243,7 @@ func TestOCR(t *testing.T) { scannedPage := scannedDoc.GetPage(1) - // Ask OCRModule whether the page needs OCR. Passing process_invisible_text as false + // Ask OCRModule whether the page needs OCR. Passing accept_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. diff --git a/Samples/OCRTest/PHP/OCRTest.php b/Samples/OCRTest/PHP/OCRTest.php index 1a698899..518ed15d 100644 --- a/Samples/OCRTest/PHP/OCRTest.php +++ b/Samples/OCRTest/PHP/OCRTest.php @@ -232,7 +232,7 @@ $scanned_doc = new PDFDoc($input_path."lorem_ipsum.pdf"); $scanned_page = $scanned_doc->GetPage(1); - // Ask OCRModule whether the page needs OCR. Passing process_invisible_text as false + // Ask OCRModule whether the page needs OCR. Passing accept_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. diff --git a/Samples/OCRTest/PYTHON/OCRTest.py b/Samples/OCRTest/PYTHON/OCRTest.py index 108d978d..7ba16a18 100644 --- a/Samples/OCRTest/PYTHON/OCRTest.py +++ b/Samples/OCRTest/PYTHON/OCRTest.py @@ -231,7 +231,7 @@ def main(): scanned_doc = PDFDoc(input_path + "lorem_ipsum.pdf") scanned_page = scanned_doc.GetPage(1) - # Ask OCRModule whether the page needs OCR. Passing process_invisible_text as False + # Ask OCRModule whether the page needs OCR. Passing accept_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. diff --git a/Samples/OCRTest/RUBY/OCRTest.rb b/Samples/OCRTest/RUBY/OCRTest.rb index 3f3a4e13..9290a165 100644 --- a/Samples/OCRTest/RUBY/OCRTest.rb +++ b/Samples/OCRTest/RUBY/OCRTest.rb @@ -257,7 +257,7 @@ scanned_doc = PDFDoc.new(input_path + "lorem_ipsum.pdf") scanned_page = scanned_doc.GetPage(1) - # Ask OCRModule whether the page needs OCR. Passing process_invisible_text as false + # Ask OCRModule whether the page needs OCR. Passing accept_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. From 0d90134260d99a5a7d180619767fc27a000282cf Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Thu, 17 Sep 2026 11:36:30 -0700 Subject: [PATCH 6/6] Fix OCRTest sample relative input paths --- Samples/OCRTest/GO/OCR_test.go | 4 ++-- Samples/OCRTest/PHP/OCRTest.php | 4 ++-- Samples/OCRTest/PYTHON/OCRTest.py | 4 ++-- Samples/OCRTest/RUBY/OCRTest.rb | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Samples/OCRTest/GO/OCR_test.go b/Samples/OCRTest/GO/OCR_test.go index d5e66955..9857d34d 100644 --- a/Samples/OCRTest/GO/OCR_test.go +++ b/Samples/OCRTest/GO/OCR_test.go @@ -239,7 +239,7 @@ func TestOCR(t *testing.T) { // A) Positive test: lorem_ipsum.pdf is a scanned document with no extractable text, so it should need OCR. - scannedDoc := NewPDFDoc(inputPath + "lorem_ipsum.pdf") + scannedDoc := NewPDFDoc(inputPath + "../lorem_ipsum.pdf") scannedPage := scannedDoc.GetPage(1) @@ -269,7 +269,7 @@ func TestOCR(t *testing.T) { // B) Negative test: newsletter.pdf already contains extractable text, so it should not need OCR. - newsletterDoc := NewPDFDoc(inputPath + "newsletter.pdf") + newsletterDoc := NewPDFDoc(inputPath + "../newsletter.pdf") newsletterPage := newsletterDoc.GetPage(1) diff --git a/Samples/OCRTest/PHP/OCRTest.php b/Samples/OCRTest/PHP/OCRTest.php index 518ed15d..e41e5e4c 100644 --- a/Samples/OCRTest/PHP/OCRTest.php +++ b/Samples/OCRTest/PHP/OCRTest.php @@ -229,7 +229,7 @@ // A) Positive test: lorem_ipsum.pdf is a scanned document with no extractable text, so it should need OCR. - $scanned_doc = new PDFDoc($input_path."lorem_ipsum.pdf"); + $scanned_doc = new PDFDoc($input_path."../lorem_ipsum.pdf"); $scanned_page = $scanned_doc->GetPage(1); // Ask OCRModule whether the page needs OCR. Passing accept_invisible_text as false @@ -254,7 +254,7 @@ // B) Negative test: newsletter.pdf already contains extractable text, so it should not need OCR. - $newsletter_doc = new PDFDoc($input_path."newsletter.pdf"); + $newsletter_doc = new PDFDoc($input_path."../newsletter.pdf"); $newsletter_page = $newsletter_doc->GetPage(1); $newsletter_needs_ocr = OCRModule::PageNeedsOCR($newsletter_page, false); diff --git a/Samples/OCRTest/PYTHON/OCRTest.py b/Samples/OCRTest/PYTHON/OCRTest.py index 7ba16a18..45a481b1 100644 --- a/Samples/OCRTest/PYTHON/OCRTest.py +++ b/Samples/OCRTest/PYTHON/OCRTest.py @@ -228,7 +228,7 @@ def main(): # A) Positive test: lorem_ipsum.pdf is a scanned document with no extractable text, so it should need OCR. - scanned_doc = PDFDoc(input_path + "lorem_ipsum.pdf") + scanned_doc = PDFDoc(input_path + "../lorem_ipsum.pdf") scanned_page = scanned_doc.GetPage(1) # Ask OCRModule whether the page needs OCR. Passing accept_invisible_text as False @@ -251,7 +251,7 @@ def main(): # B) Negative test: newsletter.pdf already contains extractable text, so it should not need OCR. - newsletter_doc = PDFDoc(input_path + "newsletter.pdf") + newsletter_doc = PDFDoc(input_path + "../newsletter.pdf") newsletter_page = newsletter_doc.GetPage(1) newsletter_needs_ocr = OCRModule.PageNeedsOCR(newsletter_page, False) diff --git a/Samples/OCRTest/RUBY/OCRTest.rb b/Samples/OCRTest/RUBY/OCRTest.rb index 9290a165..9be11983 100644 --- a/Samples/OCRTest/RUBY/OCRTest.rb +++ b/Samples/OCRTest/RUBY/OCRTest.rb @@ -254,7 +254,7 @@ # -------------------------------------------------------------------------------- # A) Positive test: lorem_ipsum.pdf is a scanned document with no extractable text, so it should need OCR. - scanned_doc = PDFDoc.new(input_path + "lorem_ipsum.pdf") + scanned_doc = PDFDoc.new(input_path + "../lorem_ipsum.pdf") scanned_page = scanned_doc.GetPage(1) # Ask OCRModule whether the page needs OCR. Passing accept_invisible_text as false @@ -278,7 +278,7 @@ scanned_doc.Close # B) Negative test: newsletter.pdf already contains extractable text, so it should not need OCR. - newsletter_doc = PDFDoc.new(input_path + "newsletter.pdf") + newsletter_doc = PDFDoc.new(input_path + "../newsletter.pdf") newsletter_page = newsletter_doc.GetPage(1) newsletter_needs_ocr = OCRModule.PageNeedsOCR(newsletter_page, false)