Skip to content

Added new kb article assigning-character-style-to-fields #544

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions knowledge-base/assigning-character-style-to-fields.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
---
title: Assigning Character Style to Fields in RadWordsProcessing
description: Learn how to apply a custom character style to fields in RadWordsProcessing for Document Processing and export the document to PDF.
type: how-to
page_title: Applying Custom Character Style to Fields in RadWordsProcessing
slug: assigning-character-style-to-fields
tags: wordsprocessing, document, processing, fields, character, style, pdf, export
res_type: kb
ticketid: 1686361
---

## Environment

| Version | Product | Author |
| ---- | ---- | ---- |
| 2025.1.205| RadWordsProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)|

## Description

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.

![Character Style in FieldInfo](images/character-style-in-fieldinfo.png)

## Solution

To assign a custom character style to fields in RadWordsProcessing and export the document to PDF, follow the steps below:

1. Create and define the [custom character styles]({%slug radwordsprocessing-concepts-styles%}).
2. Add the styles to the document's `StyleRepository`.
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.
4. Export the document to PDF format.

Here is a complete code example:

```csharp
RadFlowDocument document = new RadFlowDocument();
Section section = document.Sections.AddSection();
Footer footer = section.Footers.Add();

// Add a paragraph to the footer
Paragraph paragraph = footer.Blocks.AddParagraph();
paragraph.TextAlignment = Alignment.Right;

// Create a document editor
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
editor.MoveToParagraphStart(paragraph);

// Define custom character styles
Style characterStyle = new Style("Character Style", StyleType.Character);
characterStyle.Name = "Character Style";
characterStyle.CharacterProperties.FontSize.LocalValue = 12;
characterStyle.CharacterProperties.FontWeight.LocalValue = FontWeights.Normal;
characterStyle.CharacterProperties.ForegroundColor.LocalValue = new ThemableColor(Colors.Red);
document.StyleRepository.Add(characterStyle);

Style footerCharacterStyle = new Style("Footer Character Style", StyleType.Character);
footerCharacterStyle.Name = "Footer Character Style";
footerCharacterStyle.CharacterProperties.FontSize.LocalValue = 12;
footerCharacterStyle.CharacterProperties.FontWeight.LocalValue = FontWeights.Normal;
footerCharacterStyle.CharacterProperties.ForegroundColor.LocalValue = new ThemableColor(Colors.Aqua);
document.StyleRepository.Add(footerCharacterStyle);

// Insert text and apply styles
Run runFooterPage = editor.InsertText("Page ");
runFooterPage.StyleId = characterStyle.Id;

Telerik.Windows.Documents.Flow.Model.Fields.FieldInfo fieldInfo = editor.InsertField("PAGE", "");
fieldInfo.Start.Paragraph.StyleId = footerCharacterStyle.Id;

Run runFooterOf = editor.InsertText(" of ");
runFooterOf.StyleId = characterStyle.Id;

editor.InsertField("NUMPAGES", "");

// Update fields in the document
document.UpdateFields();

// Export the document to PDF
string outputFilePath = "sample.pdf";
File.Delete(outputFilePath);
Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider provider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();
using (Stream output = File.OpenWrite(outputFilePath))
{
provider.Export(document, output, TimeSpan.FromSeconds(10));
}
Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });
```

### Key Points:
- The `StyleId` property of `Run` objects allows you to associate a custom style.
- Fields consist of `Start` and `End` characters; you can apply styles to these elements or to the containing paragraph.
- Use the `UpdateFields` method to update the field content before export.

## See Also

- [Fields]({%slug radwordsprocessing-concepts-fields%})
- [Styles]({%slug radwordsprocessing-concepts-styles%})
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions libraries/radwordsprocessing/concepts/fields/fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,4 @@ When exporting documents to DOCX format you can use the __IsDirty__ property of
* [Document model]({%slug radwordsprocessing-model%})
* [FieldCharacter]({%slug radwordsprocessing-model-fieldcharacter%})
* [RadFlowDocumentEditor]({%slug radwordsprocessing-editing-radflowdocumenteditor%})
* [Assigning Character Style to Fields in RadWordsProcessing]({%slug assigning-character-style-to-fields%})
1 change: 1 addition & 0 deletions libraries/radwordsprocessing/concepts/fields/page-field.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ After updating the field the result would be "Page 3 of 6" (check [Updating Fiel
## See Also

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