Split a Multipage Image File Into Separate Files - Java

This tutorial shows how to create a Java application that uses the RasterCodecs class to save each page of a multipage image into a separate image file.

Summary This tutorial covers how to split multipage image files in a Java Console application.
Completion Time 30 minutes
Eclipse Project Download tutorial project (4 KB)
Platform Java Application
IDE Eclipse
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 - Java tutorial.

Create the Project and Add the LEADTOOLS References

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

The references needed depend upon the purpose of the project. References can be added by local .jar files located at <INSTALL_DIR>\LEADTOOLS21\Bin\Java.

For this project, the following references are needed:

This tutorial uses LEADTOOLS Codec library support. For a complete list of which JAR files are required for your application, refer to Files to be Included with your Java 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:


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.

Open the Main.java class in the Project Explorer. Add the following statements to the import block at the top.

// import block at the top 
import java.io.*; 
import java.nio.file.*; 
import leadtools.*; 
import leadtools.codecs.*; 

Add a new method called SplitFile(). Call this new method in the main() method after the SetLicense(); method call. Add the below code to the SplitFile() method to split a multipage file into separate PNG files. For this tutorial we used this multipage TIFF file.

static void SplitFile() 
   String multipageFile = "C:\\LEADTOOLS21\\Resources\\Images\\merged.tif"; 
   RasterCodecs codecs = new RasterCodecs(); 
   int totalPages = codecs.getTotalPages(multipageFile); 
   for (int page = 1; page <= totalPages; page++) 
      File tempFile = new File(multipageFile); 
      String fileWithoutExt = tempFile.getName(); 
      fileWithoutExt = fileWithoutExt.substring(0, fileWithoutExt.lastIndexOf(".")); 
      String outputFile = "C:\\LEADTOOLS21\\Resources\\Images\\" + fileWithoutExt + "_page" + page + ".png"; 
      RasterImage image = codecs.load(multipageFile, page); 
      codecs.save(image, outputFile, RasterImageFormat.PNG, 0); 

Run the Project

Run the project by pressing Ctrl + F11, or by selecting Run -> Run.

If the steps were followed correctly, the application runs and creates new PNG files. Each page of the sample TIFF file should be created as a PNG image file, with the page number appended to the name.


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.

See Also

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