Home : TIFF : Separate (split) a Multipage TIFF Into Multiple Single Page TIFF Files, Preserving All Tiff Tags
Q10378 - HOWTO: Separate (split) a Multipage TIFF Into Multiple Single Page TIFF Files, Preserving All Tiff Tags

Our PdfDocument object has built in functions for Combine and Separate which gives it the ability to easily combine multiple PDFs into one or separate a multipage PDF out into individual one pages.

Our TiffDocument has a Combine feature that works the same to allow combining of multiple TIFF files into a single multipage document, but it does not have a built in Separate (split) feature. However, with a bit of code, this can be accomplished quite handily:

/// <summary>
/// Splits the tiff with the name passed in to multiple files
///
/// if in file is c:\someDir\foo.tif and has 3 pages, the result will be
/// c:\someDir\foo_0000.tif
/// c:\someDir\foo_0001.tif
/// c:\someDir\foo_0002.tif
/// </summary>
/// <param name="inFile"></param>
private static void SeparateTiff(string inFile)
{
    // This reads the original TIFF and preserves tags
    TiffDocument sourceDoc = new TiffDocument(inFile);

    // since we're splitting, we will iterate through each page
    for (int i = 0; i < sourceDoc.Pages.Count; i++)
    {
        // This stuff is done out longhand so you can see how we're building the filename
        string filePath = Path.GetDirectoryName(inFile);
        string fileName = Path.GetFileNameWithoutExtension(inFile);
        string fileExt = Path.GetExtension(inFile);
        string counter = i.ToString("D4");
        string pageFileName = filePath + "\\" + fileName + "_" + counter + fileExt;

        // This is where the actual work is
        // currentPageDoc is going to be loaded with one page (image and all tags)
        // then saved out to the new file
        TiffDocument currentPageDoc = new TiffDocument();
        currentPageDoc.Pages.Add(sourceDoc.Pages[i]);
        currentPageDoc.Save(pageFileName);
    }
}

 

Related Articles
No Related Articles Available.

Article Attachments
No Attachments Available.

Related External Links
No Related Links Available.
Help us improve this article...
What did you think of this article?

poor 
1
2
3
4
5
6
7
8
9
10

 excellent
Tell us why you rated the content this way. (optional)
 
Approved Comments...
No user comments available for this article.

Powered By InstantKB.NET v1.3
Copyright © 2002, 2018. InstantASP Ltd. All Rights Reserved