Skip to content
Open
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
36 changes: 36 additions & 0 deletions Samples/OCRTest/GO/OCR_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
31 changes: 31 additions & 0 deletions Samples/OCRTest/PHP/OCRTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";

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


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