Word Editor in ASP.NET

HTML can be easily loaded into specific document location with GemBox.Document.

Following example shows how to load HTML text into bookmark.

Visual Studio solution that contains the code can be downloaded from: HtmlEditorSample.zip.

Screenshot

HTML Editor Screenshot

See the full code below.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="HtmlEditorSampleCs.Default" %>

<%@ Register Assembly="CKEditor.NET" Namespace="CKEditor.NET" TagPrefix="CKEditor" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>HTML Editor Sample</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <CKEditor:CKEditorControl ID="htmlEditor" runat="server">
                Hello &lt;b&gt;World!&lt;/b&gt;
            </CKEditor:CKEditorControl>
        </div>
        <div>
            <asp:Button ID="exportButton" runat="server" Text="Export" OnClick="OnExportButtonClicked" />
            <asp:Literal Text=" to " runat="server" />
            <asp:DropDownList ID="outputFormatList" runat="server">
                <asp:ListItem Value="docx">DOCX</asp:ListItem>
                <asp:ListItem Value="html">HTML</asp:ListItem>
                <asp:ListItem Value="mht">MHTML</asp:ListItem>
                <asp:ListItem Value="rtf">RTF</asp:ListItem>
                <asp:ListItem Value="txt">TXT</asp:ListItem>
                <asp:ListItem Selected="True" Value="pdf">PDF</asp:ListItem>
                <asp:ListItem Value="xps">XPS</asp:ListItem>
                <asp:ListItem Value="png">PNG</asp:ListItem>
                <asp:ListItem Value="jpg">JPEG</asp:ListItem>
                <asp:ListItem Value="gif">GIF</asp:ListItem>
                <asp:ListItem Value="bmp">BMP</asp:ListItem>
                <asp:ListItem Value="tif">TIFF</asp:ListItem>
                <asp:ListItem Value="wdp">WMP</asp:ListItem>
            </asp:DropDownList>
        </div>
    </form>
</body>
</html>
1using System;
2using System.IO;
3using GemBox.Document;
4
5namespace HtmlEditorSampleCs
6{
7    public partial class Default : System.Web.UI.Page
8    {
9        protected void Page_Load(object sender, EventArgs e)
10        {
11            if (!this.IsPostBack)
12            {
13                ComponentInfo.SetLicense("FREE-LIMITED-KEY");
14                ComponentInfo.FreeLimitReached += (s1, e1) => e1.FreeLimitReachedAction = FreeLimitReachedAction.ContinueAsTrial;
15            }
16        }
17
18        protected void OnExportButtonClicked(object sender, EventArgs e)
19        {
20            var templatePath = Path.Combine((string)AppDomain.CurrentDomain.GetData("DataDirectory"), "TemplateDocument.docx");
21
22            DocumentModel document;
23            using (var stream = File.OpenRead(templatePath))
24                document = DocumentModel.Load(stream, LoadOptions.DocxDefault);
25
26            document.Bookmarks["HtmlContent"].GetContent(true).LoadText(this.htmlEditor.Text, LoadOptions.HtmlDefault);
27
28            var fileName = Path.ChangeExtension("Document", this.outputFormatList.SelectedValue);
29
30            document.Save(this.Response, fileName);
31        }
32    }
33}
1Imports System.IO
2Imports GemBox.Document
3
4Public Class _Default
5    Inherits System.Web.UI.Page
6
7    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
8
9        If Not Me.IsPostBack Then
10            ComponentInfo.SetLicense("FREE-LIMITED-KEY")
11        End If
12    End Sub
13
14    Protected Sub OnExportButtonClicked(sender As Object, e As EventArgs)
15
16        Dim templatePath = Path.Combine(DirectCast(AppDomain.CurrentDomain.GetData("DataDirectory"), String), "TemplateDocument.docx")
17
18        Dim document As DocumentModel
19        Using stream = File.OpenRead(templatePath)
20            document = DocumentModel.Load(stream, LoadOptions.DocxDefault)
21        End Using
22
23        document.Bookmarks("HtmlContent").GetContent(True).LoadText(Me.htmlEditor.Text, LoadOptions.HtmlDefault)
24
25        Dim fileName = Path.ChangeExtension("Document", Me.outputFormatList.SelectedValue)
26
27        document.Save(Me.Response, fileName)
28    End Sub
29
30End Class

Check next sample or find out more about GemBox.Document and GemBox Software.