-
Notifications
You must be signed in to change notification settings - Fork 41
Added new kb article convert-color-pdf-to-black-and-white-telerik-document-processing #493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
YoanKar
merged 4 commits into
master
from
new-kb-convert-color-pdf-to-black-and-white-telerik-document-processing-e355c1c1edb14e86b52a7c951b5a84ec
Feb 25, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0870f3d
Added new kb article convert-color-pdf-to-black-and-white-telerik-doc…
162b615
Update convert-color-pdf-to-black-and-white-telerik-document-processi…
dessyordanova 8d190af
new KB - Converting Colored PDF Documents to GrayScale with Telerik D…
dessyordanova d5d92af
eliminated the reporting part
dessyordanova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
204 changes: 204 additions & 0 deletions
204
knowledge-base/convert-color-pdf-to-black-and-white-telerik-document-processing.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| --- | ||
| title: Converting Colored PDF Documents to GrayScale with Telerik Document Processing | ||
| description: Learn how to use Telerik Document Processing libraries to convert color PDF reports into black and white format. | ||
| type: how-to | ||
| page_title: How to Convert Color PDF Reports to Black & White Using Telerik Document Processing | ||
| slug: convert-color-pdf-to-black-and-white-telerik-document-processing | ||
| tags: document, processing, pdf, conversion, black, white, gray, image, convert | ||
| res_type: kb | ||
| ticketid: 1675661 | ||
| --- | ||
|
|
||
| ## Environment | ||
|
|
||
| | Version | Product | Author | | ||
| | ---- | ---- | ---- | | ||
| | 2024.4.1106| RadPdfProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| | ||
|
|
||
| ## Description | ||
|
|
||
| This article shows a sample approach how to convert a **colored** PDF document to a **grayscale** one with [RadPdfProcessing]({%slug radpdfprocessing-overview%}). | ||
|
|
||
|  | ||
|
|
||
| ## Solution | ||
|
|
||
| To convert a colored PDF file to black and white using Telerik Document Processing, follow the steps below: | ||
|
|
||
| 1. Use the [RadPdfProcessing]({%slug radpdfprocessing-overview%}) library to import the PDF file. | ||
| 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. | ||
| 3. Modify the colors to grayscale and export the processed file as a new PDF document. | ||
|
|
||
| Here is a complete code snippet that demonstrates how to achieve this conversion: | ||
|
|
||
| ```csharp | ||
| static void Main(string[] args) | ||
| { | ||
| PdfFormatProvider provider = new PdfFormatProvider(); | ||
| RadFixedDocument document = provider.Import(System.IO.File.ReadAllBytes(filePath)); | ||
|
|
||
| foreach (RadFixedPage page in document.Pages) | ||
| { | ||
| foreach (ContentElementBase element in page.Content) | ||
| { | ||
| MakeGrayscale(element); | ||
| } | ||
| } | ||
|
|
||
| string resultFile = "grayscale.pdf"; | ||
|
|
||
| if (System.IO.File.Exists(resultFile)) | ||
| { | ||
| System.IO.File.Delete(resultFile); | ||
| } | ||
|
|
||
| System.IO.File.WriteAllBytes(resultFile, provider.Export(document, TimeSpan.FromSeconds(10))); | ||
| Process.Start(resultFile); | ||
| } | ||
| private static void MakeGrayscale(ContentElementBase element) | ||
| { | ||
| TextFragment text = element as TextFragment; | ||
|
|
||
| if (text != null) | ||
| { | ||
| text.Stroke = MakeGrayscale(text.Stroke); | ||
| text.Fill = MakeGrayscale(text.Fill); | ||
| } | ||
|
|
||
|
|
||
| if (element is Path path) | ||
| { | ||
| path.Stroke = MakeGrayscale(path.Stroke); | ||
| path.Fill = MakeGrayscale(path.Fill); | ||
| } | ||
|
|
||
| Image image = element as Image; | ||
|
|
||
| if (image != null) | ||
| { | ||
| BitmapSource originalImage = image.ImageSource.GetBitmapSource(); | ||
| BitmapSource grayscaleImage = MakeGrayscale(originalImage); | ||
| image.ImageSource = new Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource(grayscaleImage); | ||
| } | ||
| } | ||
|
|
||
| private static BitmapSource MakeGrayscale(BitmapSource source) | ||
| { | ||
| byte?[,] intensities = GetPixelsIntensity(source); | ||
| BitmapSource grayscaleSource = CreateImageFromGrayPixels(intensities); | ||
|
|
||
| return grayscaleSource; | ||
| } | ||
|
|
||
| private static ColorBase MakeGrayscale(ColorBase color) | ||
| { | ||
| RgbColor rgb = (RgbColor)color; | ||
|
|
||
| byte gray = GetGrayIntensity(rgb.R, rgb.G, rgb.G); | ||
|
|
||
| return new RgbColor(rgb.A, gray, gray, gray); | ||
| } | ||
|
|
||
| private static BitmapSource CreateImageFromGrayPixels(byte?[,] pixels) | ||
| { | ||
| double dpi = 96; | ||
| int height = pixels.GetLength(0); | ||
| int width = pixels.GetLength(1); | ||
| byte[] pixelData = new byte[width * height]; | ||
|
|
||
| for (int y = 0; y < height; ++y) | ||
| { | ||
| int yIndex = y * width; | ||
| for (int x = 0; x < width; ++x) | ||
| { | ||
| pixelData[x + yIndex] = pixels[y, x] ?? 255; | ||
| } | ||
| } | ||
|
|
||
| BitmapSource bmpSource = BitmapSource.Create(width, height, dpi, dpi, PixelFormats.Gray8, null, pixelData, width); | ||
|
|
||
| return bmpSource; | ||
| } | ||
|
|
||
| private static byte?[,] GetPixelsIntensity(BitmapSource bitmapSource) | ||
| { | ||
| int width = bitmapSource.PixelWidth; | ||
| int height = bitmapSource.PixelHeight; | ||
| byte?[,] intensities = new byte?[height, width]; | ||
| int[] pixels = GetPixels(bitmapSource); | ||
| int pixelIndex = 0; | ||
|
|
||
| for (int i = 0; i < height; i++) | ||
| { | ||
| for (int j = 0; j < width; j++) | ||
| { | ||
| byte a, r, g, b; | ||
| GetComponentsFromPixel(pixels[pixelIndex++], out a, out r, out g, out b); | ||
| byte? intensity; | ||
| if (a == 0) | ||
| { | ||
| intensity = null; | ||
| } | ||
| else | ||
| { | ||
| intensity = (byte)((a / 255.0) * GetGrayIntensity(r, g, b)); | ||
| intensity = intensity.Value < 255 ? ((byte)(intensity.Value + 1)) : intensity.Value; | ||
| } | ||
|
|
||
| intensities[i, j] = intensity; | ||
| } | ||
| } | ||
|
|
||
| return intensities; | ||
| } | ||
|
|
||
| private static void GetComponentsFromPixel(int pixel, out byte a, out byte r, out byte g, out byte b) | ||
| { | ||
| b = (byte)(pixel & 0xFF); | ||
| g = (byte)((pixel >> 8) & 0xFF); | ||
| r = (byte)((pixel >> 16) & 0xFF); | ||
| a = (byte)((pixel >> 24) & 0xFF); | ||
| } | ||
|
|
||
| private static int[] GetPixels(BitmapSource source) | ||
| { | ||
| int[] pixels = new int[source.PixelWidth * source.PixelHeight]; | ||
| if (source.Format == PixelFormats.Bgr32 || source.Format == PixelFormats.Bgra32 || source.Format == PixelFormats.Pbgra32) | ||
| { | ||
| checked | ||
| { | ||
| source.CopyPixels(pixels, source.PixelWidth * 4, 0); | ||
| } | ||
| } | ||
| else if (source.Format == PixelFormats.Indexed8) | ||
| { | ||
| byte[] indices = new byte[source.PixelWidth * source.PixelHeight]; | ||
| source.CopyPixels(indices, source.PixelWidth, 0); | ||
| for (int i = 0; i < indices.Length; ++i) | ||
| { | ||
| Color c = source.Palette.Colors[indices[i]]; | ||
| pixels[i] = (c.A << 24) | (c.R << 16) | (c.G << 8) | (c.B << 0); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| FormatConvertedBitmap converted = new FormatConvertedBitmap(source, PixelFormats.Bgra32, null, 0); | ||
| converted.CopyPixels(pixels, source.PixelWidth * 4, 0); | ||
| } | ||
|
|
||
| return pixels; | ||
| } | ||
|
|
||
| private static byte GetGrayIntensity(byte r, byte g, byte b) | ||
| { | ||
| return (byte)(0.2126 * r + 0.7152 * g + 0.0722 * b); | ||
| } | ||
| ``` | ||
|
|
||
| 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. | ||
|
|
||
| ## See Also | ||
|
|
||
| - [RadPdfProcessing Overview]({%slug radpdfprocessing-overview%}) | ||
| - [How to Generate a PDF Document from Images with FixedContentEditor]({%slug pdf-from-images-with-fixedcontenteditor%}) | ||
| - [How to Generate a PDF Document from Images with RadFixedDocumentEditor]({%slug pdf-from-images-with-radfixeddocumenteditor%}) | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.