Multipage Files (TIFF or PDF) to Single Image Files (PNG)

From time to time I hear requests from customers with multi-page TIFF or PDF files to display those images natively on the web. Unfortunately, neither TIFF nor PDF are directly supported in browsers. The most straightforward way to display these files is as a sequence of PNG (Portable Network Graphics) files, and with PixTools for .NET it's just a few lines of code.


Almost half of the program below is dealing with the special case of having to render PDF files!



using System;
using System.Collections.Generic;
using System.Text;
using PixTools;
using PixTools.PixCore;
using PixTools.PixImaging;

namespace ConvertMultipage2PNG
{
    class Program
    {
        static void Main(string[] args)
        {
            int ix = 0x00;

            /**********************************************************************
             * The following code is needed only for reading / rendering non-image PDF files
             **********************************************************************/
            PDFRenderingSettings pdfSettings;

            pdfSettings = PixTools.PixScan.ReadFileDriver.PDFRendering;
            pdfSettings.EnableRendering = true;
            pdfSettings.DetectPageResolution = false;
            pdfSettings.Resolution = 100; // DPI 
            pdfSettings.Smoothing = PixPDFSmoothing.Normal;
            PixTools.PixScan.ReadFileDriver.PDFRendering = pdfSettings;
            /**********************************************************************
             * The preceeding code is needed only for reading / rendering PDF files
             **********************************************************************/

            PixImageStorage nDoc = PixImageStorage.Open("c:\\tmp\\xdocintro.pdf");

            foreach(PixImage nImg in nDoc.Images)
            {
                PixImageStorage.Save(nImg,
                    PixTools.PixFileType.Png,
                    "c:\\tmp\\out_"+(++ix)+".png", 
                    PixCompressionSettings.None,    // this is correct for PNG format, gives ZIP compression
                    PixTools.OpenFileMode.CreateAlways);    // Overwrite any previous file
            }
            nDoc.CloseFile();
            nDoc.Dispose();
        }
    }
}