PDF to grayscale TIFF


In the .NET framework the support for grayscale bitmaps is a bit puzzling. If you use bitmaps with format PixelFormat.Format16bppGrayScale you may get memory exceptions, which in fact means that this format is not supported. However, there is a way to convert PDF to grayscale Tiff in C# using PDFRasterizer.NET.

.NET: System.Drawing.Imaging.PixelFormats

In System.Drawing.Imaging, the PixelFormat.Format16bppGrayScale is defined in the .NET framework but it is not supported. Second, the is not even a definition for 8 bits per pixel grayscale bitmaps. So at first it looks impossible to create grayscale tiff file with 8, 16 or any other number of bits per pixel.

You may think that the color matrix may offer a usable solution to this (see this link), but in this context this is not the case. It only converts the colored pixels into gray, but it does not discard the Red, Green And Blues color channels. So you end up with a gray image with the size of a color image.
In a interesting blog written by Lucian Wischik (link) demonstrates how use grayscale images. For this purpose grayscale functionality is used that is present in GDI but is absent from the standard .NET framework. This works fine but you end up with a lot of code.

.NET: System.Windows.Media.PixelFormats

The PixelFormats class (note the 's' on the end) however does offer a lot of grayscale options, like Gray2 (2 bits per pixel), Gray 4, Gray8 and Gray16. This option can be used in the FormatConvertedBitmap class, which cooperates nicely with the TiffBitmapEncoder or the JpegBitmapEncoder class. We can use these to convert a color-bitmap that is generated by PDFRasterizer into a grayscale bitmap and write this e.g. to a tiff file.

Code sample to convert PDF to grayscale TIFF in C# .NET

The following code sample that converts a PDF document to a multipage grayscale demonstrates this:

1 using ( FileStream file = new FileStream(pathToPdfDocument, FileMode.Open, FileAccess.Read)) 2 { 3 document = new Document( file ); 4 } 5 6 // 7 // convert PDF via RGB bitmaps to grayscale tiff using the TiffBitmapEncoder 8 // 9 const float dpi = 300.00f; 10 const float scale = dpi/72; 11 var grayTiffStream = new FileStream("../../out_gray16bpp.tiff", FileMode.Create); 12 var tiffEncoder = new TiffBitmapEncoder(); 13 tiffEncoder.Compression = TiffCompressOption.Zip; 14 15 var nPages = document.Pages.Count; 16 for (var i = 0; i < nPages; i++) 17 { 18 var pdfPage = document.Pages[i]; 19 var rgbBitmap = new Bitmap((int) (pdfPage.Width/72.0*dpi), (int) (pdfPage.Height/72.0*dpi)); 20 var rgbGraphics = Graphics.FromImage(rgbBitmap); 21 rgbGraphics.ScaleTransform(scale, scale); 22 pdfPage.Draw(rgbGraphics); 23 var rgbBitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( 24 rgbBitmap.GetHbitmap(), 25 IntPtr.Zero, 26 Int32Rect.Empty, 27 BitmapSizeOptions.FromEmptyOptions()); 28 var grayBitmap = new FormatConvertedBitmap(); 29 grayBitmap.BeginInit(); 30 grayBitmap.Source = rgbBitmapSource; 31 grayBitmap.DestinationFormat = PixelFormats.Gray16; 32 grayBitmap.EndInit(); 33 tiffEncoder.Frames.Add(BitmapFrame.Create(grayBitmap)); 34 } 35 tiffEncoder.Save(grayTiffStream);