Save PDF Document as tiff image

Tiff image as a graphics container that can store both raster and vector images; may contain high-quality graphics that support color depths from 1 to 24-bit; supports both lossy and lossless compression; also supports multiple layers and pages. If you want your documents can convert to the high-quality graphics and don’t lose image file information when they are saved during the compression process, the tiff image is your best choice.

This article introduces a detail method to save PDF document as tiff image via document.SaveAsImage() and JoinTiffImages() methods by using Spire.PDF for .NET which 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. The below screenshot presents result after saving PDF document as tiff image:

Save PDF Document as tiff image

The main steps of the method are:

Step 1: Create a new pdf-document and load it.

PdfDocument document = new PdfDocument();
document.LoadFromFile(@"sample.pdf");

Step 2: Using document.SaveAsImage() method to save the pdf document as image array.

  private static Image[] SaveAsImage(PdfDocument document)
          {
            Image[] images = new Image[document.Pages.Count];
            for (int i = 0; i < document.Pages.Count; i++)
              {
//use the document.SaveAsImage() method save the pdf as image
                images[i] = document.SaveAsImage(i);
           }
            return images;
          }

Step 3: Using JoinTiffImages() method to save the images from pdf pages to tiff image type, with the specified encoder and image-encoder parameters.

   public static void JoinTiffImages(Image[] images, string outFile, EncoderValue compressEncoder)
        {
            //use the save encoder
            Encoder enc = Encoder.SaveFlag;
            EncoderParameters ep = new EncoderParameters(2);
            ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
            ep.Param[1] = new EncoderParameter(Encoder.Compression, (long)compressEncoder);
            Image pages = images[0];
            int frame = 0;
            ImageCodecInfo info = GetEncoderInfo("image/tiff");
            foreach (Image img in images)
            {
                if (frame == 0)
                {
                    pages = img;
                    //save the first frame
                    pages.Save(outFile, info, ep);
                }
                else
                {
                    //save the intermediate frames
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
                    pages.SaveAdd(img, ep);
               }
                if (frame == images.Length - 1)
                {
                    //flush and close.
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
                    pages.SaveAdd(ep);
                }
                frame++;
            }
        }

Download and install Spire.Pdf for .NET and use below code to experience this method to save pdf document as tiff image.

The full code:

[C#]
using System;
using System.Drawing;
using System.Drawing.Imaging;
using Spire.Pdf;
namespace SavePdfAsTiff
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument document = new PdfDocument();
            document.LoadFromFile(@"01.pdf");
    JoinTiffImages(SaveAsImage(document),"result.tiff",EncoderValue.CompressionLZW);
            System.Diagnostics.Process.Start("result.tiff");
        }
        private static Image[] SaveAsImage(PdfDocument document)
        {
            Image[] images = new Image[document.Pages.Count];
            for (int i = 0; i < document.Pages.Count; i++)
            {
                images[i] = document.SaveAsImage(i);
            }
            return images;
        }

        private static ImageCodecInfo GetEncoderInfo(string mimeType)
        {
            ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
            for (int j = 0; j < encoders.Length; j++)
            {
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            throw new Exception(mimeType + " mime type not found in ImageCodecInfo");
        }

        public static void JoinTiffImages(Image[] images, string outFile, EncoderValue compressEncoder)
        {
            //use the save encoder
            Encoder enc = Encoder.SaveFlag;
            EncoderParameters ep = new EncoderParameters(2);
            ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
            ep.Param[1] = new EncoderParameter(Encoder.Compression, (long)compressEncoder);
            Image pages = images[0];
            int frame = 0;
            ImageCodecInfo info = GetEncoderInfo("image/tiff");
            foreach (Image img in images)
            {
                if (frame == 0)
                {
                    pages = img;
                    //save the first frame
                    pages.Save(outFile, info, ep);
                }

                else
                {
                    //save the intermediate frames
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);

                    pages.SaveAdd(img, ep);
                }
                if (frame == images.Length - 1)
                {
                    //flush and close.
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
                    pages.SaveAdd(ep);
                }
                frame++;
            }
        }
    }
}
[VB.NET]
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports Spire.Pdf
Namespace SavePdfAsTiff
    Class Program
        Sub Main()
            Dim document As PdfDocument = New PdfDocument()
            document.LoadFromFile("01.pdf")
            JoinTiffImages(SaveAsImage(document), "result.tiff", EncoderValue.CompressionLZW)
            System.Diagnostics.Process.Start("result.tiff")
        End Sub
        Private Shared Function SaveAsImage(ByVal document As PdfDocument)
            Dim images() As Image = New Image(document.Pages.Count-1) {}
            Dim i As Integer
            For i = 0 To document.Pages.Count - 1 Step i + 1
                images(i) = document.SaveAsImage(i)
            Next
            Return images
        End Function
        Private Shared Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo
            Dim encoders() As ImageCodecInfo = ImageCodecInfo.GetImageEncoders()
            Dim j As Integer
            For j = 0 To encoders.Length - 1 Step j + 1
                If encoders(j).MimeType = mimeType Then
                    Return encoders(j)
                End If
            Next
            Throw New Exception(mimeType + " mime type not found in ImageCodecInfo")
        End Function
        Public Shared Sub JoinTiffImages(ByVal images() As Image, ByVal outFile As String, ByVal compressEncoder As EncoderValue)
            'use the save encoder
            Dim enc As Encoder = Encoder.SaveFlag
            Dim ep As EncoderParameters = New EncoderParameters(2)
            ep.Param(0) = New EncoderParameter(enc, CType(EncoderValue.MultiFrame, Long))
            ep.Param(1) = New EncoderParameter(Encoder.Compression, CType(compressEncoder, Long))
            Dim pages As Image = images(0)
            Dim frame As Integer = 0
            Dim info As ImageCodecInfo = GetEncoderInfo("image/tiff")
            Dim img As Image
            For Each img In images
                If frame = 0 Then
                    pages = img
                    'save the first frame
                    pages.Save(outFile, info, ep)
                Else
                    'save the intermediate frames
                    ep.Param(0) = New EncoderParameter(enc, CType(EncoderValue.FrameDimensionPage, Long))
                    pages.SaveAdd(img, ep)
                End If

                If frame = images.Length - 1 Then
                    'flush and close.
                    ep.Param(0) = New EncoderParameter(enc, CType(EncoderValue.Flush, Long))
                    pages.SaveAdd(ep)
                End If
                frame = frame + 1
            Next
        End Sub
    End Class
End Namespace