I've been working with the RadRichTextEditor control in my application. It imports and exports simple html -- as I only need things like bold, italics, centering. However, even if I set the FontFamily of the RadRichTextEditor and even the RichTextBoxElement within to Arial, it doesn't respect that when I paste in plain text from an application like Notepad. It changes the text to Verdana with a font size of 12.
If I copy and paste text from Word, I am fine. It keeps whatever font is was in Word.
Additionally, if I use the keyboard and hit shift + enter to give me a single line break (</ br>), It wraps that html with a style tag with the Verdana 12 font. I don't mind the style tag, but it would be nice if it respected the current font where the cursor is.
Any ideas?
Here is my font properties code (which is overkill, but I'm trying everything to fix this).
public void SetDefaultFontPropertiesToEditor(RadRichTextEditor editor) { editor.DocumentInheritsDefaultStyleSettings = true; editor.RichTextBoxElement.ChangeFontFamily(new Telerik.WinControls.RichTextEditor.UI.FontFamily("Arial")); editor.RichTextBoxElement.ChangeFontSize(Unit.PointToDip(10)); editor.RichTextBoxElement.ChangeFontStyle(Telerik.WinControls.RichTextEditor.UI.FontStyles.Normal); editor.RichTextBoxElement.ChangeFontWeight(Telerik.WinControls.RichTextEditor.UI.FontWeights.Normal); editor.ChangeFontFamily(new Telerik.WinControls.RichTextEditor.UI.FontFamily("Arial")); editor.ChangeFontSize(Unit.PointToDip(10)); editor.ChangeFontStyle(Telerik.WinControls.RichTextEditor.UI.FontStyles.Normal); editor.ChangeFontWeight(Telerik.WinControls.RichTextEditor.UI.FontWeights.Normal); editor.RichTextBoxElement.Document.LineSpacingType = LineSpacingType.AtLeast; editor.RichTextBoxElement.Document.LineSpacing = 0; editor.RichTextBoxElement.Document.ParagraphDefaultSpacingAfter = 0; editor.RichTextBoxElement.Document.ParagraphDefaultSpacingBefore = 10; }11 Answers, 1 is accepted
Thank you for contacting us.
To change the pasted text font you can use CommandExecuting event and paste the text manually:
void radRichTextEditor1_CommandExecuting(object sender, CommandExecutingEventArgs e){ if (e.Command is PasteCommand) { e.Cancel = true; PasteNewText(); }}public void PasteNewText(){ DocumentFragment clipboardDocument = null; string clipboardText = null; bool clipboardContainsData = false; if (ClipboardEx.ContainsDocument(null)) { clipboardDocument = ClipboardEx.GetDocument(); clipboardContainsData = true; } else if (ClipboardEx.ContainsText(null)) { clipboardText = ClipboardEx.GetText(null); clipboardContainsData = true; } if (!clipboardContainsData) { return; } if (clipboardDocument != null) { RadDocument doc = new RadDocument(); RadDocumentEditor editor = new RadDocumentEditor(doc); editor.InsertFragment(clipboardDocument); doc.Selection.SelectAll(); editor.ChangeFontFamily(new Telerik.WinControls.RichTextEditor.UI.FontFamily("Segoe Script")); DocumentFragment fragmentFromSelection = doc.Selection.CopySelectedDocumentElements(); this.radRichTextEditor1.RichTextBoxElement.ActiveDocumentEditor.InsertFragment(fragmentFromSelection); } else if (!string.IsNullOrEmpty(clipboardText)) { this.radRichTextEditor1.RichTextBoxElement.ActiveDocumentEditor.Insert(clipboardText); }}Please let me know if there is something else I can help you with.
Dimitar
Telerik
See What's Next in App Development. Register for TelerikNEXT.
I have the same problem. Copy some Calibri 11 point text from MS word - it pastes as Verdana 12 point. Copy Kristen ITC 11 point - it pastes as Kristen ITC 12 point. Copy plain text - it always pastes as Verdana 12 point, no matter what the current font of the control is set to.
If I copy rich text, I expect the paste to be in the same font and size as I copied. For plain text I expect it to paste in the font that is currently set at the insertion point.
Thank you for writing.
To achieve the desired functionality you can change the bellow code to check if the clipboard text has styles or it is just a plain text:
public void PasteNewText(){ DocumentFragment clipboardDocument = null; string clipboardText = null; bool clipboardContainsData = false; if (ClipboardEx.ContainsDocument(null)) { clipboardDocument = ClipboardEx.GetDocument(); clipboardContainsData = true; } else if (ClipboardEx.ContainsText(null)) { clipboardText = ClipboardEx.GetText(null); clipboardContainsData = true; } if (!clipboardContainsData) { return; } if (clipboardDocument != null) { RadDocument doc = new RadDocument(); RadDocumentEditor editor = new RadDocumentEditor(doc); editor.InsertFragment(clipboardDocument); doc.Selection.SelectAll(); Span span = radRichTextEditor1.RichTextBoxElement.CurrentSpanStyle; if ((Clipboard.ContainsText(TextDataFormat.Text) || Clipboard.ContainsText(TextDataFormat.UnicodeText)) && !(Clipboard.ContainsText(TextDataFormat.Rtf) || Clipboard.ContainsText(TextDataFormat.Html))) { editor.ChangeSpanStyle(span.Style); } DocumentFragment fragmentFromSelection = doc.Selection.CopySelectedDocumentElements(); this.radRichTextEditor1.RichTextBoxElement.ActiveDocumentEditor.InsertFragment(fragmentFromSelection); } else if (!string.IsNullOrEmpty(clipboardText)) { this.radRichTextEditor1.RichTextBoxElement.ActiveDocumentEditor.Insert(clipboardText); }}I hope this information is useful. Let me know if you need further assistance.
Dimitar
Telerik
See What's Next in App Development. Register for TelerikNEXT.
Thank you for writing back.
On my side the styled text is pasted correctly and the font is preserved. I would recommend you to test this with different font family and style. The easiest way to is to open the First Look example for the RadRichTextEditor (from our demo application) and paste some code from Visual Studio. Could you please confirm that pasting code still does not work as expected?
I am looking forward to your reply.
Regards,
Dimitar
Telerik
See What's Next in App Development. Register for TelerikNEXT.
Thank you for writing back.
Yes there is a separate case where this is not working correctly. I want to mention that a workaround is available in the issue description.
Do not hesitate to contact us if you have other questions.
Dimitar
Telerik
Thank you for contacting us.
You can see how to change the default font settings in the following article: Frequently Asked Questions. The font settings cannot be changed at design time they should be changed with the corresponding methods. Additionally, this should be done before any content is added to the document. If the document already contains something you should select the entire content first, if nothing is selected nothing will change. Detailed information about the selection is available here: Selection.
I hope this will be useful.
Regards,
Dimitar
Telerik
Hello Telerik,
your control changed also the format to Verdana when import rtf. But only in the first line. And only the first line is Arial. Is the first line with any other font formated, its all ok.
Here the code:
string rtfText = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1031{\fonttbl{\f0\fnil\fcharset0 Arial;}{\f1\fnil\fcharset0 Tahoma;}{\f2\fnil\fcharset0 Times New Roman;}{\f3\fnil\fcharset0 Arial Unicode MS;}{\f4\fnil\fcharset0 Calibri;}}
{\colortbl ;\red0\green0\blue0;}
\viewkind4\uc1\pard\tx300\tx600\tx900\tx1200\tx1500\tx1800\tx2100\tx2400\tx2700\tx3000\tx3300\tx3600\tx3900\tx4200\tx4500\cf1\b\fs32 Arial\f1\par
Tahoma\par
\f2 Times New Roman\par
\f0 Arial\f2\par
\f3 Arial Unicode MS\par
\f4 Calibri\b0\f0\fs20\par
}";
RtfFormatProvider rtf = new RtfFormatProvider();
this.radRichTextEditor1.Document = rtf.Import(rtfText);
this.richTextBox1.Rtf = rtfText;
In the screenshot you can see, left the standard .net rtf control and rigth the Telerik richtexteditor.
Thank you for writing.
I was able to reproduce the observed issue. I have logged it in our Feedback Portal. You can track the item for status changes and add your vote for it here.
Unfortunately due to the nature of the issue I cannot suggest a workaround for it. If it is suitable for your case you can select the text and change its font.
Your Telerik Points have been updated for this report.
Should you have any other questions do not hesitate to ask.
Regards,
Dimitar
Telerik
