Split tiff image and Convert to PDF document

Spire.PDF for .NET is a PDF component which contains an incredible wealth of features to create, read, edit and manipulate PDF documents on .NET, Silverlight and WPF Platform. As a professional .NET PDF component, it also includes many useful features, for example, functionalities of adding header and footer, drawing table, saving PDF document as tiff and Splitting tiff image and drawing to pdf document without installing Adobe Acrobat or any other third party libraries.

This article would introduce a detail method to split the tiff image and draw to pdf document. The below picture show the effect of splitting tiff image and drawing to pdf document:

Split tiff image and Convert to pdf document

The main steps of the method are:

One: Split multi-page TIFF file to multiple frames

  • Load a multipage Tiff.
  • Use Image.GetFrameCount method to get the number of frames of tiff Image.
  • Initialize a new instance of Guid to save the dimension from tiff image.
  • Using Guid to initialize a new instance of the System.Drawing.Imaging.FrameDimension class.
  • Iterate over the Tiff Frame Collection and save them to Image array.

Two: Draw image to PDF

  • Initialize a new instance of Spire.Pdf.PdfDocument.
  • Iterate over the image array.
  • Use Spire.Pdf.PdfPageBase.Canvas.DrawImage method to draw image into page with the specified coordinates and image size.

Download and install Spire.Pdf for .NET and use below code to experience this method to split tiff and convert it to pdf document.

The full code of method:

[C#]
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Spire.Pdf;
using Spire.Pdf.Graphics;
namespace SplitTiff
{
    class Program
    {
        static void Main(string[] args)
        {
            using (PdfDocument pdfDocument = new PdfDocument())
            {
                Image tiffImage = Image.FromFile(@"..\..\demo.tiff");
                Image[] images = SplitTIFFImage(tiffImage);
                for (int i = 0; i < images.Length; i++)
                {
                    PdfImage pdfImg = PdfImage.FromImage(images[i]);
                    PdfPageBase page = pdfDocument.Pages.Add();
                    float width = pdfImg.Width * 0.5f;
                    float height = pdfImg.Height * 0.5f;
                    float x = (page.Canvas.ClientSize.Width - width) / 2;
                    //set the image of the page 
                    page.Canvas.DrawImage(pdfImg, x, 0, width, height);
                }
                pdfDocument.SaveToFile(@"..\..\result.pdf");
                System.Diagnostics.Process.Start(@"..\..\result.pdf");
            }
        }
        public static Image[] SplitTIFFImage(Image tiffImage)
        {
            int frameCount = tiffImage.GetFrameCount(FrameDimension.Page);
            Image[] images = new Image[frameCount];
            Guid objGuid = tiffImage.FrameDimensionsList[0];
            FrameDimension objDimension = new FrameDimension(objGuid);
            for (int i = 0; i < frameCount; i++)
            {
                tiffImage.SelectActiveFrame(objDimension, i);
                using (MemoryStream ms = new MemoryStream())
                {
                    tiffImage.Save(ms, ImageFormat.Tiff);
                    images[i] = Image.FromStream(ms);
                }
            }
            return images;
        }
    }
}
[VB.NET]
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO
Imports Spire.Pdf
Imports Spire.Pdf.Graphics

Namespace SplitTiff
    Class Program
        Shared Sub Main(ByVal args() As String)
            Using pdfDocument As New PdfDocument

                Dim tiffImage As Image = Image.FromFile("..\..\demo.tiff")
                Dim images() As Image = SplitTIFFImage(tiffImage)

                Dim i As Integer
                For i = 0 To images.Length - 1 Step i + 1
                    Dim pdfImg As PdfImage = PdfImage.FromImage(images(i))
                    Dim page As PdfPageBase = pdfDocument.Pages.Add()
                    Dim width As Single = pdfImg.Width * 0.5F
                    Dim height As Single = pdfImg.Height * 0.5F
                    Dim x As Single = (page.Canvas.ClientSize.Width - width) / 2
                    'set the image of the page 
                    page.Canvas.DrawImage(pdfImg, x, 0, width, height)
                Next


                pdfDocument.SaveToFile("..\..\result.pdf")
                System.Diagnostics.Process.Start("..\..\result.pdf")
            End Using
        End Sub
        Private Shared Function SplitTIFFImage(ByVal tiffImage As Image)

            Dim frameCount As Integer = tiffImage.GetFrameCount(FrameDimension.Page)
            Dim images() As Image = New Image(frameCount) {}
            Dim objGuid As Guid = tiffImage.FrameDimensionsList(0)
            Dim objDimension As FrameDimension = New FrameDimension(objGuid)
            Dim i As Integer
            For i = 0 To frameCount - 1 Step i + 1
                tiffImage.SelectActiveFrame(objDimension, i) 

                Using ms As New MemoryStream

                    tiffImage.Save(ms, ImageFormat.Tiff)
                    images(i) = Image.FromStream(ms)
                End Using

            Next
            Return images
        End Function
    End Class
End Namespace

If you are interested in the method of converting pdf to tiff image you can refer the Save PDF Document as tiff image article in our website.