Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions Samples/OCRTest/GO/OCR_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
48 changes: 48 additions & 0 deletions Samples/OCRTest/PHP/OCRTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";

}
Expand Down
45 changes: 45 additions & 0 deletions Samples/OCRTest/PYTHON/OCRTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
48 changes: 48 additions & 0 deletions Samples/OCRTest/RUBY/OCRTest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down