|
| 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: radwordsprocessing, 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 | +I want to apply a custom character style to fields, such as page number and total pages, in RadWordsProcessing for Document Processing. I need the document to reflect the applied styles and be exported to PDF format. |
| 21 | + |
| 22 | +This knowledge base article also answers the following questions: |
| 23 | +- How to set a character style for a field in RadWordsProcessing? |
| 24 | +- How to format fields like PAGE and NUMPAGES with custom styles? |
| 25 | +- How to export styled fields to PDF using RadWordsProcessing? |
| 26 | + |
| 27 | +## Solution |
| 28 | + |
| 29 | +To assign a custom character style to fields in RadWordsProcessing and export the document to PDF, follow the steps below: |
| 30 | + |
| 31 | +1. Create and define the custom character styles. |
| 32 | +2. Add the styles to the document's `StyleRepository`. |
| 33 | +3. Specify the desired style for the `Run` objects in the field or the paragraph containing the field. |
| 34 | +4. Export the document to PDF format. |
| 35 | + |
| 36 | +Here is a complete code example: |
| 37 | + |
| 38 | +```csharp |
| 39 | +RadFlowDocument document = new RadFlowDocument(); |
| 40 | +Section section = document.Sections.AddSection(); |
| 41 | +Footer footer = section.Footers.Add(); |
| 42 | + |
| 43 | +// Add a paragraph to the footer |
| 44 | +Paragraph paragraph = footer.Blocks.AddParagraph(); |
| 45 | +paragraph.TextAlignment = Alignment.Right; |
| 46 | + |
| 47 | +// Create a document editor |
| 48 | +RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document); |
| 49 | +editor.MoveToParagraphStart(paragraph); |
| 50 | + |
| 51 | +// Define custom character styles |
| 52 | +Style characterStyle = new Style("Character Style", StyleType.Character); |
| 53 | +characterStyle.Name = "Character Style"; |
| 54 | +characterStyle.CharacterProperties.FontSize.LocalValue = 12; |
| 55 | +characterStyle.CharacterProperties.FontWeight.LocalValue = FontWeights.Normal; |
| 56 | +characterStyle.CharacterProperties.ForegroundColor.LocalValue = new ThemableColor(Colors.Red); |
| 57 | +document.StyleRepository.Add(characterStyle); |
| 58 | + |
| 59 | +Style footerCharacterStyle = new Style("Footer Character Style", StyleType.Character); |
| 60 | +footerCharacterStyle.Name = "Footer Character Style"; |
| 61 | +footerCharacterStyle.CharacterProperties.FontSize.LocalValue = 12; |
| 62 | +footerCharacterStyle.CharacterProperties.FontWeight.LocalValue = FontWeights.Normal; |
| 63 | +footerCharacterStyle.CharacterProperties.ForegroundColor.LocalValue = new ThemableColor(Colors.Aqua); |
| 64 | +document.StyleRepository.Add(footerCharacterStyle); |
| 65 | + |
| 66 | +// Insert text and apply styles |
| 67 | +Run runFooterPage = editor.InsertText("Page "); |
| 68 | +runFooterPage.StyleId = characterStyle.Id; |
| 69 | + |
| 70 | +Telerik.Windows.Documents.Flow.Model.Fields.FieldInfo fieldInfo = editor.InsertField("PAGE", ""); |
| 71 | +fieldInfo.Start.Paragraph.StyleId = footerCharacterStyle.Id; |
| 72 | + |
| 73 | +Run runFooterOf = editor.InsertText(" of "); |
| 74 | +runFooterOf.StyleId = characterStyle.Id; |
| 75 | + |
| 76 | +editor.InsertField("NUMPAGES", ""); |
| 77 | + |
| 78 | +// Update fields in the document |
| 79 | +document.UpdateFields(); |
| 80 | + |
| 81 | +// Export the document to PDF |
| 82 | +string outputFilePath = "sample.pdf"; |
| 83 | +File.Delete(outputFilePath); |
| 84 | +Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider provider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider(); |
| 85 | +using (Stream output = File.OpenWrite(outputFilePath)) |
| 86 | +{ |
| 87 | + provider.Export(document, output, TimeSpan.FromSeconds(10)); |
| 88 | +} |
| 89 | +Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true }); |
| 90 | +``` |
| 91 | + |
| 92 | +### Key Points: |
| 93 | +- The `StyleId` property of `Run` objects allows you to associate a custom style. |
| 94 | +- Fields consist of `Start` and `End` characters; you can apply styles to these elements or to the containing paragraph. |
| 95 | +- Use the `UpdateFields` method to update the field content before export. |
| 96 | + |
| 97 | +## See Also |
| 98 | + |
| 99 | +- [RadWordsProcessing Overview](https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/overview) |
| 100 | +- [RadWordsProcessing Fields Documentation](https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/features/fields) |
| 101 | +- [RadWordsProcessing PDF Export](https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/formats-and-conversion/pdf) |
0 commit comments