var desiredDpi = 150;
var dtStartAnalysis = DateTime.Now;
using (var rasterizer = new GhostscriptRasterizer())
{
var ms = new MemoryStream(request.DocumentBytes);
rasterizer.Open(ms);
var codec = ImageCodecInfo.GetImageEncoders().Where(ice => ice.MimeType == "image/tiff").ElementAt(0);
var encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionCCITT4);
for (var pageNumber = 1; pageNumber <= rasterizer.PageCount; pageNumber++)
{
var img = rasterizer.GetPage(desiredDpi, desiredDpi, pageNumber);
var newImg = new Bitmap(img.Width, img.Height);
//this section converts the image to grayscale prior to slapping it down
var g = Graphics.FromImage(newImg);
var colorMatrix = new ColorMatrix(
new[]
{
new[] {.3f, .3f, .3f, 0, 0},
new[] {.59f, .59f, .59f, 0, 0},
new[] {.11f, .11f, .11f, 0, 0},
new[] {0f, 0, 0, 1, 0},
new[] {0f, 0, 0, 0, 1}
});
var attributes = new ImageAttributes();
attributes.SetColorMatrix(colorMatrix);
attributes.SetThreshold(0.8f); //threshold for switching gray to black or white
g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, attributes);
g.Dispose();
using (var targetStream = new MemoryStream())
{
newImg.Save(targetStream, codec, encoderParams);
}
}
}
Trace.WriteLine(string.Format("Converted PDF to TIFF images in {0}", (DateTime.Now - dtStartAnalysis).TotalMilliseconds));
Here's how I'm converting pdf to tiff currently. Specifically to tiff, if you're using CCITT4 you want to make sure your pdf image is converted to bitonal, otherwise you will have loss of any image that's lightly drawn on the page.
The ColorMatrix and the SetThreshold() settings work in my specific case but anyone trying this on their own PDF documents should be aware that you may have to mess with those settings (at least with the threshold).
-DM