Convert Images to Searchable PDF with OCR - macOS Swift Console

This tutorial shows how to convert an image to a searchable PDF with the LEAD OCR Engine in a Swift macOS Console application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to convert an image to a searchable PDF with the LEAD OCR Engine.
Completion Time 30 minutes
Project Download tutorial project (5 KB)
Platform Swift macOS Console Application
IDE Xcode
Runtime License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Convert Images to Searchable PDF with OCR - macOS Swift Console tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. This tutorial requires the following Framework references located at <INSTALL_DIR>\LEADTOOLS22\Bin\Xcode\Frameworks\macOS:

Edit the Leadtools-Bridging-Header.h file to add the following imports:

#import <Leadtools.Document.Writer/Leadtools.Document.Writer.h> 
#import <Leadtools.Ocr/Leadtools.Ocr.h> 

For a complete list of which Framework files are required for your application, refer to Files to be Included in your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Note

Adding LEADTOOLS references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Add the OCR Code

With the project created, the references added, and the license set, coding can begin.

Using the Project Navigator, open main.swift. To initialize and run the OCR Engine, add a new OCR(input: String, output: String) function. Call it beneath SetLicense(), as shown below. Add the following code to the OCR() function to initialize the OCR Engine and convert the given image to a searchable PDF file.

SetLicense() 
 
let input = "/path/to/LEADTOOLS22/Resources/Images/OCR1.TIF" 
let output = "/path/to/LEADTOOLS22/Resources/Images/OCR1.pdf" 
 
OCR(input: input,output: output) 
 
func OCR(input: String, output: String) { 
    let ocrEngine = LTOcrEngineManager.createEngine(LTOcrEngineType.LEAD) 
    do { 
        try ocrEngine.startup(rasterCodecs: nil, documentWriter: nil, workDirectory: nil, engineDirectory: "/path/to/LEADTOOLS22/Bin/Common/OcrLEADRuntime") 
    } catch { 
        print("Unable to start OCR Engine. \n \(error.localizedDescription)") 
    } 
    do { 
        try ocrEngine.autoRecognizeManager.run(imageFileName: input, documentFileName: output, zonesFileName: nil, format: LTDocumentFormat.pdf) 
        print("OCR output saved to \(output)") 
    } catch { 
        print("Unable to save file as PDF.") 
    } 
} 

Run the Project

Clean the project to clear any errors by selecting Product -> Clean Build Folder or Shift + Command + K.

Run the project by selecting Product -> Run or Command + R.

If the steps were followed correctly, the application runs and converts the given image to PDF format and saves it to the specified location, as a searchable PDF.

Screenshot of the console display.

Wrap-up

This tutorial showed how to create a simple macOS Swift Console application that initializes the LEAD OCR Engine, takes a specified input file, and outputs the recognition results to the specified output file in the specified format.

See Also

Help Version 22.0.2021.10.18
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.