Convert PDF to an image using a dither matrix

Convert PDF, Images
8/24/2015

In this codesample we will show how you can convert a PDF page to a bitmap image, while applying a dither matrix to the generated image. Dithering is the processing of applying a matrix to the original image and thereby adding seemingly randomized noise. This is done to prevent color banding and other large scale patterns to occur in your image. More information on dithering can be found on wikipedia.

C# code sample

1 using (FileStream fs = new FileStream(@"..\..\input.pdf", FileMode.Open, FileAccess.Read)) 2 { 3 Document d = new Document(fs); 4 5 int[][] DitherMatrix = new int[][] { 6 new int[] { 9, 9, 7 }, 7 new int[] { 4, 1, 0 }, 8 new int[] { 0, 5, 2 }}; 9 Page page = d.Pages[0]; 10 11 Bitmap bitmap = new Bitmap((int)page.Width, (int)page.Height); 12 13 ConvertToTiffOptions options = new ConvertToTiffOptions(); 14 15 //set a dithering matrix 16 options.DitherMatrix = DitherMatrix; 17 18 using (MemoryStream ms = new MemoryStream()) 19 { 20 using (Graphics graphics = Graphics.FromImage(bitmap)) 21 { 22 graphics.SmoothingMode = SmoothingMode.AntiAlias; 23 graphics.Clear(Color.White); 24 page.ConvertToTiff(ms, options); 25 } 26 bitmap = new Bitmap(ms, false); 27 bitmap.Save(@"..\..\output.bmp"); 28 } 29 }