Skip to content

Commit b67cfec

Browse files
Merge pull request #495 from telerik/new-kb-change-block-text-color-in-pdf-table-660a82dac9264a21b31c875748ef554e
Added new kb article change-block-text-color-in-pdf-table
2 parents 8aeeb68 + 4bf35e7 commit b67cfec

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
title: Changing Block's Text Color in PDF Documents Using RadPdfProcessing
3+
description: Learn how to modify the text color within tables in PDF documents using the RadPdfProcessing library.
4+
type: how-to
5+
page_title: How to Modify Text Color in PDF Tables with RadPdfProcessing
6+
slug: change-text-color-pdf-radpdfprocessing
7+
tags: pdf, document, processing, text, color, foreground, table, cell, block
8+
res_type: kb
9+
ticketid: 1674934
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+
When working with PDF documents using [RadPdfProcessing]({%slug radpdfprocessing-overview%}), you may need to change the foreground color of the text inside a table to differentiate between various pieces of information, such as an account number and its value. This knowledge base article also answers the following questions:
21+
- How to change the text color within a PDF table using RadPdfProcessing?
22+
- How to differentiate text elements in a PDF document by color?
23+
- How to apply foreground colors to the text of Blocks within a PDF table?
24+
25+
## Solution
26+
27+
To change the text color inside a table in a PDF document using RadPdfProcessing, use the **FillColor** property of [GraphicProperties]({%slug radpdfprocessing-editing-text-and-graphic-properties%}). This property controls the color used for drawing the content elements of a `Block`. You can temporarily change the graphic properties for specific text elements by using the `SaveGraphicProperties()` and `RestoreGraphicProperties()` methods. This allows you to apply different colors, at different stages, to different parts of the text inside a table cell.
28+
29+
Here's how to achieve this:
30+
31+
1. Create a [Table]({%slug radpdfprocessing-editing-table%}) and add a [Row]({%slug radpdfprocessing-editing-tablerow%}) and a [Cell]({%slug radpdfprocessing-editing-tablecell%}) to it.
32+
2. Add a [Block]({%slug radpdfprocessing-editing-block%}) to the cell for the text you want to display.
33+
3. Use `SaveGraphicProperties()` to save the current graphic state.
34+
4. Set the [FillColor]({%slug radpdfprocessing-concepts-colors-and-color-spaces%}) property of [GraphicProperties]({%slug radpdfprocessing-editing-text-and-graphic-properties%}) to the desired color.
35+
5. Insert the text into the block.
36+
6. Use `RestoreGraphicProperties()` to revert to the previous graphic state.
37+
7. Repeat steps 2-6 for any additional text blocks with different colors.
38+
39+
```csharp
40+
Table table = new Table();
41+
table.LayoutType = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.TableLayoutType.FixedWidth;
42+
43+
TableRow row = table.Rows.AddTableRow();
44+
TableCell cell = row.Cells.AddTableCell();
45+
46+
// First text block
47+
Block block = cell.Blocks.AddBlock();
48+
block.SaveGraphicProperties();
49+
block.GraphicProperties.FillColor = new RgbColor(0, 0, 255); // Blue color for "Account No."
50+
block.InsertText("Account No.");
51+
block.RestoreGraphicProperties();
52+
53+
// Second text block
54+
block = cell.Blocks.AddBlock();
55+
block.SaveGraphicProperties();
56+
block.GraphicProperties.FillColor = new RgbColor(0, 255, 0); // Green color for the account number value
57+
block.InsertText("12345678910");
58+
block.RestoreGraphicProperties();
59+
```
60+
61+
![Change Text Color in PDF](images/change-text-color-pdf-radpdfprocessing.png)
62+
63+
By following these steps, you can successfully differentiate text elements in a PDF document by changing their foreground colors.
64+
65+
## See Also
66+
67+
- [Text and Graphic Properties in RadPdfProcessing]({%slugradpdfprocessing-editing-text-and-graphic-properties%})
68+
- [Block Content in RadPdfProcessing]({%slug radpdfprocessing-editing-block%})
69+
- [Colors and Color Spaces]({%slug radpdfprocessing-concepts-colors-and-color-spaces%})
5.74 KB
Loading

libraries/radpdfprocessing/editing/text-and-graphic-properties.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,4 @@ Both Text and Graphic properties contain methods that can preserve and restore t
126126

127127
* [FixedContentEditor]({%slug radpdfprocessing-editing-fixedcontenteditor%})
128128
* [Block]({%slug radpdfprocessing-editing-block%})
129+
* [Changing Block's Text Color in PDF Documents Using RadPdfProcessing]({%slug change-text-color-pdf-radpdfprocessing%})

0 commit comments

Comments
 (0)