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 |
|
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.
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>\LEADTOOLS23\Bin\Xcode\Frameworks\macOS
:
Leadtools.Codecs.Fax.framework
Leadtools.Codecs.Tif.framework
Leadtools.Codecs.framework
Leadtools.Document.Core.framework
Leadtools.Document.Writer.framework
Leadtools.Drawing.framework
Leadtools.framework
Leadtools.ImageProcessing.Color.framework
Leadtools.ImageProcessing.Core.framework
Leadtools.ImageProcessing.Effects.framework
Leadtools.ImageProcessing.Utilities.framework
Leadtools.Ocr.framework
Leadtools.Pdf.framework
Leadtools.Svg.framework
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.
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.
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/LEADTOOLS23/Resources/Images/OCR1.TIF"
let output = "/path/to/LEADTOOLS23/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/LEADTOOLS23/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.")
}
}
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.
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.