Search Persits Software Knowledge Base

 

Article PS150809184

HOWTO: Convert a mutli-page PDF document to multi-image TIFF with AspPDF.NET

Problem Description

The following article demonstrates how to convert a multi-page PDF document to a multi-page TIFF image using AspPDF.NET and the Microsoft .NET framework.

Solution

AspPDF.NET's PDF-to-Image functionality enables the individual pages of a PDF document to be converted to images in PNG, JPEG and TIFF formats. Multi-page TIFF images are not supported by AspPDF.NET but can be easily created in the Microsoft .NET environment. The following C# code sample demonstrates how to use AspPDF.NET in conjunction with .NET objects to convert a PDF to a multipage TIFF.

using System.Drawing.Imaging;
using Persits.PDF;
...

PdfManager objPDF = new PdfManager();
PdfDocument objDoc = objPDF.OpenDocument(@"c:\path\test.pdf");

// Obtain TIFF encoder info object
ImageCodecInfo encoderInfo = null;
ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
for (int i = 0; i < encoders.Length; ++i)
{
   if (encoders[i].MimeType.ToLower() == "image/tiff")
   {
      encoderInfo = encoders[i];
      break;
   }
}

// Create tiff image array, one image per document page.
Image [] objTiff = new Image[objDoc.Pages.Count];

EncoderParameters EncoderParams = new EncoderParameters(1);
System.Drawing.Imaging.Encoder SaveFlag = System.Drawing.Imaging.Encoder.SaveFlag;

foreach (PdfPage objPage in objDoc.Pages)
{
   // Convert PDF page to TIFF. Resolution parameters are 72dpi by default.
   PdfPreview objPreview = objPage.ToImage("ResolutionX=300; ResolutionY=300");
   byte[] ImageBytes = objPreview.SaveToMemory(ImageFormat.Tiff);
   MemoryStream objStream = new MemoryStream(ImageBytes);
   objTiff[objPage.Index - 1] = Image.FromStream(objStream);

   EncoderParams.Param[0] = new EncoderParameter(SaveFlag, (long)EncoderValue.MultiFrame);

   if (objPage.Index == 1)
   {
      // Save first page as a TIFF
      objTiff[0].Save(@"c:\path\out.tif", encoderInfo, EncoderParams);
   }
   else
   {
      // Append additional pages
      EncoderParams.Param[0] = new EncoderParameter(SaveFlag, (long)EncoderValue.FrameDimensionPage);
      objTiff[0].SaveAdd(objTiff[objPage.Index - 1], EncoderParams);
   }
}

// Finish appending
EncoderParams.Param[0] = new EncoderParameter(SaveFlag, (long)EncoderValue.Flush);
objTiff[0].SaveAdd(EncoderParams);

Created: 8/9/2015 1:18:33 PM
Last Modified: 8/9/2015 1:18:33 PM

Copyright © Persits Software, Inc. 1998 - 2017
For technical support, write to support@persits.com.