Rich Text Editor / Loading and Saving Documents / Web Pages (HTML)
In This Topic
    Web Pages (HTML)
    In This Topic

    NOV Rich Text view lets you easily import and export documents from and to HTML. You can even load a web page directly in the NOV rich text view, i.e. the NOV text component can act as a mini web browser that includes support for many HTML 5 and CSS 3 features.

     Loading from HTML

    To import an HTML document from a file, you can use the Load methods as shown in the Loading Documents topic.

    Load HTML from local file
    Copy Code
    richTextView.LoadFromLocalFile(@"C:\MyDocument.html");
    

    If you want to load a page directly from an URL, you can use the LoadFromUri method. Note that loading a web page requires browser-like capabilities like full support for CSS and JavaScript and while the NOV rich text view comoes with good CSS support, it doesn't support JavaScript, so some pages might not load correctly.

    Load a web page from an URL
    Copy Code
    richTextView.LoadFromUri(new NUri("https://en.wikipedia.org/wiki/EPUB"));
    

    If you want to be notified when the web page has loaded, you should subscribe to the DocumentLoaded event.

    You can also load an HTML document from a string if you convert that string to a byte array first and place it in a memory stream as the following code snippet demonstrates:

    Load HTML from string
    Copy Code
    string html = "<p>This is a paragraph with <b>some bold text</b> and <span style='color:red'>some red text</span>.</p>";
    using (MemoryStream stream = new MemoryStream(NEncoding.UTF8.GetBytes(html)))
    {
        richTextView.LoadFromStream(stream, NTextFormat.Html);
    }
    
     Saving to HTML

    To export a document to HTML, you can use the Save methods as shown in the Saving Documents topic.

    Save to HTML to a local file
    Copy Code
    richTextView.SaveToLocalFile(@"C:\MyDocument.html");
    

    You can pass an NHtmlSaveSettings instance to the Save method if you want to configure the HTML export process. The NHtmlSaveSettings class exposes the following properties:

    • DocumentType - specifies the document type of the generated HTML document. By default set to HTML 5 doc type.
    • EmbedImages - specifies whether to embed images as Base64 strings in the generated HTML documents or not. By default set to true. If this property is set to false, images will be generated in the same folder as the generated HTML files. Note that for some platforms like Silverlight this is not possible due to security reasons.
    • InlineStyles - determines whether to inline the CSS styles in "style" attributes or to create and use CSS classes. By default set to false, which means that the HTML exporter will create and use CSS classes. By default set to false.
    • MinifyHtml - determines whether to minify the resulting HTML by not adding any new lines or tabulations, i.e. whether to place all HTML and CSS code on a single line. By default set to true.

    The following code snippet demonstrates how to save an HTML document to a file with all CSS styles inlined to style attributes:

    Save to HTML with inlined CSS styles
    Copy Code
    NHtmlSaveSettings saveSettings = new NHtmlSaveSettings();
    saveSettings.InlineStyles = true;
    richTextView.SaveToLocalFile(@"C:\HtmlDocument.html", NTextFormat.Html, saveSettings);
    

    To save a rich text document to an HTML string you should first save it to a stream and then convert the stream to a string. The following is a code example:

    Save to an HTML string
    Copy Code
    using (MemoryStream stream = new MemoryStream())
    {
        m_RichText.SaveToStream(stream, NTextFormat.Html);
        string html = NEncoding.UTF8.GetString(stream.ToArray());
    }
    
    See Also