Skip to content

Commit 237fffd

Browse files
Merge pull request #543 from telerik/yoan/change-flowdocument-font
"Globally change the font of a RadFlowDocument" KB
2 parents 45a52c2 + 907a7f3 commit 237fffd

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
---
2+
title: How to Globally Change the Font of a RadFlowDocument Before Export
3+
description: Learn how to recursively change all fonts in a document before exporting it to another format using RadWordsProcessing.
4+
type: how-to
5+
page_title: How to Globally Change the Font of a RadFlowDocument Before Export
6+
slug: wordsprocessing-globally-change-font-radflowdocument
7+
tags: font, radflowdocument, wordsprocessing, document, processing, pdf, export, global, font, change
8+
res_type: kb
9+
---
10+
11+
## Environment
12+
13+
| Version | Product | Author |
14+
|----|----|----|
15+
| 2025.1.205 | [RadWordsProcessing]({%slug radwordsprocessing-overview%}) | [Yoan Karamanov](https://www.telerik.com/blogs/author/yoan-karamanov) |
16+
17+
## Description
18+
19+
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:
20+
21+
- You need to ensure proper font display in the exported document
22+
- You want to maintain consistent styling across different sections
23+
- You need to replace fonts that might not be available in the target environment or format
24+
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.
25+
26+
## Solution
27+
28+
The solution involves three key steps:
29+
30+
1. Import the document from your source format
31+
2. Recursively iterate through all [Run]({%slug radwordsprocessing-model-run%}) elements in the document and change their font
32+
3. Export the document to your desired format
33+
34+
Here's a complete code example that demonstrates how to change all fonts in a **RadFlowDocument** to _Arial_ before exporting to PDF:
35+
36+
```csharp
37+
using System;
38+
using System.IO;
39+
using Telerik.Windows.Documents.Flow.FormatProviders.Docx;
40+
using Telerik.Windows.Documents.Flow.FormatProviders.Pdf;
41+
using Telerik.Windows.Documents.Flow.Model;
42+
using Telerik.Windows.Documents.Flow.Model.Styles;
43+
44+
namespace GlobalFontChanger
45+
{
46+
class Program
47+
{
48+
static void Main(string[] args)
49+
{
50+
// 1. Import the document
51+
DocxFormatProvider docxProvider = new DocxFormatProvider();
52+
RadFlowDocument radFlowDocument;
53+
54+
using (Stream input = File.OpenRead("input.docx"))
55+
{
56+
radFlowDocument = docxProvider.Import(input);
57+
}
58+
59+
// 2. Change the font for all runs in the document
60+
foreach (Run run in radFlowDocument.EnumerateChildrenOfType<Run>())
61+
{
62+
run.FontFamily = new ThemableFontFamily("Arial");
63+
}
64+
65+
// 3. Export the document to PDF
66+
PdfFormatProvider pdfProvider = new PdfFormatProvider();
67+
68+
using (Stream output = File.OpenWrite("output.pdf"))
69+
{
70+
pdfProvider.Export(radFlowDocument, output);
71+
}
72+
73+
Console.WriteLine("Document successfully processed and exported with Arial font.");
74+
}
75+
}
76+
}
77+
```
78+
79+
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:
80+
81+
```csharp
82+
// Import from RTF
83+
RtfFormatProvider rtfProvider = new RtfFormatProvider();
84+
using (Stream input = File.OpenRead("input.rtf"))
85+
{
86+
radFlowDocument = rtfProvider.Import(input);
87+
}
88+
89+
// Change fonts as shown above
90+
91+
// Export to DOCX
92+
DocxFormatProvider docxExportProvider = new DocxFormatProvider();
93+
using (Stream output = File.OpenWrite("output.docx"))
94+
{
95+
docxExportProvider.Export(radFlowDocument, output);
96+
}
97+
```
98+
99+
### Advanced Font Handling
100+
101+
If you need more control over font changes, you can implement conditional logic:
102+
103+
```csharp
104+
foreach (Run run in radFlowDocument.EnumerateChildrenOfType<Run>())
105+
{
106+
// Change only specific fonts
107+
if (run.FontFamily.ToString().Contains("Times New Roman"))
108+
{
109+
run.FontFamily = new ThemableFontFamily("Arial");
110+
}
111+
else if (run.FontFamily.ToString().Contains("Calibri"))
112+
{
113+
run.FontFamily = new ThemableFontFamily("Verdana");
114+
}
115+
116+
// Transform bold styling to italic
117+
if (run.FontWeight == FontWeights.Bold)
118+
{
119+
run.FontWeight = FontWeights.Normal;
120+
run.FontStyle = FontStyles.Italic;
121+
}
122+
// Transform italic styling to bold
123+
else if (run.FontStyle == FontStyles.Italic)
124+
{
125+
run.FontStyle = FontStyles.Normal;
126+
run.FontWeight = FontWeights.Bold;
127+
}
128+
129+
// Increase font size for small text, decrease for large text
130+
if (run.FontSize < 11)
131+
{
132+
run.FontSize = 11; // Increase smaller fonts
133+
}
134+
else if (run.FontSize > 16)
135+
{
136+
run.FontSize = 16; // Decrease larger fonts
137+
}
138+
}
139+
```
140+
141+
>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).
142+
143+
## See Also
144+
145+
* [RadFlowDocument Overview]({%slug radwordsprocessing-model-radflowdocument%})
146+
* [Working with Runs]({%slug radwordsprocessing-model-run%})
147+
* [Styles and Formatting]({%slug radwordsprocessing-concepts-styles%})
148+
* [Style Properties]({%slug radwordsprocessing-concepts-style-properties%})

0 commit comments

Comments
 (0)