Send comments on this topic. | Back to Introduction - All Topics | Help Version 15.12.21
Splitting a Multi-page TIFF File Tutorial

Take the following steps to start a project:

  1. Start Visual Studio .NET.
  2. Choose File->New->Project… from the menu.
  3. In the New Project dialog box, Choose either "Visual C# Projects" or "Visual Basic Projects" in the Projects Type List, and choose "Windows Application " in the Templates List.
  4. Type the project name as "Splitting Multi-page TIFF file" in the Project Name field, and then choose OK. If desired, type a new location for your project or select a directory using the Browse button, and then choose OK .
  5. In the "Solution Explorer" window, right-click on the "References" folder, and select "Add Reference…" from the context menu. In the "Add Reference" dialog box, select the ".NET" tab and browse to the LEADTOOLS For .NET "\LEADTOOLS 15\Bin\DotNet\Win32 " folder and select the following DLLs:
    • Leadtools.dll
    • Leadtools.Codecs.dll
  6. Click the Select button and then press the OK button to add the above DLLs to the application.
  7. Go to the toolbox (View->Toolbox) and Drag and drop a Button control to the top of the form and set the following properties for it:
    TextName
    Split TIFFbuttonSplit
  8. Switch to Form1 code view (right-click Form1 in the solution explorer then select View Code ) and add the following lines at the beginning of the file: [Visual Basic]
    
    Imports System.IO
    Imports Leadtools
    Imports Leadtools.Codecs
    Imports Leadtools.ImageProcessing
    
    [C#]
    
    using System.IO;
    using Leadtools;
    using Leadtools.Codecs;
    using Leadtools.ImageProcessing;
    
  9. Add an event handler to the buttonSplit Click event and code it as follows:

    [Visual Basic]

    
    Private Sub buttonSplit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonSplit.Click
       RasterCodecs.Startup()
    
       ' 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:\Program Files\LEAD Technologies\LEADTOOLS 15\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
    
       RasterCodecs.Shutdown()
    End Sub
    
    Function CreateMultiPageFile(ByVal codecs As RasterCodecs) As String
       ' Create a multi-page TIF file
       Dim pageFileName() As String = _
       { _
          "C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Ocr1.tif", _
          "C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Ocr2.tif", _
          "C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Ocr3.tif", _
          "C:\Program Files\LEAD Technologies\LEADTOOLS 15\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:\Program Files\LEAD Technologies\LEADTOOLS 15\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#]
    
    private void buttonSplit_Click(object sender, System.EventArgs e)
    {
       RasterCodecs.Startup();
    
       // 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:\Program Files\LEAD Technologies\LEADTOOLS 15\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();
       }
    
       RasterCodecs.Shutdown();
    }
    
    string CreateMultiPageFile(RasterCodecs codecs)
    {
       // Create a multi-page TIF file
       string[] pageFileName =
       {
          @"C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Ocr1.tif",
          @"C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Ocr2.tif",
          @"C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Ocr3.tif",
          @"C:\Program Files\LEAD Technologies\LEADTOOLS 15\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:\Program Files\LEAD Technologies\LEADTOOLS 15\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;
    }
    
  10. Build, and Run the program to test it.