Skip to content

Commit 0870f3d

Browse files
author
KB Bot
committed
Added new kb article convert-color-pdf-to-black-and-white-telerik-document-processing
1 parent 8a1532f commit 0870f3d

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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, grays
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.
21+
22+
## Solution
23+
24+
To convert a colored PDF file to black and white using Telerik Document Processing, follow the steps below:
25+
26+
1. Use the [RadPdfProcessing](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/overview) library to import the PDF file.
27+
2. Iterate through the content of the PDF file, including Path, TextFragment, and Image instances.
28+
3. Modify the colors to grayscale and export the processed file as a new PDF document.
29+
30+
Here is a complete code snippet that demonstrates how to achieve this conversion:
31+
32+
```csharp
33+
using System;
34+
using System.Diagnostics;
35+
using System.Windows.Media.Imaging;
36+
using Telerik.Windows.Documents.Fixed.FormatProviders.Pdf;
37+
using Telerik.Windows.Documents.Fixed.Model;
38+
using Telerik.Windows.Documents.Fixed.Model.Graphics;
39+
using Telerik.Windows.Documents.Fixed.Model.Objects;
40+
using Telerik.Windows.Documents.Fixed.Model.Text;
41+
42+
namespace PDFToBlackAndWhite
43+
{
44+
class Program
45+
{
46+
static void Main(string[] args)
47+
{
48+
// Load your original PDF report
49+
PdfFormatProvider provider = new PdfFormatProvider();
50+
RadFixedDocument document = provider.Import(System.IO.File.ReadAllBytes("yourOriginalReport.pdf"));
51+
52+
// Convert each element's color to grayscale
53+
foreach (RadFixedPage page in document.Pages)
54+
{
55+
foreach (ContentElementBase element in page.Content)
56+
{
57+
MakeGrayscale(element);
58+
}
59+
}
60+
61+
// Export the modified document as a new PDF file
62+
string resultFile = "grayscaleReport.pdf";
63+
System.IO.File.WriteAllBytes(resultFile, provider.Export(document));
64+
Process.Start(new ProcessStartInfo(resultFile) { UseShellExecute = true });
65+
}
66+
67+
private static void MakeGrayscale(ContentElementBase element)
68+
{
69+
if (element is TextFragment text)
70+
{
71+
text.Stroke = MakeGrayscale(text.Stroke);
72+
text.Fill = MakeGrayscale(text.Fill);
73+
}
74+
75+
if (element is Path path)
76+
{
77+
path.Stroke = MakeGrayscale(path.Stroke);
78+
path.Fill = MakeGrayscale(path.Fill);
79+
}
80+
81+
if (element is Image image)
82+
{
83+
// Convert the image to grayscale - Implementation depends on your specific requirements
84+
}
85+
}
86+
87+
// Implement the MakeGrayscale method for ColorBase, Path, and Image as per your requirements
88+
}
89+
}
90+
```
91+
92+
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.
93+
## See Also
94+
95+
- [RadPdfProcessing Overview](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/overview)
96+
- [Telerik Document Processing Introduction](https://docs.telerik.com/devtools/document-processing/introduction)
97+
- [Conditional Formatting in Telerik Reporting](https://docs.telerik.com/reporting/designing-reports/connecting-to-data/expressions/using-expressions/conditional-formatting)

0 commit comments

Comments
 (0)