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:

Download


To verify the functionality of our SDK, download the latest «HTML to RTF .Net» with code examples, 32.6 Mb.

Restrictions:

Free version of «HTML to RTF .Net» has a notification "Created by an unlicensed version of «HTML to RTF .Net»" and the random addition of the word "TRIAL VERSION".

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))
	{
	sing (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 imgList = new List();

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));
}

Technical information and requirements


Requires only .NET Framework 4.0 and up or .NET Core 2.0 and up. Our product is compatible with all languages .NET 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, .NET Core
  • .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:

  • Windows
  • Linux
  • Mac OS
WindowsLinuxMac OS

Our component has proven itself on cloud platforms and services:

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