Example E3449
Visible to All Users

Reporting for WinForms - How to Change Formatting in the XRRichText Control

This example uses the non-visual RichEditDocumentServer component to edit and format rich text content of the XRRichText control.

Original Content

The report preview with the XRRichText control that displays the original RTF content is shown below:

Report with XRRichText Control Before Formatting

Modified Content

The RichEditDocumentServer component applies default formatting, removes whitespaces at the end of the text, appends formatted text, and assigns the resulting RTF text to the XRRichText.Rtf property. The resulting report is shown in the following image:

Report with XRRichText Control After Formatting

Files to Review

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

Form1.cs(vb)
C#
using System; using System.Windows.Forms; using DevExpress.XtraReports.UI; namespace ReportingXRRichTextFormatSample { public partial class Form1 : Form { private XtraReport1 report = new XtraReport1(); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { report.OverrideRtfFormatting = checkBox1.Checked; report.CreateDocument(); report.ShowPreviewDialog(); } } }
XtraReport1.cs(vb)
C#
using System.Drawing; using DevExpress.XtraReports.UI; using DevExpress.XtraRichEdit; using DevExpress.XtraRichEdit.API.Native; namespace ReportingXRRichTextFormatSample { public partial class XtraReport1 : DevExpress.XtraReports.UI.XtraReport { private RichEditDocumentServer richEditDocumentServer; public bool OverrideRtfFormatting { get; set; } public XtraReport1() { InitializeComponent(); } private void XtraReport1_BeforePrint(object sender, System.ComponentModel.CancelEventArgs e) { xrRichText1.ExpressionBindings.Clear(); if (OverrideRtfFormatting) { if (richEditDocumentServer == null) richEditDocumentServer = new RichEditDocumentServer(); } else xrRichText1.ExpressionBindings.Add(new ExpressionBinding() { PropertyName = "Rtf", Expression = "[RtfContent]", EventName = "BeforePrint" }); } private void xrRichText1_BeforePrint(object sender, System.ComponentModel.CancelEventArgs e) { if (!OverrideRtfFormatting) return; richEditDocumentServer.RtfText = GetCurrentColumnValue("RtfContent").ToString(); ApplyRTFModification(richEditDocumentServer); xrRichText1.Rtf = richEditDocumentServer.RtfText; } private void ApplyRTFModification(RichEditDocumentServer server) { // Apply default formatting server.Document.DefaultCharacterProperties.FontName = "Arial"; server.Document.DefaultCharacterProperties.FontSize = 9; server.Document.DefaultCharacterProperties.ForeColor = Color.FromArgb(120, 120, 120); server.Document.DefaultParagraphProperties.Alignment = ParagraphAlignment.Center; // Remove whitespaces from the end of RTF content DocumentRange[] dots = server.Document.FindAll(".", SearchOptions.None); DocumentPosition lastDot = dots[dots.Length - 1].End; server.Document.Delete(server.Document.CreateRange(lastDot, server.Document.Range.End.ToInt() - lastDot.ToInt())); // Append formatted word DocumentRange range = server.Document.InsertText(server.Document.Range.End, " [Approved]"); CharacterProperties cp = server.Document.BeginUpdateCharacters(range); cp.FontName = "Courier New"; cp.FontSize = 10; cp.ForeColor = Color.Red; cp.Underline = UnderlineType.Single; cp.UnderlineColor = Color.Red; server.Document.EndUpdateCharacters(cp); } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.