Skip to content

Commit

Permalink
Merge pull request #495 from telerik/new-kb-change-block-text-color-i…
Browse files Browse the repository at this point in the history
…n-pdf-table-660a82dac9264a21b31c875748ef554e

Added new kb article change-block-text-color-in-pdf-table
  • Loading branch information
dessyordanova authored Jan 30, 2025
2 parents 8aeeb68 + 4bf35e7 commit b67cfec
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
69 changes: 69 additions & 0 deletions knowledge-base/change-block-text-color-in-pdf-table.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
title: Changing Block's Text Color in PDF Documents Using RadPdfProcessing
description: Learn how to modify the text color within tables in PDF documents using the RadPdfProcessing library.
type: how-to
page_title: How to Modify Text Color in PDF Tables with RadPdfProcessing
slug: change-text-color-pdf-radpdfprocessing
tags: pdf, document, processing, text, color, foreground, table, cell, block
res_type: kb
ticketid: 1674934
---

## Environment

| Version | Product | Author |
| ---- | ---- | ---- |
| 2024.4.1106| RadPdfProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)|

## Description

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:
- How to change the text color within a PDF table using RadPdfProcessing?
- How to differentiate text elements in a PDF document by color?
- How to apply foreground colors to the text of Blocks within a PDF table?

## Solution

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.

Here's how to achieve this:

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.
2. Add a [Block]({%slug radpdfprocessing-editing-block%}) to the cell for the text you want to display.
3. Use `SaveGraphicProperties()` to save the current graphic state.
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.
5. Insert the text into the block.
6. Use `RestoreGraphicProperties()` to revert to the previous graphic state.
7. Repeat steps 2-6 for any additional text blocks with different colors.

```csharp
Table table = new Table();
table.LayoutType = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.TableLayoutType.FixedWidth;

TableRow row = table.Rows.AddTableRow();
TableCell cell = row.Cells.AddTableCell();

// First text block
Block block = cell.Blocks.AddBlock();
block.SaveGraphicProperties();
block.GraphicProperties.FillColor = new RgbColor(0, 0, 255); // Blue color for "Account No."
block.InsertText("Account No.");
block.RestoreGraphicProperties();

// Second text block
block = cell.Blocks.AddBlock();
block.SaveGraphicProperties();
block.GraphicProperties.FillColor = new RgbColor(0, 255, 0); // Green color for the account number value
block.InsertText("12345678910");
block.RestoreGraphicProperties();
```

![Change Text Color in PDF](images/change-text-color-pdf-radpdfprocessing.png)

By following these steps, you can successfully differentiate text elements in a PDF document by changing their foreground colors.

## See Also

- [Text and Graphic Properties in RadPdfProcessing]({%slugradpdfprocessing-editing-text-and-graphic-properties%})
- [Block Content in RadPdfProcessing]({%slug radpdfprocessing-editing-block%})
- [Colors and Color Spaces]({%slug radpdfprocessing-concepts-colors-and-color-spaces%})
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,4 @@ Both Text and Graphic properties contain methods that can preserve and restore t

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

0 comments on commit b67cfec

Please sign in to comment.