|
| 1 | +--- |
| 2 | +title: Converting Colored PDF Documents to GrayScale with Telerik Document Processing |
| 3 | +description: Learn how to use Telerik Document Processing libraries to convert color PDF reports into black and white format. |
| 4 | +type: how-to |
| 5 | +page_title: How to Convert Color PDF Reports to Black & White Using Telerik Document Processing |
| 6 | +slug: convert-color-pdf-to-black-and-white-telerik-document-processing |
| 7 | +tags: document, processing, pdf, conversion, black, white, gray, image, convert |
| 8 | +res_type: kb |
| 9 | +ticketid: 1675661 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +| Version | Product | Author | |
| 15 | +| ---- | ---- | ---- | |
| 16 | +| 2024.4.1106| RadPdfProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +This article shows a sample approach how to convert a **colored** PDF document to a **grayscale** one with [RadPdfProcessing]({%slug radpdfprocessing-overview%}). |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +## Solution |
| 25 | + |
| 26 | +To convert a colored PDF file to black and white using Telerik Document Processing, follow the steps below: |
| 27 | + |
| 28 | +1. Use the [RadPdfProcessing]({%slug radpdfprocessing-overview%}) library to import the PDF file. |
| 29 | +2. Iterate through the content of the PDF file, including [Path]({%slug radpdfprocessing-model-path%}), [TextFragment]({%slug radpdfprocessing-model-textfragment%}), and [Image]({%slug radpdfprocessing-model-image%}) instances. |
| 30 | +3. Modify the colors to grayscale and export the processed file as a new PDF document. |
| 31 | + |
| 32 | +Here is a complete code snippet that demonstrates how to achieve this conversion: |
| 33 | + |
| 34 | +```csharp |
| 35 | + static void Main(string[] args) |
| 36 | + { |
| 37 | + PdfFormatProvider provider = new PdfFormatProvider(); |
| 38 | + RadFixedDocument document = provider.Import(System.IO.File.ReadAllBytes(filePath)); |
| 39 | + |
| 40 | + foreach (RadFixedPage page in document.Pages) |
| 41 | + { |
| 42 | + foreach (ContentElementBase element in page.Content) |
| 43 | + { |
| 44 | + MakeGrayscale(element); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + string resultFile = "grayscale.pdf"; |
| 49 | + |
| 50 | + if (System.IO.File.Exists(resultFile)) |
| 51 | + { |
| 52 | + System.IO.File.Delete(resultFile); |
| 53 | + } |
| 54 | + |
| 55 | + System.IO.File.WriteAllBytes(resultFile, provider.Export(document, TimeSpan.FromSeconds(10))); |
| 56 | + Process.Start(resultFile); |
| 57 | + } |
| 58 | + private static void MakeGrayscale(ContentElementBase element) |
| 59 | + { |
| 60 | + TextFragment text = element as TextFragment; |
| 61 | + |
| 62 | + if (text != null) |
| 63 | + { |
| 64 | + text.Stroke = MakeGrayscale(text.Stroke); |
| 65 | + text.Fill = MakeGrayscale(text.Fill); |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + if (element is Path path) |
| 70 | + { |
| 71 | + path.Stroke = MakeGrayscale(path.Stroke); |
| 72 | + path.Fill = MakeGrayscale(path.Fill); |
| 73 | + } |
| 74 | + |
| 75 | + Image image = element as Image; |
| 76 | + |
| 77 | + if (image != null) |
| 78 | + { |
| 79 | + BitmapSource originalImage = image.ImageSource.GetBitmapSource(); |
| 80 | + BitmapSource grayscaleImage = MakeGrayscale(originalImage); |
| 81 | + image.ImageSource = new Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource(grayscaleImage); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private static BitmapSource MakeGrayscale(BitmapSource source) |
| 86 | + { |
| 87 | + byte?[,] intensities = GetPixelsIntensity(source); |
| 88 | + BitmapSource grayscaleSource = CreateImageFromGrayPixels(intensities); |
| 89 | + |
| 90 | + return grayscaleSource; |
| 91 | + } |
| 92 | + |
| 93 | + private static ColorBase MakeGrayscale(ColorBase color) |
| 94 | + { |
| 95 | + RgbColor rgb = (RgbColor)color; |
| 96 | + |
| 97 | + byte gray = GetGrayIntensity(rgb.R, rgb.G, rgb.G); |
| 98 | + |
| 99 | + return new RgbColor(rgb.A, gray, gray, gray); |
| 100 | + } |
| 101 | + |
| 102 | + private static BitmapSource CreateImageFromGrayPixels(byte?[,] pixels) |
| 103 | + { |
| 104 | + double dpi = 96; |
| 105 | + int height = pixels.GetLength(0); |
| 106 | + int width = pixels.GetLength(1); |
| 107 | + byte[] pixelData = new byte[width * height]; |
| 108 | + |
| 109 | + for (int y = 0; y < height; ++y) |
| 110 | + { |
| 111 | + int yIndex = y * width; |
| 112 | + for (int x = 0; x < width; ++x) |
| 113 | + { |
| 114 | + pixelData[x + yIndex] = pixels[y, x] ?? 255; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + BitmapSource bmpSource = BitmapSource.Create(width, height, dpi, dpi, PixelFormats.Gray8, null, pixelData, width); |
| 119 | + |
| 120 | + return bmpSource; |
| 121 | + } |
| 122 | + |
| 123 | + private static byte?[,] GetPixelsIntensity(BitmapSource bitmapSource) |
| 124 | + { |
| 125 | + int width = bitmapSource.PixelWidth; |
| 126 | + int height = bitmapSource.PixelHeight; |
| 127 | + byte?[,] intensities = new byte?[height, width]; |
| 128 | + int[] pixels = GetPixels(bitmapSource); |
| 129 | + int pixelIndex = 0; |
| 130 | + |
| 131 | + for (int i = 0; i < height; i++) |
| 132 | + { |
| 133 | + for (int j = 0; j < width; j++) |
| 134 | + { |
| 135 | + byte a, r, g, b; |
| 136 | + GetComponentsFromPixel(pixels[pixelIndex++], out a, out r, out g, out b); |
| 137 | + byte? intensity; |
| 138 | + if (a == 0) |
| 139 | + { |
| 140 | + intensity = null; |
| 141 | + } |
| 142 | + else |
| 143 | + { |
| 144 | + intensity = (byte)((a / 255.0) * GetGrayIntensity(r, g, b)); |
| 145 | + intensity = intensity.Value < 255 ? ((byte)(intensity.Value + 1)) : intensity.Value; |
| 146 | + } |
| 147 | + |
| 148 | + intensities[i, j] = intensity; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return intensities; |
| 153 | + } |
| 154 | + |
| 155 | + private static void GetComponentsFromPixel(int pixel, out byte a, out byte r, out byte g, out byte b) |
| 156 | + { |
| 157 | + b = (byte)(pixel & 0xFF); |
| 158 | + g = (byte)((pixel >> 8) & 0xFF); |
| 159 | + r = (byte)((pixel >> 16) & 0xFF); |
| 160 | + a = (byte)((pixel >> 24) & 0xFF); |
| 161 | + } |
| 162 | + |
| 163 | + private static int[] GetPixels(BitmapSource source) |
| 164 | + { |
| 165 | + int[] pixels = new int[source.PixelWidth * source.PixelHeight]; |
| 166 | + if (source.Format == PixelFormats.Bgr32 || source.Format == PixelFormats.Bgra32 || source.Format == PixelFormats.Pbgra32) |
| 167 | + { |
| 168 | + checked |
| 169 | + { |
| 170 | + source.CopyPixels(pixels, source.PixelWidth * 4, 0); |
| 171 | + } |
| 172 | + } |
| 173 | + else if (source.Format == PixelFormats.Indexed8) |
| 174 | + { |
| 175 | + byte[] indices = new byte[source.PixelWidth * source.PixelHeight]; |
| 176 | + source.CopyPixels(indices, source.PixelWidth, 0); |
| 177 | + for (int i = 0; i < indices.Length; ++i) |
| 178 | + { |
| 179 | + Color c = source.Palette.Colors[indices[i]]; |
| 180 | + pixels[i] = (c.A << 24) | (c.R << 16) | (c.G << 8) | (c.B << 0); |
| 181 | + } |
| 182 | + } |
| 183 | + else |
| 184 | + { |
| 185 | + FormatConvertedBitmap converted = new FormatConvertedBitmap(source, PixelFormats.Bgra32, null, 0); |
| 186 | + converted.CopyPixels(pixels, source.PixelWidth * 4, 0); |
| 187 | + } |
| 188 | + |
| 189 | + return pixels; |
| 190 | + } |
| 191 | + |
| 192 | + private static byte GetGrayIntensity(byte r, byte g, byte b) |
| 193 | + { |
| 194 | + return (byte)(0.2126 * r + 0.7152 * g + 0.0722 * b); |
| 195 | + } |
| 196 | +``` |
| 197 | + |
| 198 | +Ensure to adjust the `MakeGrayscale` methods for `ColorBase`, `Path`, and `Image` according to your specific needs. This sample demonstrates the basic approach to converting document elements to grayscale but might require adjustments for complex scenarios or specific color processing requirements. |
| 199 | + |
| 200 | +## See Also |
| 201 | + |
| 202 | +- [RadPdfProcessing Overview]({%slug radpdfprocessing-overview%}) |
| 203 | +- [How to Generate a PDF Document from Images with FixedContentEditor]({%slug pdf-from-images-with-fixedcontenteditor%}) |
| 204 | +- [How to Generate a PDF Document from Images with RadFixedDocumentEditor]({%slug pdf-from-images-with-radfixeddocumenteditor%}) |
0 commit comments