diff --git a/knowledge-base/images/pdfprocessing-text-color.png b/knowledge-base/images/pdfprocessing-text-color.png new file mode 100644 index 00000000..eb2e2718 Binary files /dev/null and b/knowledge-base/images/pdfprocessing-text-color.png differ diff --git a/knowledge-base/pdfprocessing-text-color.md b/knowledge-base/pdfprocessing-text-color.md new file mode 100644 index 00000000..e2d0a5db --- /dev/null +++ b/knowledge-base/pdfprocessing-text-color.md @@ -0,0 +1,68 @@ +--- +title: Setting Text Color Using PdfProcessing +description: Learn how to set/change text color using the Telerik PdfProcessing library. +type: how-to +page_title: How to Set Text Color Using PdfProcessing +meta_title: How to Set Text Color Using PdfProcessing +slug: pdfprocessing-text-color +tags: pdf, processing, text, color, block, color, graphic, set, change +res_type: kb +ticketid: 1695311 +--- + +## Environment + +| Version | Product | Author | +| ---- | ---- | ---- | +| 2025.2.520| RadPdfProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| + +## Description + +Learn how to change the text color of a [Block]({%slug radpdfprocessing-editing-block%}). + +![Change Text Block's ForeColor ><](images/pdfprocessing-text-color.png) + +## Solution + +To set the text color of a block, use the `FillColor` property of the [Block]({%slug radpdfprocessing-editing-block%})'s `GraphicProperties`. This property determines the color used for rendering the 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. + +### Example Code + +```csharp +RadFixedDocument document = new RadFixedDocument(); +RadFixedPage page = document.Pages.AddPage(); +FixedContentEditor editor = new FixedContentEditor(page); + +// Create a block to add styled content +Block infoBlock = new Block(); + +// Save current graphic properties +infoBlock.SaveGraphicProperties(); + +// Set the FillColor for the text +infoBlock.GraphicProperties.FillColor = new RgbColor(255, 10, 10); +infoBlock.InsertText("Telerik Document Processing: "); + +// Restore previous graphic properties +infoBlock.RestoreGraphicProperties(); +infoBlock.InsertLineBreak(); +infoBlock.InsertText("RadPdfProcessing"); +infoBlock.InsertLineBreak(); + +// Position and draw the block +editor.Position.Translate(100, 100); +editor.DrawBlock(infoBlock); + +// Export the document +string fileName = $"{Guid.NewGuid()}.pdf"; +File.Delete(fileName); +PdfFormatProvider provider = new PdfFormatProvider(); +using Stream stream = File.OpenWrite(fileName); +provider.Export(document, stream, TimeSpan.FromSeconds(10)); + +Process.Start(new ProcessStartInfo() { UseShellExecute = true, FileName = fileName }); +``` + +## See Also + +- [Text and Graphic Properties in PdfProcessing]({%slug radpdfprocessing-editing-text-and-graphic-properties%}) diff --git a/libraries/radpdfprocessing/editing/block.md b/libraries/radpdfprocessing/editing/block.md index b3325a87..be168ee0 100644 --- a/libraries/radpdfprocessing/editing/block.md +++ b/libraries/radpdfprocessing/editing/block.md @@ -298,3 +298,4 @@ The code in __Example 9__ splits a block in two. The first will contains text "H * [How to Measure Text in WordsProcessing .NET Framework]({%slug wordsprocessing-measure-text-netframework%}) * [How to Measure Text in WordsProcessing .NET Standard]({%slug wordsprocessing-measure-text-netstandard%}) * [How to Generate a PDF Document from Images with FixedContentEditor]({%slug pdf-from-images-with-fixedcontenteditor%}) + * [How to Change Text Color Using PdfProcessing]({%slug pdfprocessing-text-color%}) diff --git a/libraries/radpdfprocessing/editing/text-and-graphic-properties.md b/libraries/radpdfprocessing/editing/text-and-graphic-properties.md index 871d20ef..b684c251 100644 --- a/libraries/radpdfprocessing/editing/text-and-graphic-properties.md +++ b/libraries/radpdfprocessing/editing/text-and-graphic-properties.md @@ -127,3 +127,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%}) + * [How to Change Text Color Using PdfProcessing]({%slug pdfprocessing-text-color%})