|
| 1 | +--- |
| 2 | +title: Converting Colored PDF Document Images to GrayScale in .NET Standard |
| 3 | +description: Learn how to convert colored images in PDF documents to grayscale using Telerik PdfProcessing in an ASP.NET Core (.NET 9) environment. |
| 4 | +type: how-to |
| 5 | +page_title: How to Convert Colored PDF Document Images to GrayScale in .NET Standard |
| 6 | +meta_title: How to Convert Colored PDF Document Images to GrayScale in .NET Standard |
| 7 | +slug: convert-pdf-grayscale-aspnet-core |
| 8 | +tags: pdf, processing, telerik, document, asp, core, convert, grayscale, image, gray, black, white, color |
| 9 | +res_type: kb |
| 10 | +ticketid: 1697916 |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | + |
| 15 | +| Version | Product | Author | |
| 16 | +| ---- | ---- | ---- | |
| 17 | +| 2025.3.806| RadPdfProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| |
| 18 | + |
| 19 | +## Description |
| 20 | + |
| 21 | +Convert colored images in PDF documents using Telerik [PdfProcessing]({%slug radpdfprocessing-overview%}) in an ASP.NET Core (.NET 9) environment. |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +## Solution |
| 27 | + |
| 28 | +To convert colored PDFs to grayscale in ASP.NET Core (.NET 9), follow these steps: |
| 29 | + |
| 30 | +1. Use the Telerik [PdfProcessing]({%slug radpdfprocessing-overview%}) library to load and manipulate PDF content. |
| 31 | +2. Replace the image processing logic with a custom implementation using `System.Drawing`. |
| 32 | + |
| 33 | +Here is the complete code example: |
| 34 | + |
| 35 | +```csharp |
| 36 | +using System; |
| 37 | +using System.Diagnostics; |
| 38 | +using System.Drawing; |
| 39 | +using System.IO; |
| 40 | +using Telerik.Windows.Documents.Fixed.FormatProviders.Pdf; |
| 41 | +using Telerik.Windows.Documents.Fixed.Model; |
| 42 | +using Telerik.Windows.Documents.Fixed.Model.Objects; |
| 43 | +using Telerik.Windows.Documents.ImageUtils; |
| 44 | + |
| 45 | +class Program |
| 46 | +{ |
| 47 | + static void Main(string[] args) |
| 48 | + { |
| 49 | + // Initialize ImagePropertiesResolver |
| 50 | + ImagePropertiesResolver defaultImagePropertiesResolver = new ImagePropertiesResolver(); |
| 51 | + Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.ImagePropertiesResolver = defaultImagePropertiesResolver; |
| 52 | + |
| 53 | + string inputPath = "colored.pdf"; // Path to the input PDF |
| 54 | + string outputPath = "grayscale.pdf"; // Path for the grayscale output |
| 55 | +
|
| 56 | + ConvertPdfToGrayscale(inputPath, outputPath); |
| 57 | + |
| 58 | + Process.Start(new ProcessStartInfo() { FileName = outputPath, UseShellExecute = true }); |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Converts all content in a PDF document to grayscale. |
| 63 | + /// </summary> |
| 64 | + public static void ConvertPdfToGrayscale(string inputPath, string outputPath) |
| 65 | + { |
| 66 | + PdfFormatProvider provider = new PdfFormatProvider(); |
| 67 | + RadFixedDocument document = provider.Import(File.ReadAllBytes(inputPath), TimeSpan.FromSeconds(10)); |
| 68 | + |
| 69 | + foreach (RadFixedPage page in document.Pages) |
| 70 | + { |
| 71 | + foreach (ContentElementBase element in page.Content) |
| 72 | + { |
| 73 | + Image img = element as Image; |
| 74 | + if (img != null) |
| 75 | + { |
| 76 | + ImageSource source = img.ImageSource; |
| 77 | + EncodedImageData encodedImageData = source.GetEncodedImageData(); |
| 78 | + byte[] original = encodedImageData.Data; |
| 79 | + |
| 80 | + Bitmap grayBitmap = ConvertToGrayscale(original); |
| 81 | + |
| 82 | + // Save the grayscale bitmap to a temporary file |
| 83 | + string tempImagePath = "temp_grayscale_image.jpeg"; |
| 84 | + grayBitmap.Save(tempImagePath, System.Drawing.Imaging.ImageFormat.Jpeg); |
| 85 | + |
| 86 | + // Update the image source in the PDF |
| 87 | + using (FileStream fs = File.Open(tempImagePath, FileMode.Open)) |
| 88 | + { |
| 89 | + ImageSource grayscaleImageSource = new ImageSource(fs); |
| 90 | + img.ImageSource = grayscaleImageSource; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // Save the modified PDF to the output path |
| 97 | + if (File.Exists(outputPath)) |
| 98 | + { |
| 99 | + File.Delete(outputPath); |
| 100 | + } |
| 101 | + |
| 102 | + File.WriteAllBytes(outputPath, provider.Export(document, TimeSpan.FromSeconds(10))); |
| 103 | + } |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Converts a byte array of image data to grayscale. |
| 107 | + /// </summary> |
| 108 | + public static Bitmap ConvertToGrayscale(byte[] imageBytes) |
| 109 | + { |
| 110 | + using (var ms = new MemoryStream(imageBytes)) |
| 111 | + { |
| 112 | + using (var originalImage = new Bitmap(ms)) |
| 113 | + { |
| 114 | + Bitmap grayBitmap = new Bitmap(originalImage.Width, originalImage.Height); |
| 115 | + |
| 116 | + for (int y = 0; y < originalImage.Height; y++) |
| 117 | + { |
| 118 | + for (int x = 0; x < originalImage.Width; x++) |
| 119 | + { |
| 120 | + Color originalColor = originalImage.GetPixel(x, y); |
| 121 | + int grayValue = (int)(originalColor.R * 0.3 + originalColor.G * 0.59 + originalColor.B * 0.11); |
| 122 | + Color grayColor = Color.FromArgb(grayValue, grayValue, grayValue); |
| 123 | + grayBitmap.SetPixel(x, y, grayColor); |
| 124 | + } |
| 125 | + } |
| 126 | + return grayBitmap; |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +## See Also |
| 134 | + |
| 135 | +- [Converting Colored PDF Documents to GrayScale in .NET Framework]({%slug convert-color-pdf-to-black-and-white-telerik-document-processing%}) |
0 commit comments