|
| 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 | + |
| 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%}) |
0 commit comments