diff --git a/Samples/OCRTest/GO/OCR_test.go b/Samples/OCRTest/GO/OCR_test.go index ddfdb1b1..9857d34d 100644 --- a/Samples/OCRTest/GO/OCR_test.go +++ b/Samples/OCRTest/GO/OCR_test.go @@ -233,6 +233,65 @@ 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) Positive test: lorem_ipsum.pdf is a scanned document with no extractable text, so it should need OCR. + + scannedDoc := NewPDFDoc(inputPath + "../lorem_ipsum.pdf") + + scannedPage := scannedDoc.GetPage(1) + + // 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. + + 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 (negative test): page 1 of newsletter.pdf does not need OCR") + } + + // Only run OCR if it is actually needed + + if newsletterNeedsOCR { + opts = NewOCROptions() + if use_iris { + opts.SetOCREngine("iris") + } + 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 101e2d56..e41e5e4c 100644 --- a/Samples/OCRTest/PHP/OCRTest.php +++ b/Samples/OCRTest/PHP/OCRTest.php @@ -224,6 +224,54 @@ 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) 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_page = $scanned_doc->GetPage(1); + + // 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. + + $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"; + + // Only run OCR if it is actually needed + + if ($newsletter_needs_ocr) { + $opts = new OCROptions(); + if ($use_iris) { + $opts->SetOCREngine("iris"); + } + $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 397471e1..45a481b1 100644 --- a/Samples/OCRTest/PYTHON/OCRTest.py +++ b/Samples/OCRTest/PYTHON/OCRTest.py @@ -222,6 +222,51 @@ 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) 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_page = scanned_doc.GetPage(1) + + # 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. + + 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") + + # Only run OCR if it is actually needed + + if newsletter_needs_ocr: + opts = OCROptions() + if use_iris: + opts.SetOCREngine("iris") + 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 1226f617..9be11983 100644 --- a/Samples/OCRTest/RUBY/OCRTest.rb +++ b/Samples/OCRTest/RUBY/OCRTest.rb @@ -249,6 +249,54 @@ 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) 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) + + # 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. + 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" + + # Only run OCR if it is actually needed + if scanned_needs_ocr + opts = OCROptions.new + if use_iris + opts.SetOCREngine("iris") + end + opts.AddLang("eng") + OCRModule.ProcessPDF(scanned_doc, opts) + scanned_doc.Save(output_path + "lorem_ipsum_conditional.pdf", 0) + end + + 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 puts e