-
Notifications
You must be signed in to change notification settings - Fork 41
"Globally change the font of a RadFlowDocument" KB #543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
148 changes: 148 additions & 0 deletions
148
knowledge-base/wordsprocessing-globally-change-font-radflowdocument.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
--- | ||
title: How to Globally Change the Font of a RadFlowDocument Before Export | ||
description: Learn how to recursively change all fonts in a document before exporting it to another format using RadWordsProcessing. | ||
type: how-to | ||
page_title: How to Globally Change the Font of a RadFlowDocument Before Export | ||
slug: wordsprocessing-globally-change-font-radflowdocument | ||
tags: font, radflowdocument, wordsprocessing, document, processing, pdf, export, global, font, change | ||
res_type: kb | ||
--- | ||
|
||
## Environment | ||
|
||
| Version | Product | Author | | ||
|----|----|----| | ||
| 2025.1.205 | [RadWordsProcessing]({%slug radwordsprocessing-overview%}) | [Yoan Karamanov](https://www.telerik.com/blogs/author/yoan-karamanov) | | ||
|
||
## Description | ||
|
||
When working with documents in various Word formats, you might need to ensure consistent font usage across the entire document before exporting it. This is particularly useful when: | ||
|
||
- You need to ensure proper font display in the exported document | ||
- You want to maintain consistent styling across different sections | ||
- You need to replace fonts that might not be available in the target environment or format | ||
This article demonstrates how to recursively iterate through all text runs in a [RadFlowDocument]({%slug radwordsprocessing-model-radflowdocument%}) and apply a global font change before export. The solution supports various document formats that can be imported as a **RadFlowDocument**, including DOCX, DOC, RTF, HTML, and plain text. | ||
|
||
## Solution | ||
|
||
The solution involves three key steps: | ||
|
||
1. Import the document from your source format | ||
2. Recursively iterate through all [Run]({%slug radwordsprocessing-model-run%}) elements in the document and change their font | ||
3. Export the document to your desired format | ||
|
||
Here's a complete code example that demonstrates how to change all fonts in a **RadFlowDocument** to _Arial_ before exporting to PDF: | ||
|
||
```csharp | ||
using System; | ||
using System.IO; | ||
using Telerik.Windows.Documents.Flow.FormatProviders.Docx; | ||
using Telerik.Windows.Documents.Flow.FormatProviders.Pdf; | ||
using Telerik.Windows.Documents.Flow.Model; | ||
using Telerik.Windows.Documents.Flow.Model.Styles; | ||
|
||
namespace GlobalFontChanger | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
// 1. Import the document | ||
DocxFormatProvider docxProvider = new DocxFormatProvider(); | ||
RadFlowDocument radFlowDocument; | ||
|
||
using (Stream input = File.OpenRead("input.docx")) | ||
{ | ||
radFlowDocument = docxProvider.Import(input); | ||
} | ||
|
||
// 2. Change the font for all runs in the document | ||
foreach (Run run in radFlowDocument.EnumerateChildrenOfType<Run>()) | ||
{ | ||
run.FontFamily = new ThemableFontFamily("Arial"); | ||
} | ||
|
||
// 3. Export the document to PDF | ||
PdfFormatProvider pdfProvider = new PdfFormatProvider(); | ||
|
||
using (Stream output = File.OpenWrite("output.pdf")) | ||
{ | ||
pdfProvider.Export(radFlowDocument, output); | ||
} | ||
|
||
Console.WriteLine("Document successfully processed and exported with Arial font."); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
You can adapt this solution to work with other formats by changing the import and export format providers. For example, to import from RTF and export to DOCX: | ||
|
||
```csharp | ||
// Import from RTF | ||
RtfFormatProvider rtfProvider = new RtfFormatProvider(); | ||
using (Stream input = File.OpenRead("input.rtf")) | ||
{ | ||
radFlowDocument = rtfProvider.Import(input); | ||
} | ||
|
||
// Change fonts as shown above | ||
|
||
// Export to DOCX | ||
DocxFormatProvider docxExportProvider = new DocxFormatProvider(); | ||
using (Stream output = File.OpenWrite("output.docx")) | ||
{ | ||
docxExportProvider.Export(radFlowDocument, output); | ||
} | ||
``` | ||
|
||
### Advanced Font Handling | ||
|
||
If you need more control over font changes, you can implement conditional logic: | ||
|
||
```csharp | ||
foreach (Run run in radFlowDocument.EnumerateChildrenOfType<Run>()) | ||
{ | ||
// Change only specific fonts | ||
if (run.FontFamily.ToString().Contains("Times New Roman")) | ||
{ | ||
run.FontFamily = new ThemableFontFamily("Arial"); | ||
} | ||
else if (run.FontFamily.ToString().Contains("Calibri")) | ||
{ | ||
run.FontFamily = new ThemableFontFamily("Verdana"); | ||
} | ||
|
||
// Transform bold styling to italic | ||
if (run.FontWeight == FontWeights.Bold) | ||
{ | ||
run.FontWeight = FontWeights.Normal; | ||
run.FontStyle = FontStyles.Italic; | ||
} | ||
// Transform italic styling to bold | ||
else if (run.FontStyle == FontStyles.Italic) | ||
{ | ||
run.FontStyle = FontStyles.Normal; | ||
run.FontWeight = FontWeights.Bold; | ||
} | ||
|
||
// Increase font size for small text, decrease for large text | ||
if (run.FontSize < 11) | ||
{ | ||
run.FontSize = 11; // Increase smaller fonts | ||
} | ||
else if (run.FontSize > 16) | ||
{ | ||
run.FontSize = 16; // Decrease larger fonts | ||
} | ||
} | ||
``` | ||
|
||
>note In **.NET Standard**, font handling for PDF export differs from other frameworks. System fonts may not be properly embedded or may use fallback fonts which can affect text appearance and layout in documents exported to PDF. For more details on how to resolve this, see the [Export section of the Flow PdfFormatProvider]({%slug radwordsprocessing-formats-and-conversion-pdf-pdfformatprovider%}#export). | ||
|
||
## See Also | ||
|
||
* [RadFlowDocument Overview]({%slug radwordsprocessing-model-radflowdocument%}) | ||
* [Working with Runs]({%slug radwordsprocessing-model-run%}) | ||
* [Styles and Formatting]({%slug radwordsprocessing-concepts-styles%}) | ||
* [Style Properties]({%slug radwordsprocessing-concepts-style-properties%}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it is good to mention (if you decide) about the fonts providers (for exporting to PDF) in .NET Standard. Adding a link would be enough.