Skip to content

Commit 16b983b

Browse files
Merge pull request #544 from telerik/new-kb-assigning-character-style-to-fields-d02c20031d564bf4a610c796d8d25663
Added new kb article assigning-character-style-to-fields
2 parents c5f80fd + 5cf2402 commit 16b983b

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
title: Assigning Character Style to Fields in RadWordsProcessing
3+
description: Learn how to apply a custom character style to fields in RadWordsProcessing for Document Processing and export the document to PDF.
4+
type: how-to
5+
page_title: Applying Custom Character Style to Fields in RadWordsProcessing
6+
slug: assigning-character-style-to-fields
7+
tags: wordsprocessing, document, processing, fields, character, style, pdf, export
8+
res_type: kb
9+
ticketid: 1686361
10+
---
11+
12+
## Environment
13+
14+
| Version | Product | Author |
15+
| ---- | ---- | ---- |
16+
| 2025.1.205| RadWordsProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)|
17+
18+
## Description
19+
20+
Learn how to apply a custom character style to fields, such as [page number]({%slug radwordsprocessing-concepts-page-field%}) and [total pages]({%slug radwordsprocessing-concepts-sectionpages-field%}), in [RadWordsProcessing]({%slug radwordsprocessing-overview%}). The document reflects the applied styles when exporting to PDF format.
21+
22+
![Character Style in FieldInfo](images/character-style-in-fieldinfo.png)
23+
24+
## Solution
25+
26+
To assign a custom character style to fields in RadWordsProcessing and export the document to PDF, follow the steps below:
27+
28+
1. Create and define the [custom character styles]({%slug radwordsprocessing-concepts-styles%}).
29+
2. Add the styles to the document's `StyleRepository`.
30+
3. Specify the desired style for the [Run]({%slug radwordsprocessing-model-run%}) objects in the [field]({%slug radwordsprocessing-concepts-fields%}) or the paragraph containing the field.
31+
4. Export the document to PDF format.
32+
33+
Here is a complete code example:
34+
35+
```csharp
36+
RadFlowDocument document = new RadFlowDocument();
37+
Section section = document.Sections.AddSection();
38+
Footer footer = section.Footers.Add();
39+
40+
// Add a paragraph to the footer
41+
Paragraph paragraph = footer.Blocks.AddParagraph();
42+
paragraph.TextAlignment = Alignment.Right;
43+
44+
// Create a document editor
45+
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
46+
editor.MoveToParagraphStart(paragraph);
47+
48+
// Define custom character styles
49+
Style characterStyle = new Style("Character Style", StyleType.Character);
50+
characterStyle.Name = "Character Style";
51+
characterStyle.CharacterProperties.FontSize.LocalValue = 12;
52+
characterStyle.CharacterProperties.FontWeight.LocalValue = FontWeights.Normal;
53+
characterStyle.CharacterProperties.ForegroundColor.LocalValue = new ThemableColor(Colors.Red);
54+
document.StyleRepository.Add(characterStyle);
55+
56+
Style footerCharacterStyle = new Style("Footer Character Style", StyleType.Character);
57+
footerCharacterStyle.Name = "Footer Character Style";
58+
footerCharacterStyle.CharacterProperties.FontSize.LocalValue = 12;
59+
footerCharacterStyle.CharacterProperties.FontWeight.LocalValue = FontWeights.Normal;
60+
footerCharacterStyle.CharacterProperties.ForegroundColor.LocalValue = new ThemableColor(Colors.Aqua);
61+
document.StyleRepository.Add(footerCharacterStyle);
62+
63+
// Insert text and apply styles
64+
Run runFooterPage = editor.InsertText("Page ");
65+
runFooterPage.StyleId = characterStyle.Id;
66+
67+
Telerik.Windows.Documents.Flow.Model.Fields.FieldInfo fieldInfo = editor.InsertField("PAGE", "");
68+
fieldInfo.Start.Paragraph.StyleId = footerCharacterStyle.Id;
69+
70+
Run runFooterOf = editor.InsertText(" of ");
71+
runFooterOf.StyleId = characterStyle.Id;
72+
73+
editor.InsertField("NUMPAGES", "");
74+
75+
// Update fields in the document
76+
document.UpdateFields();
77+
78+
// Export the document to PDF
79+
string outputFilePath = "sample.pdf";
80+
File.Delete(outputFilePath);
81+
Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider provider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();
82+
using (Stream output = File.OpenWrite(outputFilePath))
83+
{
84+
provider.Export(document, output, TimeSpan.FromSeconds(10));
85+
}
86+
Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });
87+
```
88+
89+
### Key Points:
90+
- The `StyleId` property of `Run` objects allows you to associate a custom style.
91+
- Fields consist of `Start` and `End` characters; you can apply styles to these elements or to the containing paragraph.
92+
- Use the `UpdateFields` method to update the field content before export.
93+
94+
## See Also
95+
96+
- [Fields]({%slug radwordsprocessing-concepts-fields%})
97+
- [Styles]({%slug radwordsprocessing-concepts-styles%})
Loading

libraries/radwordsprocessing/concepts/fields/fields.md

+1
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,4 @@ When exporting documents to DOCX format you can use the __IsDirty__ property of
261261
* [Document model]({%slug radwordsprocessing-model%})
262262
* [FieldCharacter]({%slug radwordsprocessing-model-fieldcharacter%})
263263
* [RadFlowDocumentEditor]({%slug radwordsprocessing-editing-radflowdocumenteditor%})
264+
* [Assigning Character Style to Fields in RadWordsProcessing]({%slug assigning-character-style-to-fields%})

libraries/radwordsprocessing/concepts/fields/page-field.md

+1
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,4 @@ After updating the field the result would be "Page 3 of 6" (check [Updating Fiel
5757
## See Also
5858

5959
* [RadFlowDocumentEditor]({%slug radwordsprocessing-editing-radflowdocumenteditor%})
60+
* [Assigning Character Style to Fields in RadWordsProcessing]({%slug assigning-character-style-to-fields%})

0 commit comments

Comments
 (0)