diff --git a/examples/requirements.txt b/examples/requirements.txt index 931b69f..0d57b0b 100644 --- a/examples/requirements.txt +++ b/examples/requirements.txt @@ -1,4 +1,5 @@ aspose-pdf lxml pydicom -pandas \ No newline at end of file +pandas +pytesseract \ No newline at end of file diff --git a/examples/working_with_documents/example_create_pdf_document.py b/examples/working_with_documents/example_create_pdf_document.py index e57d372..999cf09 100644 --- a/examples/working_with_documents/example_create_pdf_document.py +++ b/examples/working_with_documents/example_create_pdf_document.py @@ -37,6 +37,38 @@ def create_searchable_pdf(input_pdf, output_pdf): image_file.unlink(missing_ok=True) +def create_searchable_document(infile, outfile, image_file_path, page_number=1): + """Use optical character recognition (OCR) to create a searchable PDF document. + + Args: + infile (str): The path to the input PDF file. + outfile (str): The path to the output searchable PDF file. + image_file_path (str): The path to the intermediate image file. + page_number (int): The page number to process. + + Returns: + None + """ + + # Requires: pip install pytesseract + # Also ensure the Tesseract OCR engine is installed and available on your system PATH. + import pytesseract + + try: + image_stream = io.FileIO(image_file_path, 'x') + document = ap.Document(infile) + resolution = ap.devices.Resolution(300) + png_device = ap.devices.PngDevice(resolution) + png_device.process(document.pages[page_number], image_stream) + image_stream.close() + pdf = pytesseract.image_to_pdf_or_hocr(image_file_path, extension='pdf') + document = ap.Document(io.BytesIO(pdf)) + document.save(outfile) + finally: + image_file = Path(image_file_path) + image_file.unlink(missing_ok=True) + + def run_all_examples(data_dir=None, license_path=None): """Run PDF creation examples and report status.""" set_license(license_path) diff --git a/sample_data/working_with_documents/input/create_searchable_document.pdf b/sample_data/working_with_documents/input/create_searchable_document.pdf new file mode 100644 index 0000000..a3daa09 Binary files /dev/null and b/sample_data/working_with_documents/input/create_searchable_document.pdf differ