diff --git a/Samples/OCRTest/GO/OCR_test.go b/Samples/OCRTest/GO/OCR_test.go index ddfdb1b1..2b9e4266 100644 --- a/Samples/OCRTest/GO/OCR_test.go +++ b/Samples/OCRTest/GO/OCR_test.go @@ -233,6 +233,42 @@ 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 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 { + 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..95ce005f 100644 --- a/Samples/OCRTest/PHP/OCRTest.php +++ b/Samples/OCRTest/PHP/OCRTest.php @@ -224,6 +224,37 @@ 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 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"; + + // 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..0036557e 100644 --- a/Samples/OCRTest/PYTHON/OCRTest.py +++ b/Samples/OCRTest/PYTHON/OCRTest.py @@ -222,6 +222,36 @@ 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 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") + + # 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..3001defd 100644 --- a/Samples/OCRTest/RUBY/OCRTest.rb +++ b/Samples/OCRTest/RUBY/OCRTest.rb @@ -249,6 +249,36 @@ 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 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" + + # 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