New to Telerik UI for WinFormsStart a free 30-day trial

How to auto-fit RadRichTextEditor

Updated over 6 months ago

Environment

Product VersionProductAuthor
2018.3.1016RadRichTextEditor for WinFormsDesislava Yordanova

Description

This article aims to demonstrate a sample approach how to auto-fit the document in RadRichTextEditor so that the editor shows always the whole page.

Solution

It can be achieved by adjusting the scale factor programmatically in order to fit the page. Thus, by calculating a ratio between the size of the editor and the actual page size, you can use this ratio to set the ScaleFactor property:

Auto-Fit RadRichTextEditor

C#
        public RadForm1()
        {
            InitializeComponent(); 

            this.radRichTextEditor1.SizeChanged += radRichTextEditor1_SizeChanged;  
        }

        private void SacleEditor()
        {
            float ratio = (this.radRichTextEditor1.Size.Width - this.radRichTextEditor1.RichTextBoxElement.VerticalScrollBar.Size.Width - 5) 
                / (float)this.radRichTextEditor1.Document.SectionDefaultPageSize.Width;
            this.radRichTextEditor1.ScaleFactor = new System.Drawing.SizeF(ratio, ratio);
        }
 
        private void radRichTextEditor1_SizeChanged(object sender, EventArgs e)
        {
            SacleEditor();
        }

        private void radButton1_Click(object sender, EventArgs e)
        {
            Telerik.WinControls.RichTextEditor.UI.DocumentPrintLayoutPresenter presenter = 
                radRichTextEditor1.RichTextBoxElement.ActiveEditorPresenter as DocumentPrintLayoutPresenter;
            presenter.PagePresentersMargin = new Telerik.WinForms.Documents.Model.SizeF(0, 0);
 
            this.radRichTextEditor1.SizeChanged += radRichTextEditor1_SizeChanged;
            SacleEditor();
        }
 

Now, you can resize the form and the document will fit the width:

auto-fit-richtexteditor 001

See Also