Convert PDF to multipage TIFF in C# .NET

Convert PDF, Images
3/10/2010

This article shows how to convert PDF to multipage TIFF in C# using PDFRasterizer.NET 3.0.

Code sample to convert a PDF to multipage TIFF using C# or VB.NET

There are basically two ways to achieve this conversion. The simplest way is to just call Document.ConvertToTiff().

C#

1 using (FileStream fileIn = new FileStream( 2 "in.pdf", FileMode.Open, FileAccess.Read)) 3 { 4 Document document = new Document(fileIn); 5 6 ConvertToTiffOptions options = new ConvertToTiffOptions(); 7 options.Compression = TiffCompression.CcittG4; 8 options.Resolution = 150; 9 options.PixelFormat = PixelFormat.Bw1Bpp; 10 11 using (FileStream fileOut = new FileStream( 12 "out.tif", FileMode.Create, FileAccess.Write)) 13 { 14 document.ConvertToTiff(fileOut, options); 15 } 16 }

VB.NET

1 Using fileIn As New FileStream( _ 2 "in.pdf", FileMode.Open, FileAccess.Read) 3 4 Dim pdf As New Document(fileIn) 5 6 Dim options As New ConvertToTiffOptions() 7 options.Compression = TiffCompression.CcittG4 8 options.Resolution = 150 9 options.PixelFormat = PixelFormat.Bw1Bpp 10 11 Using fileOut As New FileStream( _ 12 "out.tif", FileMode.Create, FileAccess.Write) 13 14 pdf.ConvertToTiff(fileOut, options) 15 End Using 16 End Using

The Resolution and Compression settings of the ConvertToTiffOptions class allow you to control the size of the output. Lowering the resolution will lead to smaller files, at the cost of lower quality output. The effect of changing the compression settings depends on the graphical content of the PDF file.

LZW compression may result in smaller file sizes for PDF file that contain more than just black text on a white background. It may not be usable however if the resulting file needs to be processed further by a component that only supports CCITT G3/G4 compression. Many fax machines for example only support CCITT G3.

Page.Draw

Another way to convert PDF to TIFF using C#, is to use Page.Draw() to create a bitmap for each page, and then use standard .Net methods to write these to a tiff file. There are two issue with this though:

  • .Net does not support creating a graphics instance for a monochrome bitmap. So this solution only works for generating color TIFF files. This, in fact, is the main reason why our software offers the ConvertToTiff methods.
  • Writing multipage TIFF files is fairly involved using only standard .NET calls. See the sample code below (C# only). A similar effect can be achieved via ConvertToTiff in a much simpler way by just changing the PixelFormat to Rgba32Bpp.

Using standard .Net calls to generate multipage Tiff (C# only)

1 double scale = dpi / 72; 2 int n = document.Pages.Count; 3 4 if (n > 0) 5 { 6 // Convert the first page. 7 8 Page page = document.Pages[0]; 9 10 using (Bitmap tiff = new Bitmap((int)(scale * page.Width), (int)(scale * page.Height))) 11 { 12 using (Graphics graphics = Graphics.FromImage(tiff)) 13 { 14 graphics.SmoothingMode = SmoothingMode.AntiAlias; 15 graphics.ScaleTransform((float)scale, (float)scale); 16 graphics.Clear(Color.White); 17 page.Draw(graphics); 18 } 19 tiff.SetResolution((float)dpi, (float)dpi); 20 21 EncoderParameters firstImageParameters = new EncoderParameters(); 22 firstImageParameters.Param = new EncoderParameter[2]; 23 firstImageParameters.Param[1] = new EncoderParameter( 24 Encoder.SaveFlag, (long)EncoderValue.MultiFrame); 25 26 EncoderParameters nextImageParameters = new EncoderParameters(); 27 nextImageParameters.Param = new EncoderParameter[2]; 28 nextImageParameters.Param[1] = new EncoderParameter( 29 Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage); 30 31 EncoderParameters flushImageParameters = new EncoderParameters(); 32 flushImageParameters.Param = new EncoderParameter[1]; 33 flushImageParameters.Param[0] = new EncoderParameter( 34 Encoder.SaveFlag, (long)EncoderValue.Flush); 35 36 firstImageParameters.Param[0] = new EncoderParameter( 37 Encoder.Compression, (long)EncoderValue.CompressionLZW); 38 nextImageParameters.Param[0] = new EncoderParameter( 39 Encoder.Compression, (long)EncoderValue.CompressionLZW); 40 41 ImageCodecInfo tiffCodec = null; 42 foreach (System.Drawing.Imaging.ImageCodecInfo codec in 43 System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()) 44 { 45 if (codec.FormatDescription.Equals("TIFF")) 46 { 47 tiffCodec = codec; 48 } 49 } 50 if (tiffCodec == null) 51 { 52 throw new ApplicationException("could not find tiff codec"); 53 } 54 tiff.Save(file, tiffCodec, firstImageParameters); 55 56 // Convert the remaining pages. 57 58 for (int i = 1; i < n; i++) 59 { 60 page = document.Pages[i]; 61 62 using (Bitmap nextBitmap = new Bitmap((int)(scale * page.Width), (int)(scale * page.Height))) 63 { 64 using (Graphics graphics = Graphics.FromImage(nextBitmap)) 65 { 66 graphics.SmoothingMode = SmoothingMode.AntiAlias; 67 graphics.ScaleTransform((float)scale, (float)scale); 68 graphics.Clear(Color.White); 69 page.Draw(graphics); 70 } 71 72 nextBitmap.SetResolution((float)dpi, (float)dpi); 73 74 tiff.SaveAdd(nextBitmap, nextImageParameters); 75 } 76 } 77 78 tiff.SaveAdd(flushImageParameters); 79 } 80 }