PDFOne .NET
Powerful all-in-one PDF library for .NET
Compatibility
VS 2005/2008/2010/2012/2013

Convert PDF To Multipage TIFF Images Using PDFOne .NET

Learn to export each page in a PDF document as a frame in a multi-page TIFF image using C# and PDFOne .NET.
By V. Subhash
Important

In Version 5 of PDFOne .NET, a new SaveAsMultiPageTiff() method was introduced, which internally use this code. You do not have to go through the metafile-creation process.

In a previous article, we saw how to convert a multi-page TIFF image to a PDF document. In this article, we will see how to perform the opposite - convert a PDF document to a multipage TIFF image.

GDI provides built-in support for TIFF creation and manipulation in Windows. I found the code to create or parse TIFF image not very straighword in good old C++. It does not seem to have evolved much in .NET either. For this reason, I have created a separate class named TIFF_Utility for TIFF manipulation. This makes my "PDF-to-TIFF" conversion code a bit more modular.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gnostice.PDFOne;
using System.Drawing.Imaging;


namespace PDFOne_dotNET_Examples {   

   class PDF_To_Multipage_TIFF {

      static void Main(string[] args) {   

         // Create a PDF document object
         PDFDocument doc = new PDFDocument("your-license-key");

         // Load an existing PDF document
         doc.Load("sample_doc.pdf");
            
         // Export each page in the PDF to a bitmap image
         // and save the location of the images in an array
         int i, n = doc.GetPageCount();
         String[] exported_images = new String[n];
         Metafile temp_metafile;            
         for (i = 1; i <= n; i++) {
            temp_metafile = doc.GetPageMetafile(i);
            exported_images[i-1] = "page" + i.ToString() + ".bmp";
            temp_metafile.Save(exported_images[i-1], ImageFormat.Bmp); 
         }

         // Close the document
         doc.Close();

         // Use the TIFF utility to convert the bitmap images to TIFF
         TIFF_Utility tu = new TIFF_Utility();
         tu.CreateMultiPageTiff(exported_images, "sample_doc.tiff");

      }
   }
}

I use the PDFDocument.GetPageMetafile() method to export each page to a bitmap image file. I also store the pathname of the image in a string array. I then create an instance of my TIFF_Utility class and perform the conversion. Here is the source for TIFF_Utility class. I had had added this class as a reference to my original project.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

class TIFF_Utility {
   private static ImageCodecInfo TIFF_ImageCodecInfo = null;
   private static EncoderParameter FirstFrame_EncoderParameter = null;
   private static EncoderParameter MiddleFrame_EncoderParameter = null;
   private static EncoderParameter LastFrame_EncoderParameter = null;

   public TIFF_Utility() {
      foreach (ImageCodecInfo ImageCodecInfo1 in ImageCodecInfo.GetImageEncoders()) {
         if (ImageCodecInfo1.MimeType == "image/tiff") {
            TIFF_ImageCodecInfo = ImageCodecInfo1;
         }
      }

      FirstFrame_EncoderParameter =
            new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag,
                                 (long)System.Drawing.Imaging.EncoderValue.MultiFrame);
      MiddleFrame_EncoderParameter =
            new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag,
                                 (long)System.Drawing.Imaging.EncoderValue.FrameDimensionPage);
      LastFrame_EncoderParameter =
            new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag,
                                 (long)System.Drawing.Imaging.EncoderValue.Flush);

   }

   //  Accepts a set of images and converts them to multipage TIFF file
   public void CreateMultiPageTiff(String[] input_images, String output_image) {      
      EncoderParameters EncoderParameters1 = new EncoderParameters(1);
      EncoderParameters1.Param[0] = FirstFrame_EncoderParameter;

      Bitmap tiff_image = new Bitmap(input_images[0]);
      tiff_image.Save(output_image, TIFF_ImageCodecInfo, EncoderParameters1);

      int i, n = input_images.Length;
      EncoderParameters1.Param[0] = MiddleFrame_EncoderParameter;
      for (i = 1; i < n; i++) {
         Bitmap temp_image = new Bitmap(input_images[i]);         
         tiff_image.SaveAdd(temp_image, EncoderParameters1);
      }

      EncoderParameters1.Param[0] = LastFrame_EncoderParameter;
      tiff_image.SaveAdd(EncoderParameters1);
   } 
    
}

UPDATE (15-May-2013): Please note that TIFF supports several container formats and the class assumes the chosen default supported by GDI is the best for all occasions. This is not correct. Also, the System.Drawing.Imaging namespace is not recommended for ASP.NET and Windows Services. Microsoft has made this disclaimer:

Classes within the System.Drawing.Imaging namespace are not supported for use within a Windows or ASP.NET service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions.

This means that the TIFF_Utility or using the System.Drawing namespace is not good for Web applications. My assumption is that huge memory maps that are required to process some TIFF and other image files are likely to burst the load profiles for ASP.NET and Windows services. If you are determined to go through this route, then the solution may be a bit awkward. The document conversion should be deferred by the Web application to a Windows service which watches a folder for PDF documents and queues the found files for conversion. For its own longevity, the Windows service should not perform the conversion. Instead, it should spawn an old-world console EXE PDF conversion application written using PDFOne .NET. The console EXE application hopefully will not be affected by any performance limitations. If you have any comments regarding this assumption or if you have a better solution, please send an e-mail to support at gnostice dot com.

---o0O0o---

Our .NET Developer Tools
Gnostice Document Studio .NET

Multi-format document-processing component suite for .NET developers.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
Gnostice Document Studio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
Gnostice Document Studio Java

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2024 Gnostice Information Technologies Private Limited. All rights reserved.