How to write a function to convert HTML to RTF in C# and VB.Net for 15 minutes?

How to write a function to convert HTML to RTF in C# and VB.Net for 15 minutes?

HTML to RTF .Net

.Net assembly to convert HTML to Text, RTF and DOCX in .Net and C#.
html-to-rtf-scheme

HTML to RTF .Net

How to write a function to convert HTML to RTF in C# and VB.Net for 15 minutes?
html-to-rtf-scheme

Introduction

Imagine, that in 15 minutes after reading this text you will be able to provide API for your application to convert HTML page into RTF documents, using C# or VB.Net.

            SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf();
                    string html = @"<p>Hello World!</p>";
                    string rtf = String.Empty;
                    if (h.OpenHtml(html))
                    {
                    rtf = h.ToRtf();
                    }
                

Now you can operate with HtmlToRtf class which provides you by various methods and properties to convert HTML as MemoryStream, String, Files, URI and so forth:

How looks HtmlToRtf .Net class

Download

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

Limitations

HTML to RTF .Net The limitations of the free version are: The trial notice "Created by unlicensed version of HTML to RTF .Net" and the random addition of the word "TRIAL".


Five examples to convert HTML to RTF in C# and VB.Net

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

            SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf();

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

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

                // Open the result for demonstration purposes.
                if (ok)
                    System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outputFile)
                    { UseShellExecute = true });
            }
2. Convert HTML to RTF in memory using C#; Set page size to Letter/Landscape, add page numbers:
            SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf();

            // Let's set some page options.
            h.PageStyle.PageSize.Letter();
            h.PageStyle.PageOrientation.Landscape();

            h.PageStyle.PageNumbers.Format = "Page {page} of {numpages}";
            h.PageStyle.PageNumbers.AlignH = SautinSoft.HtmlToRtf.eAlign.Center;
            h.PageStyle.PageNumbers.AlignV = SautinSoft.HtmlToRtf.eAlign.Bottom;

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

            // Specify the 'BaseURL' property that component can find the full path to images, like a: <img src="..\pict.png" and
            // to external css, like a:  <link rel="stylesheet" href="/css/style.css" >.
            h.BaseURL = Path.GetFullPath(inputFile);

            using (FileStream htmlFileStream = new FileStream(inputFile, FileMode.Open))
            {
                if (h.OpenHtml(htmlFileStream))
                {
                    using (MemoryStream rtfMemoryStream = new MemoryStream())
                    {
                        bool ok = h.ToRtf(rtfMemoryStream);

                        // Open the result for demonstration purposes.
                        if (ok)
                        {
                            File.WriteAllBytes(outputFile, rtfMemoryStream.ToArray());
                            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outputFile)
                            { UseShellExecute = true });
                        }
                    }
                }
            }
3. Convert HTML to RTF in memory using VB.Net; Set page size to Letter/Landscape, add page numbers:
        Dim h As New SautinSoft.HtmlToRtf()

        Dim inputFile As String = "d:\utf-8.html"
        Dim outputFile As String = Path.ChangeExtension(inputFile, ".rtf")

        ' Let's set some page options.
        h.PageStyle.PageSize.Letter()
        h.PageStyle.PageOrientation.Landscape()

        h.PageStyle.PageNumbers.Format = "Page {page} of {numpages}"
        h.PageStyle.PageNumbers.AlignH = SautinSoft.HtmlToRtf.eAlign.Center
        h.PageStyle.PageNumbers.AlignV = SautinSoft.HtmlToRtf.eAlign.Bottom

        ' Specify the 'BaseURL' property that component can find the full path to images, like a: <img src="..\pict.png" and
        ' to external css, like a:  <link rel="stylesheet" href="/css/style.css">.
        h.BaseURL = Path.GetFullPath(inputFile)

        Using htmlFileStream As New FileStream(inputFile, FileMode.Open)
            If h.OpenHtml(htmlFileStream) Then
                Using rtfMemoryStream As New MemoryStream()
                    Dim ok As Boolean = h.ToRtf(rtfMemoryStream)
                    ' Open the result for demonstration purposes.
                    If ok Then
                        File.WriteAllBytes(outputFile, rtfMemoryStream.ToArray())
                        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outputFile)
                            With { .UseShellExecute = True})
                    End If
                End Using
            End If
        End Using
4. Convert HTML to RTF in C#; Add a custom page header from HTML, add footer from another RTF:
            SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf();
            string inputFile = @"d:\sample.html";
            string outputFile = Path.ChangeExtension(inputFile, ".rtf");

            // Add a header from HTML string.
            h.PageStyle.PageHeader.Html("This is a bold italic line.");

            // Add a footer from RTF file.
            h.PageStyle.PageFooter.FromRtfFile(@"d:\footer.rtf");

            // Make the conversion of HTML to RTF.
            if (h.OpenHtml(inputFile))
            {
                bool ok = h.ToRtf(outputFile);

                // Open the result for demonstration purposes.
                if (ok)
                    System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outputFile)
                    { UseShellExecute = true });
            }
5. Convert HTML to RTF and extract all images from HTML (local, remote, even embedded within base-64) in C#:
            SautinSoft.HtmlToRtf h = new HtmlToRtf();
            string htmlPath = @"d:\Fathers and Sons.html";
            string htmlString = File.ReadAllText(htmlPath);
            string rtfPath = Path.ChangeExtension(htmlPath, ".rtf");

            List<HtmlToRtf.SautinImage> imgList = new List<HtmlToRtf.SautinImage>();

            h.BaseURL = @"d:\";

            // Convert HTML to and place all images inside imgList.
            h.ConvertString(htmlString, imgList);

            // Save all images to HDD.
            foreach (HtmlToRtf.SautinImage img in imgList)
            {
                img.Img.Save(String.Format(@"d:\{0}.png", img.Cid));
            }

Requirements 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 higher.

.NET Standard 2.0

.NET Core 2.0 and higher.


Multi-platform component, runs on:


Our component has proven itself on cloud platforms and services:

  • Microsoft Azure
  • Amazon Web Services (AWS)
  • Google Cloud Platform
  • SharePoint
  • Docker
  • etc.

If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below: