How to convert HTML to DOCX using C# in .Net Core | Framework
 
PRODUCTS   DOWNLOAD PURCHASE SUPPORT BLOG  COMPANY  
 
Key Features
HTML to RTF
>>HTML to DOCX
HTML to Text
Merge RTF
Download trial
Purchase
Online-demo
Documentation
Version History
Tutorials & Videos
How to convert multiple HTML files to a single RTF using C# .Net platform.

How to convert HTML to DOCX using C# in .Net Core | Framework

How to convert HTML to DOCX using C# in .Net Core | Framework

Introduction

    Imagine, that in 15 minutes after reading this text you will be able to provide API for your application to convert HTML documents into DOCX format using C#, VB.Net or other .Net language.
This solution can be used both in .Net Core and .Net Framework.
            SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf();

            string htmlString = @"Hello World!";
            h.OpenHtml(htmlBytes);
			byte[] docxBytes = h.ToDocx();
          
To start, install the latest version of «HTML to RTF .Net» component by any way:

Despite of the component names «HTML to RTF .Net», it can simultaneously convert to DOCX format completely supporting the Office Open XML specification. And we've decided to not change the component's name.

Not too shabby! Now you can operate with HtmlToRtf class which provides you by various methods and properties to convert HTML to DOCX:

API to convert HTML to DOCX in Visual Studio

Using the both overloaded methods OpenHtml() and ToDocx() you can transform HTML documents to DOCX format as MemoryStream, Files, URI or Bytes Array.



Nuget  Online Demo   Download, 7.0.8.16 (Jul 16th, 2018)



Have a Question?

You have a question, need a support or a code example: ask our support team at support@sautinsoft.com. We will help you quickly and with pleasure.

At the final point of this page we've prepared various examples to convert HTML to RTF.

Various examples to convert HTML to DOCX in C# and VB.Net

1. Simple conversion of HTML file to DOCX file in C#:

            SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf();
            string inputFile = @"d:\sample.html";
            string outputFile = Path.ChangeExtension(inputFile, ".docx");

            if (h.OpenHtml(inputFile))
            {
                bool ok = h.ToDocx(outputFile);
            }
2. Convert HTML Stream to DOCX Stream in C#:
            SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf();

            string inputFile = @"d:\utf-8.html";
            string outputFile = Path.ChangeExtension(inputFile, ".docx");

            // Specify the 'BaseURL' property that component can find the full path to images, like a: .
            h.BaseURL = Path.GetFullPath(inputFile);

            using (FileStream htmlFileStrem = new FileStream(inputFile, FileMode.Open))
            {
                if (h.OpenHtml(htmlFileStrem))
                {
                    using (MemoryStream docxMemoryStream = new MemoryStream())
                    {
                        bool ok = h.ToDocx(docxMemoryStream);
                    }
                }
            }
3. Convert HTML to DOCX in memory using VB.Net:
        Dim h As New SautinSoft.HtmlToRtf()

        Dim inputFile As String = "d:\pic.html"
        Dim outputFile As String = Path.ChangeExtension(inputFile, ".docx")

        ' Read our HTML file a bytes.
        Dim htmlBytes() As Byte = File.ReadAllBytes(inputFile)

        ' Specify the 'BaseURL' property that component can find the full path to images, like a: .
        h.BaseURL = Path.GetFullPath(inputFile)

        If h.OpenHtml(htmlBytes) Then
            Dim docxBytes() As Byte = h.ToDocx()

            ' Open the result for demonstation purposes.
            If docxBytes IsNot Nothing Then
                File.WriteAllBytes(outputFile, docxBytes)
                System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outputFile) With {.UseShellExecute = True})
            End If
        End If
4. Convert HTML to DOCX in C#; Add a custom page header from HTML, add footer from another RTF:
            SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf();

            string inputFile = @"d:\document.html";
            string outputFile = Path.ChangeExtension(inputFile, ".docx");

            // Set page header and footer.
            string headerFromHtml = File.ReadAllText(@"header.html");
            string footerFromRtf = File.ReadAllText(@"footer.rtf");

            // Add page header.
            h.PageStyle.PageHeader.Html(headerFromHtml);

            // Add extra space between header and page contents.
            h.PageStyle.PageHeader.MarginBottom.Mm(10);

            // Add page footer.
            h.PageStyle.PageFooter.Rtf(footerFromRtf);

            if (h.OpenHtml(inputFile))
            {
                bool ok = h.ToDocx(outputFile);
            }
5. Add page numbering during to HTML to DOCX conversion C#:
            SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf();

            string inputFile = @"..\..\sample.html";
            string outputFile = Path.ChangeExtension(inputFile, ".docx");

            // Add page numbering.
            // Let's set page numbers from 1st page
            h.PageStyle.PageNumbers.Appearance = SautinSoft.HtmlToRtf.ePageNumberingAppearence.PageNumFirst;

            // Lest's align page numbers by top-center
            h.PageStyle.PageNumbers.AlignV = SautinSoft.HtmlToRtf.eAlign.Top;
            h.PageStyle.PageNumbers.AlignH = SautinSoft.HtmlToRtf.eAlign.Center;

            // Lest's set page numbers format as "Page 1 of 20".
            h.PageStyle.PageNumbers.Format = "Page {page} of {numpages}";

            // Set page numbers font: Calibry, 19.
            h.PageStyle.PageNumbers.Font.Face = SautinSoft.HtmlToRtf.eFontFace.f_Calibri;
            h.PageStyle.PageNumbers.Font.Size = 19;


            if (h.OpenHtml(inputFile))
            {
                bool ok = h.ToDocx(outputFile);
            }

If anyone needs a code sample in C#, VB.Net, ASP.Net etc "How to convert HTML to DOCX", email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page). We'll help you certainly!


Requrements and Technical Information

     Requires only .Net Framework 4.0 and up or .Net Core 2.0 and up. Our product is compatible with all .Net languages and supports all Operating Systems where .Net Framework and .Net Core can be used.
Note, that «HTML to RTF .Net» is entirely written in managed C#, which makes it absolutely standalone and an independent library.

.Net Framework 4.0 and higher and .Net Core 2.0 and higher

.Net Framework 4.0, 4.5, 4.6.1 and up.

.Net Core 2.0 and up.



Reasonable prices

     The library offers developers high quality conversion at reasonably prices. The main point of interest is that «HTML to RTF .Net» is less expensive than all of its more or less popular counterparts. For example, a single license cost $339 with no additional fees or royalties!


Download

To see this functionality firsthand, download the freshest «HTML to RTF .Net» with code examples, 20.3 Mb.

 
 
Products Support Company Contact

  • Sweden, Stockholm
  • Mortviksvagen 68B 142 43 SKOGAS
  • Sales & Support:
  • Tel: +46 766922352
  • Email: sales@sautinsoft.com
  • Developers:
  • Email: support@sautinsoft.com
  •       
    Copyright © 2002 - 2018 SautinSoft (started from sautin.com). All rights reserved.