How to save a document in HTML format C# and VB.Net

  1. Save to a file:
    
                // The file format will be detected automatically from the file extension: ".html".
                dc.Save(@"d:\Book.html");
    

    To not rely on the file extension and guarantee that the file contents is really HTML, you may specify HtmlFixedSaveOptions or HtmlFlowingSaveOptions as 2nd parameter.

    
    dc.Save(@"d:\Book.html", new HtmlFixedSaveOptions());
    

    In HtmlFixedSaveOptions or HtmlFlowingSaveOptions you can also set various options which affects to the saving process.

    In HTML fixed mode the component renders HTML markup using absolutely positioned elements.

    In HTML flowing mode the component renders HTML as a flow document without any absolute positioning.

                dc.Save(@"d:\Book.html", new HtmlFixedSaveOptions()
                {
                    Version = HtmlVersion.Html5,
                    CssExportMode = CssExportMode.Inline
                });
    
  2. Save to a Stream:
                // Let's save our document to a MemoryStream.
                using (MemoryStream ms = new MemoryStream())
                {
                    // HTML Fixed.
                    dc.Save(ms, new HtmlFixedSaveOptions());
                 
                    // Or HTML flowing.
                    dc.Save(ms, new HtmlFlowingSaveOptions());
                }
    
 

Complete code

using System.IO;
using SautinSoft.Document;

namespace Example
{
    class Program
    {
        
        static void Main(string[] args)
        {
            SaveToHtmlFile();
            SaveToHtmlStream();
        }

        static void SaveToHtmlFile()
        {
            // Assume we already have a document 'dc'.
            DocumentCore dc = new DocumentCore();
            dc.Content.End.Insert("Hey Guys and Girls!");

            string fileHtmlFixed = @"Result-Fixed.html";
            string fileHtmlFlowing = @"Result-Flowing.html";

            // Save to HTML file: HtmlFixed.
            dc.Save(fileHtmlFixed, new HtmlFixedSaveOptions()
            {
                Version = HtmlVersion.Html5,
                CssExportMode = CssExportMode.Inline
            });

            // Save to HTML file: HtmlFlowing.
            dc.Save(fileHtmlFlowing, new HtmlFlowingSaveOptions()
            {
                Version = HtmlVersion.Html5,
                CssExportMode = CssExportMode.Inline,
                ListExportMode = HtmlListExportMode.ByHtmlTags
            });
        }

        static void SaveToHtmlStream()
        {
            // There variables are necessary only for demonstration purposes.
            byte[] fileData = null;
            string fileHtmlFixed = @"Result-Fixed.html";
            string fileHtmlFlowing = @"Result-Flowing.html";

            // Assume we already have a document 'dc'.
            DocumentCore dc = new DocumentCore();
            dc.Content.End.Insert("Hey Guys and Girls!");

            // Let's save our document to a MemoryStream.
            using (MemoryStream ms = new MemoryStream())
            {
                // HTML Fixed.
                dc.Save(ms, new HtmlFixedSaveOptions());
                fileData = ms.ToArray();

                File.WriteAllBytes(fileHtmlFixed, fileData);

                // Or HTML flowing.
                dc.Save(ms, new HtmlFlowingSaveOptions());
                fileData = ms.ToArray();

                File.WriteAllBytes(fileHtmlFlowing, fileData);
            }
        }
    }
}

Download.

        
            Imports System.IO
Imports SautinSoft.Document

Module ExampleVB

    Sub Main()
        SaveToHtmlFile()
        SaveToHtmlStream()
    End Sub

    Public Sub SaveToHtmlFile()

        ' Assume we already have a document 'dc'.
        Dim dc As New DocumentCore()
        dc.Content.End.Insert("Hey Guys and Girls!")

        Dim fileHtmlFixed As String = "Result-Fixed.html"
        Dim fileHtmlFlowing As String = "Result-Flowing.html"

        ' Save to HTML file: HtmlFixed.
        dc.Save(fileHtmlFixed, New HtmlFixedSaveOptions() With {
                .Version = HtmlVersion.Html5,
                .CssExportMode = CssExportMode.Inline
            })

        ' Save to HTML file: HtmlFlowing.
        dc.Save(fileHtmlFlowing, New HtmlFlowingSaveOptions() With {
                .Version = HtmlVersion.Html5,
                .CssExportMode = CssExportMode.Inline,
                .ListExportMode = HtmlListExportMode.ByHtmlTags
            })
    End Sub

    Public Sub SaveToHtmlStream()

        ' There variables are necessary only for demonstration purposes.
        Dim fileData() As Byte = Nothing
        Dim fileHtmlFixed As String = "Result-Fixed.html"
        Dim fileHtmlFlowing As String = "Result-Flowing.html"

        ' Assume we already have a document 'dc'.
        Dim dc As New DocumentCore()
        dc.Content.End.Insert("Hey Guys and Girls!")

        ' Let's save our document to a MemoryStream.
        Using ms As New MemoryStream()

            ' HTML Fixed.
            dc.Save(ms, New HtmlFixedSaveOptions())
            fileData = ms.ToArray()

            File.WriteAllBytes(fileHtmlFixed, fileData)

            ' Or HTML flowing.
            dc.Save(ms, New HtmlFlowingSaveOptions())
            fileData = ms.ToArray()

            File.WriteAllBytes(fileHtmlFlowing, fileData)
        End Using
    End Sub
End Module

Download.

© SautinSoft 2019