Splitting a Multi-page TIFF File Tutorial

Take the following steps to start a project:

  1. Start Visual Studio.

  2. Choose File -> New -> Project... from the menu.

  3. In the New Project dialog box, choose either Visual C# Projects or VB Projects in the Projects Type list. Then, choose Windows Forms Application in the templates list.

  4. Name the project "Split Multi-page TIFF Files" If desired, provide a new location for your project. Click OK.

  5. In the Solution Explorer panel, right-click on the References node and select Add Reference... from the context menu. In the Add Reference dialog box, browse to the "C:\LEADTOOLS 19\Bin\DotNet4\Win32" folder and select the following DLLs:

    • Leadtools.dll
    • Leadtools.Codecs.dll
    • Leadtools.Codecs.Fax.dll
    • Leadtools.Codecs.Tif.dll

    Click the Select button and then press the OK button to add the above DLLs to the application.

  6. Drag and drop a Button from the Toolbox onto the Form1 UI designer. Change the following properties:

    Text Name
    Split TIFF buttonSplit
  7. Switch to the code view of Form1 (right-click Form1 in the Solution Explorer and select View Code) and add the following lines at the beginning of the file:

    VB
    Imports System.IO 
    Imports Leadtools 
    Imports Leadtools.Codecs 
    Imports Leadtools.ImageProcessing 
         

    C#
    using System.IO; 
    using Leadtools; 
    using Leadtools.Codecs; 
    using Leadtools.ImageProcessing; 
         

  8. Add the following initialization code to the Form1 class

    VB
    Protected Overrides Sub OnLoad(ByVal e As EventArgs) 
       ' Requires a license file that unlocks 1D barcode read functionality. 
       Dim MY_LICENSE_FILE As String = "C:\LEADTOOLS 19\Common\License\leadtools.lic" 
       Dim MY_DEVELOPER_KEY As String = System.IO.File.ReadAllText("C:\LEADTOOLS 19\Common\License\leadtools.lic.key") 
       RasterSupport.SetLicense(MY_LICENSE_FILE, MY_DEVELOPER_KEY) 
                     
       MyBase.OnLoad(e) 
    End Sub 
     
    Private Function CreateMultiPageFile(ByVal codecs As RasterCodecs) As String 
       ' Create a multi-page TIF file 
       Dim pageFileName() As String = _ 
       { _ 
          "C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.tif", _ 
          "C:\Users\Public\Documents\LEADTOOLS Images\Ocr2.tif", _ 
          "C:\Users\Public\Documents\LEADTOOLS Images\Ocr3.tif", _ 
          "C:\Users\Public\Documents\LEADTOOLS Images\Ocr4.tif" _ 
       } 
       Dim image As RasterImage = Nothing 
       For i As Integer = 0 To pageFileName.Length - 1 
          ' load the page image 
          Dim page As RasterImage = codecs.Load(pageFileName(i)) 
          If (i = 0) Then 
             ' first image, just save it into our variable 
             image = page 
          Else 
             ' add this page into our image 
             image.AddPage(page) 
             ' we do not need this page anymore 
             page.Dispose() 
          End If 
       Next 
       Dim outputFileName As String = "C:\Users\Public\Documents\LEADTOOLS Images\MultiPage.tif" 
       If (File.Exists(outputFileName)) Then 
          File.Delete(outputFileName) 
       End If 
       codecs.Save(image, outputFileName, RasterImageFormat.Tif, 1, 1, 4, 1, CodecsSavePageMode.Overwrite) 
       image.Dispose() 
       Return outputFileName 
    End Function 
         

    C#
    protected override void OnLoad(EventArgs e) 
    {                
       // Requires a license file that unlocks 1D barcode read functionality. 
       string MY_LICENSE_FILE = @"C:\LEADTOOLS 19\Common\License\leadtools.lic"; 
       string MY_DEVELOPER_KEY = System.IO.File.ReadAllText(@"C:\LEADTOOLS 19\Common\License\leadtools.lic.key"); 
       RasterSupport.SetLicense(MY_LICENSE_FILE, MY_DEVELOPER_KEY); 
     
       buttonSplit.Click += buttonSplit_Click; 
                     
       base.OnLoad(e); 
    } 
     
    private string CreateMultiPageFile(RasterCodecs codecs) 
    { 
       // Create a multi-page TIF file 
       string[] pageFileName = 
       { 
          @"C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.tif", 
          @"C:\Users\Public\Documents\LEADTOOLS Images\Ocr2.tif", 
          @"C:\Users\Public\Documents\LEADTOOLS Images\Ocr3.tif", 
          @"C:\Users\Public\Documents\LEADTOOLS Images\Ocr4.tif" 
       }; 
       RasterImage image = null; 
       for(int i = 0; i < pageFileName.Length; i++) 
       { 
          // load the page image 
          RasterImage page = codecs.Load(pageFileName[i]); 
          if(i == 0) 
          { 
             // first image, just save it into our variable 
             image = page; 
          } 
          else 
          { 
             // add this page into our image 
             image.AddPage(page); 
             // we do not need this page anymore 
             page.Dispose(); 
          } 
       } 
       string outputFileName = @"C:\Users\Public\Documents\LEADTOOLS Images\MultiPage.tif"; 
       if(File.Exists(outputFileName)) 
          File.Delete(outputFileName); 
       codecs.Save(image, outputFileName, RasterImageFormat.Tif, 1, 1, 4, 1, CodecsSavePageMode.Overwrite); 
       image.Dispose(); 
       return outputFileName; 
    } 
         

  9. Add an event handler to the buttonSplit Click event and code it as follows:

    VB
    Private Sub buttonSplit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonSplit.Click 
       ' Intitialize a new RasterCodecs object. 
       Dim codecs As New RasterCodecs() 
       Dim multiPageFileName As String = CreateMultiPageFile(codecs) 
       ' Find out how many pages the file has 
       Dim info As CodecsImageInfo = codecs.GetInformation(multiPageFileName, True) 
       ' Loop through the pages of the file; perform specific processing on it, then save each page to a separate file: 
       Dim flip As New FlipCommand() 
       Dim outputDir As String = "C:\Users\Public\Documents\LEADTOOLS Images" 
       For pageNumber As Integer = 1 To info.TotalPages 
          ' Load the next image in the loop 
          Dim image As RasterImage = codecs.Load(multiPageFileName, 0, CodecsLoadByteOrder.BgrOrGray, pageNumber, pageNumber) 
          ' Perform specific processing on the page, such as flipping it 
          flip.Run(image) 
          ' Save the page to a separate file 
          Dim pageFileName As String = Path.Combine(outputDir, "Page" + pageNumber.ToString() + ".tif") 
          codecs.Save(image, pageFileName, RasterImageFormat.Tif, 0) 
       Next 
    End Sub 
         

    C#
    private void buttonSplit_Click(object sender, System.EventArgs e) 
    { 
       // Intitialize a new RasterCodecs object. 
       RasterCodecs codecs = new RasterCodecs(); 
       string multiPageFileName = CreateMultiPageFile(codecs); 
       // Find out how many pages the file has 
       CodecsImageInfo info = codecs.GetInformation(multiPageFileName, true); 
                        
       // Loop through the pages of the file; perform specific processing on it, then save each page to a separate file: 
       FlipCommand flip = new FlipCommand(); 
       string outputDir = @"C:\Users\Public\Documents\LEADTOOLS Images"; 
       for(int pageNumber = 1; pageNumber <= info.TotalPages; pageNumber++) 
       { 
          // Load the next image in the loop 
          RasterImage image = codecs.Load(multiPageFileName, 0, CodecsLoadByteOrder.BgrOrGray, pageNumber, pageNumber); 
          // Perform specific processing on the page, such as flipping it 
          flip.Run(image); 
          // Save the page to a separate file 
          string pageFileName = Path.Combine(outputDir, "Page" + pageNumber + ".tif"); 
          codecs.Save(image, pageFileName, RasterImageFormat.Tif, 0); 
          image.Dispose(); 
       } 
    } 
         

  10. Build and run the program to test it.

Help Version 19.0.2017.10.27
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
LEADTOOLS Imaging, Medical, and Document