|
| 1 | +--- |
| 2 | +title: Save Content with Predefined Font |
| 3 | +page_title: Save Content with Predefined Font | RadEditor for ASP.NET AJAX Documentation |
| 4 | +description: Save RadEditor Content with Predefined Font |
| 5 | +slug: editor/how-to/save-content-with-predefined-font |
| 6 | +tags: save, content, with, predefined, font |
| 7 | +published: True |
| 8 | +position: 12 |
| 9 | +--- |
| 10 | + |
| 11 | +# Save Content with Predefined Font |
| 12 | + |
| 13 | +In this article you will see how to save the RadEditor's content with a predefined font. |
| 14 | + |
| 15 | +Generally, the editor's content is saved in a database without the external style sheets. This means only the inline styles will be preserved when the content is saved in a database. |
| 16 | + |
| 17 | +To define a default font for the editor content when it is submitted to the server you should use inline styles. You can use one of the following approaches: |
| 18 | + |
| 19 | +* [Define a wrapper element with a particular font for the content area](#define-a-wrapper-element-with-a-particular-font-for-the-content-area) |
| 20 | +* [Wrap the editor's content in an element with a desired font on submit](#wrap-the-editors-content-in-an-element-with-a-desired-font-on-submit) |
| 21 | +* [Capture the enter key press over the editor content area in order and fire an editor's format command](#capture-the-enter-key-press-over-the-editor-content-area-in-order-and-fire-an-editors-format-command) |
| 22 | +* [Define external stylesheets and use CSS inliner tool that will convert them to inline styles](#define-external-stylesheets-and-use-css-inliner-tool-that-will-convert-them-to-inline-styles) |
| 23 | + |
| 24 | +## Define a wrapper element with a particular font for the content area |
| 25 | + |
| 26 | +You can simply wrap the content of the editor inside an HTML element that has the desired inline font. |
| 27 | + |
| 28 | +>caption Example 1: RadEditor content is wrapped inside an element with a particular font. |
| 29 | +
|
| 30 | +````ASP.NET |
| 31 | +<telerik:RadEditor ID="RadEditor1" runat="server"> |
| 32 | + <Content> |
| 33 | + <div style="font-family:Arial;font-size:20px;"> |
| 34 | + some content in editor |
| 35 | + </div> |
| 36 | + </Content> |
| 37 | +</telerik:RadEditor> |
| 38 | +```` |
| 39 | + |
| 40 | +## Wrap the editor's content in an element with a desired font on submit |
| 41 | + |
| 42 | +You can use the editor's [get_html()]({%slug editor/client-side-programming/methods/get_html%}) and [set_html()]({%slug editor/client-side-programming/methods/set_html%}) methods in order to get and set the content inside an element with a particular font before submit. |
| 43 | + |
| 44 | +>caption Example 2: Wrap editor's content inside an element with a desired font on submit. |
| 45 | +
|
| 46 | +````ASP.NET |
| 47 | +<script> |
| 48 | + function submitContent(sender, args) { |
| 49 | + var editor = $find("RadEditor2"); |
| 50 | + editor.set_html("<div style='font-family:Arial;font-size:20px;'>" + editor.get_html(true) + "</div>"); |
| 51 | + } |
| 52 | +</script> |
| 53 | +<telerik:RadButton ID="RadButton1" runat="server" Text="Submit Editor Content" AutoPostBack="true" OnClientClicked="submitContent" /> |
| 54 | +<telerik:RadEditor ID="RadEditor2" runat="server"> |
| 55 | + <Content> |
| 56 | + some content in editor |
| 57 | + </Content> |
| 58 | +</telerik:RadEditor> |
| 59 | +```` |
| 60 | + |
| 61 | +## Capture the enter key press over the editor content area in order and fire an editor's format command |
| 62 | + |
| 63 | +You can fire a particular font command (e.g., `FontName` or `RealFontSize`). This can be done via the toolbar or automatically by attaching to the [enter key press of the content area]({%slug editor/client-side-programming/events/onclientload%}). |
| 64 | + |
| 65 | +>caption Example 3: Fire editor's font commands when pressing enter key in the content area. |
| 66 | +
|
| 67 | +````ASP.NET |
| 68 | +<script> |
| 69 | + function OnClientLoad(editor, args) { |
| 70 | + editor.attachEventHandler("onkeydown", function (e) { |
| 71 | + if (e.keyCode == 13) { |
| 72 | + editor.fire("FontName", { value: "Arial" }); |
| 73 | + editor.fire("RealFontSize", { value: "20px" }); |
| 74 | + } |
| 75 | + }); |
| 76 | + } |
| 77 | +</script> |
| 78 | +<telerik:RadEditor ID="RadEditor3" runat="server" OnClientLoad="OnClientLoad"> |
| 79 | + <Content> |
| 80 | + some content in editor |
| 81 | + </Content> |
| 82 | +</telerik:RadEditor> |
| 83 | +```` |
| 84 | + |
| 85 | +## Define external stylesheets and use CSS inliner tool that will convert them to inline styles |
| 86 | + |
| 87 | +You can use any external CSS Inliner Tool to convert the external stylesheets of the editor to inline styles (**Example 4**). |
| 88 | + |
| 89 | +1. Define the .css files via the CssFiles collection. |
| 90 | +1. Read the .css files and place that content inside the style tag of the editor content. |
| 91 | +1. Execute the CSS Inliner Tool over the modified content. |
| 92 | + |
| 93 | +>caption Example 4: Use an external CSS Inliner Tool to convert external stylesheets to inline styles. |
| 94 | +
|
| 95 | +````CSS |
| 96 | +/*ParagraphStylization.css*/ |
| 97 | +p { |
| 98 | + font-family: Arial; |
| 99 | + font-size: 20px; |
| 100 | +} |
| 101 | + |
| 102 | +```` |
| 103 | +````ASP.NET |
| 104 | +<telerik:RadEditor runat="server" ID="RadEditor1"> |
| 105 | + <CssFiles> |
| 106 | + <telerik:EditorCssFile Value="~/Styles/ParagraphStylization.css" /> |
| 107 | + </CssFiles> |
| 108 | + <Content> |
| 109 | + <p> |
| 110 | + some content in paragraph |
| 111 | + </p> |
| 112 | + </Content> |
| 113 | +</telerik:RadEditor> |
| 114 | +```` |
| 115 | +````C# |
| 116 | +protected void Button1_Click(object sender, EventArgs e) |
| 117 | +{ |
| 118 | + StringBuilder css = new StringBuilder(); |
| 119 | + foreach (var item in RadEditor1.CssFiles) |
| 120 | + { |
| 121 | + EditorCssFile cssFile = (EditorCssFile)item; |
| 122 | + string path = Server.MapPath(cssFile.Value); |
| 123 | + css.Append(File.ReadAllText(path)); |
| 124 | + } |
| 125 | + |
| 126 | + string content = RadEditor1.Content; |
| 127 | + |
| 128 | + string rawContent = "<html><head><style>" + css.ToString() + "</style></head><body>" + content + "</body></html>"; |
| 129 | + |
| 130 | + var contentInlineStyles = MyCSSInlinerTool.MoveCssInline(rawContent, true); //Where MyCSSInlinerTool is an external tool that that converts stylesheets to inline styles. |
| 131 | +} |
| 132 | +```` |
| 133 | +````VB |
| 134 | +Protected Sub Button1_Click(sender As Object, e As EventArgs) |
| 135 | +Dim css As New StringBuilder() |
| 136 | +For Each item As var In RadEditor1.CssFiles |
| 137 | +Dim cssFile As EditorCssFile = DirectCast(item, EditorCssFile) |
| 138 | +Dim path As String = Server.MapPath(cssFile.Value) |
| 139 | +css.Append(File.ReadAllText(path)) |
| 140 | +Next |
| 141 | + |
| 142 | +Dim content As String = RadEditor1.Content |
| 143 | + |
| 144 | +Dim rawContent As String = "<html><head><style>" & css.ToString() & "</style></head><body>" & content & "</body></html>" |
| 145 | + |
| 146 | +Dim contentInlineStyles = MyCSSInlinerTool.MoveCssInline(rawContent, True) 'Where MyCSSInlinerTool is an external tool that that converts stylesheets to inline styles. |
| 147 | +End Sub |
| 148 | +```` |
| 149 | + |
| 150 | +## See Also |
| 151 | + |
| 152 | +* [RadEditor OnClientLoad event]({%slug editor/client-side-programming/events/onclientload%}) |
| 153 | +* [RadEditor get_html() method]({%slug editor/client-side-programming/methods/get_html%}) |
| 154 | +* [RadEditor set_html() method]({%slug editor/client-side-programming/methods/set_html%}) |
| 155 | +* [PreMailer.Net](https://github.com/milkshakesoftware/PreMailer.Net) |
0 commit comments