This tutorial shows how to save each page of a multipage image into separate image files in a C# .NET Core application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to split multipage image files using the RasterCodecs class in a C# .NET Core Console application. |
Completion Time | 30 minutes |
Visual Studio Project | Download tutorial project (1 KB) |
Platform | C# .NET Core Console Application |
IDE | Visual Studio 2017, 2019 |
Development 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 Split a Multipage Image File Into Separate Files - C# .NET Core 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. References can be added via NuGet packages.
This tutorial requires the following NuGet package:
Leadtools.Formats.Raster.Common
For a complete list of which DLL files are required for your application, refer to Files to be Included With 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 NuGet 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.
In the Solution Explorer, open Program.cs
. Add the following statements to the using
block at the top of Program.cs
.
using System;
using System.IO;
using Leadtools;
using Leadtools.Codecs;
In the Program
class, add a new string fileName
, below the set license code. Set the new string value to the file path containing the multipage image file. Add a new method named SplitFile(string inputFile)
to the Program
class. Call it in the Main()
method after the fileName
string value, as shown below.
static void Main(string[] args)
{
if (!SetLicense())
Console.WriteLine("Error setting license");
else
Console.WriteLine("License file set successfully");
string multipageFile = @"FILE PATH TO MULTIPAGE IMAGE FILE";
SplitFile(multipageFile);
}
Add the code below to the SplitFile()
method to load each page individually from a multipage image file and export the page as its own image file.
static void SplitFile(string inputFile)
{
using (RasterCodecs codecs = new RasterCodecs())
{
int totalPages = codecs.GetTotalPages(inputFile);
for (int page = 1; page <= totalPages; page++)
{
string outputFile = $@"C:\LEADTOOLS21\Resources\Images\{Path.GetFileNameWithoutExtension(inputFile)}_page{page}.png";
using (RasterImage image = codecs.Load(inputFile, page))
codecs.Save(image, outputFile, RasterImageFormat.Png, 0);
}
}
}
Note
To test the code above, use a multipage file, such as TIFF or PDF. If such file is not available, create one by following the Create a Multipage File from Multiple Images Tutorial.
Because the
RasterCodecs
class implementsIDisposable
, make sure it is in ausing
statement for proper disposal.
Run the project by pressing F5, or by selecting Debug -> Start Debugging.
If the steps were followed correctly, the console appears and the application creates new PNG image files from each page of the multipage image file.
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 RasterImage
and RasterCodecs
classes.