Split a Multipage Image File Into Separate Files - macOS Swift Console

This tutorial shows how to load a multipage file and save each page from it to separate image files in a macOS Swift Console application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to split multipage image files in a macOS Swift Console application.
Completion Time 20 minutes
Visual Studio Project Download tutorial project (5 KB)
Platform macOS Swift Console Application
IDE Xcode
Development 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 Split a Multipage Image File Into Separate Files - 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>\LEADTOOLS23\Bin\Xcode\Frameworks\macOS:

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

#import <Leadtools.Codecs/Leadtools.Codecs.h> 

For a complete list of which 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 Split Image Code

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

Using the Project Navigator, open main.swift. Add a new string multipageFile and set it to the multipage image file you wish to split, as shown below.

SetLicense() 
 
let multipageFile: String = "<INSTALL_DIR>/LEADTOOLS23/Resources/Images/merged.tif" 
SplitFile(file: multipageFile) 

Note: The code below requires a multipage file, such as a TIFF or PDF file. If you do not have one available, you can create one by following the steps in the Create a Multipage File from Multiple Images tutorial.

Add a new function named SplitFile(file: String) and call it below the string value created above. Add the following code to load the multipage file and export each page as its own image to file.

C#
func SplitFile(file: String) { 
    let codecs: LTRasterCodecs = LTRasterCodecs() 
    var error: NSError? 
    let totalPages: Int = codecs.totalPages(in: file, error: &error) 
    if error != nil { 
        print(error!.localizedDescription) 
    } 
    let fileName = URL(string: file)!.deletingPathExtension().lastPathComponent 
    for page in 1...totalPages { 
        let outputFile: String = "<INSTALL_DIR>/LEADTOOLS23/Resources/Images/\(page).png" 
        do { 
            let image: LTRasterImage = try codecs.load(file: file, pageNumber: page) 
            try codecs.save(image, file: outputFile, format: LTRasterImageFormat.png, bitsPerPixel: 0) 
            print("Saved file: \(URL(string: outputFile)!.deletingPathExtension().lastPathComponent)") 
        } catch { 
            print(error.localizedDescription) 
        } 
    } 
} 

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 creates new PNG file for each page of the multipage file.

Console display of extracted information.

Wrap-up

This tutorial showed how to add the necessary references to load all the pages of a TIFF image file and split them into separate PNG images using the LTRasterImage and LTRasterCodecs classes.

See Also

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


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