Resolving Distorted Font of TOC (Table of Contents) Title When Converting DOCX to PDF
Environment
| Version | Product | Author |
|---|---|---|
| 2026.1.210 | RadWordsProcessing | Desislava Yordanova |
| 4.8.1 | .NET Framework |
Description
When converting a DOCX file to PDF format using the WordsProcessing library, the text's font may differ from the original file for the TOC (Table of contents) title. This article explains why this is observed and how to handle this behavior.
Solution
The TOC title uses the TOC Heading style.
In Word (and in RadWordsProcessing), the text Table of Contents is formatted by the built‑in style TOC Heading. If that style isn’t explicitly set (or you set it before the TOC updates), the export may fall back to defaults and you’ll see a different font in the PDF.
Once the DOCX file is imported in a RadFlowDocument using the DocxFormatProvider, there are two possible approaches to specify the font for the TOC title:
Applying a Custom Style
- Define a custom style for the "Table of Contents" title.
- Apply the custom style to the corresponding paragraph in the document.
var style = new Telerik.Windows.Documents.Flow.Model.Styles.Style(BuiltInStyleNames.TocHeadingStyleId, StyleType.Character);
style.CharacterProperties.FontFamily.LocalValue = new ThemableFontFamily("Times New Roman");
style.CharacterProperties.FontSize.LocalValue = 14;
document.StyleRepository.Add(style);
var tocTitle = document
.EnumerateChildrenOfType<Paragraph>()
.FirstOrDefault(p =>
p.Inlines.OfType<Run>().Any(r =>
(r.Text ?? string.Empty).Trim().Equals("Table of Contents", StringComparison.OrdinalIgnoreCase) ||
(r.Text ?? string.Empty).Trim().Equals("Contents", StringComparison.OrdinalIgnoreCase)));
if (tocTitle != null)
{
tocTitle.StyleId = BuiltInStyleNames.TocHeadingStyleId;
}
Modifying the Applied Style
-
Get the style that is already applied to the TOC title.
-
Adjust the imported style and apply the desired font:
var tocTitle = document
.EnumerateChildrenOfType<Paragraph>()
.FirstOrDefault(p =>
p.Inlines.OfType<Run>().Any(r =>
(r.Text ?? string.Empty).Trim().Equals("Table of Contents", StringComparison.OrdinalIgnoreCase) ||
(r.Text ?? string.Empty).Trim().Equals("Contents", StringComparison.OrdinalIgnoreCase)));
if (tocTitle != null)
{
var tocHeadingStyle = document.StyleRepository.GetStyle(tocTitle.StyleId);
if (tocHeadingStyle != null)
{
tocHeadingStyle.CharacterProperties.FontFamily.LocalValue = new ThemableFontFamily("Times New Roman");
tocHeadingStyle.CharacterProperties.FontWeight.LocalValue = FontWeights.Bold;
}
}