|
| 1 | +--- |
| 2 | +title: Setting Text Color Using PdfProcessing |
| 3 | +description: Learn how to set the text color using the Telerik PdfProcessing library. |
| 4 | +type: how-to |
| 5 | +page_title: How to Change Text Color Using PdfProcessing |
| 6 | +meta_title: How to Change Text Color Using PdfProcessing |
| 7 | +slug: pdfprocessing-text-color |
| 8 | +tags: pdf, processing, text color, cell, fill, color, graphic, properties |
| 9 | +res_type: kb |
| 10 | +ticketid: 1695311 |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | + |
| 15 | +| Version | Product | Author | |
| 16 | +| ---- | ---- | ---- | |
| 17 | +| 2025.2.520| RadPdfProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| |
| 18 | + |
| 19 | +## Description |
| 20 | + |
| 21 | +I want to set the text color in a cell but cannot find an option for this in `TextProperties`. The only visible option is `HighlightColor`. |
| 22 | + |
| 23 | +This knowledge base article also answers the following questions: |
| 24 | +- How to change the text color in PdfProcessing cells? |
| 25 | +- How to use FillColor to set cell text color in Telerik Document Processing? |
| 26 | +- What is the method to apply custom text color in RadPdfProcessing blocks? |
| 27 | + |
| 28 | +## Solution |
| 29 | + |
| 30 | +To set the text color in a cell, use the `FillColor` property of `GraphicProperties`. This property determines the color used for rendering content elements of a block. To apply the change only to specific text, use the `SaveGraphicProperties()` and `RestoreGraphicProperties()` methods. These methods allow you to apply a temporary change and revert to the previous settings. |
| 31 | + |
| 32 | +Follow the steps below: |
| 33 | + |
| 34 | +1. Create a new `RadFixedDocument` and add a page. |
| 35 | +2. Initialize a `FixedContentEditor` for the page. |
| 36 | +3. Create a `Block` and call `SaveGraphicProperties()` to save the current settings. |
| 37 | +4. Assign a color to `GraphicProperties.FillColor`. |
| 38 | +5. Insert the text that needs the custom color. |
| 39 | +6. Call `RestoreGraphicProperties()` to revert to the previous settings. |
| 40 | +7. Continue adding text or elements to the block. |
| 41 | +8. Export the document using `PdfFormatProvider`. |
| 42 | + |
| 43 | +### Example Code |
| 44 | + |
| 45 | +```csharp |
| 46 | +RadFixedDocument document = new RadFixedDocument(); |
| 47 | +RadFixedPage page = document.Pages.AddPage(); |
| 48 | +FixedContentEditor editor = new FixedContentEditor(page); |
| 49 | + |
| 50 | +// Create a block to add styled content |
| 51 | +Block infoBlock = new Block(); |
| 52 | + |
| 53 | +// Save current graphic properties |
| 54 | +infoBlock.SaveGraphicProperties(); |
| 55 | + |
| 56 | +// Set the FillColor for the text |
| 57 | +infoBlock.GraphicProperties.FillColor = new RgbColor(255, 10, 10); |
| 58 | +infoBlock.InsertText("Telerik Document Processing: "); |
| 59 | + |
| 60 | +// Restore previous graphic properties |
| 61 | +infoBlock.RestoreGraphicProperties(); |
| 62 | +infoBlock.InsertLineBreak(); |
| 63 | +infoBlock.InsertText("RadPdfProcessing"); |
| 64 | +infoBlock.InsertLineBreak(); |
| 65 | + |
| 66 | +// Position and draw the block |
| 67 | +editor.Position.Translate(100, 100); |
| 68 | +editor.DrawBlock(infoBlock); |
| 69 | + |
| 70 | +// Export the document |
| 71 | +string fileName = $"{Guid.NewGuid()}.pdf"; |
| 72 | +File.Delete(fileName); |
| 73 | +PdfFormatProvider provider = new PdfFormatProvider(); |
| 74 | +using Stream stream = File.OpenWrite(fileName); |
| 75 | +provider.Export(document, stream, TimeSpan.FromSeconds(10)); |
| 76 | + |
| 77 | +Process.Start(new ProcessStartInfo() { UseShellExecute = true, FileName = fileName }); |
| 78 | +``` |
| 79 | + |
| 80 | +## See Also |
| 81 | + |
| 82 | +- [Text and Graphic Properties in PdfProcessing](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/editing/text-and-graphic-properties) |
| 83 | +- [Block Class Documentation](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/editing/block) |
| 84 | +- [RadPdfProcessing Overview](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/overview) |
0 commit comments