Converts HTML, XHTML, ASPX string to RTF or Text file. Output file will be overwritten.

Namespace:  SautinSoft
Assembly:  SautinSoft.HtmlToRtf (in SautinSoft.HtmlToRtf.dll) Version: 5.8.2.8

Syntax

      
 C#  Visual Basic 
public int ConvertStringToFile(
	string htmlString,
	string outputFile
)
Public Function ConvertStringToFile ( _
	htmlString As String, _
	outputFile As String _
) As Integer

Parameters

htmlString
String
Any string in HTML format, even piece of HTML code
outputFile
String
Local path to save output RTF or Text file

Return Value

0 - converting successfully
2 - can't create output file, check the output path
3 - converting failed

Remarks



Examples

CopyHow to convert HTML string to RTF file in C#
using System;
using System.IO;
using System.Collections.Generic;
using SautinSoft;

namespace Sample
{
    class Test
    {

        static void Main(string[] args)
        {
            // Convert HTML to RTF in memory
            // If you need more information about "HTML to RTF .Net" email us at:
            // support[at]sautinsoft.com        
            SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf();
            // After purchasing the license, please insert your serial number here to activate the component
            //h.Serial = "XXXXXXXXXXXXXXX";

            DirectoryInfo htmlDir = new DirectoryInfo(@"..\..\..\..\..\..\Testing HTMLs");
            string htmlFile = Path.Combine(htmlDir.FullName, "sample doc.html");
            string rtfFile = Path.ChangeExtension(htmlFile, ".rtf");

            // Assume that we already have HTML as string object.
            string htmlString = File.ReadAllText(htmlFile);

            // Set BaseUrl to transform relative paths into absolute.
            h.BaseURL = htmlDir.FullName;

            // Convert HTML to RTF in memory.
            string rtfString = h.ConvertString(htmlString);

            if (rtfString!=null)
            {
                File.WriteAllText(rtfFile, rtfString);
                Console.WriteLine("Converted successfully!");
                System.Diagnostics.Process.Start(rtfFile);
            }
            else
            {
                Console.WriteLine("Converting failed!");
                Console.ReadLine();
            }
        }
    }
}
CopyHow to convert HTML string to RTF file in VB.Net
Imports System
Imports System.IO
Imports System.Text

Module Module1
    Sub Main()
        ' Convert HTML to RTF in memory
        ' If you need more information about "HTML to RTF .Net" email us at:
        ' support[at]sautinsoft.com        
        Dim h As New SautinSoft.HtmlToRtf()
        ' After purchasing the license, please insert your serial number here to activate the component
        'h.Serial = "XXXXXXXXXXXXXXX";


        Dim htmlDir As New DirectoryInfo("..\..\..\..\..\Testing HTMLs")
        Dim htmlFile As String = Path.Combine(htmlDir.FullName, "sample doc.html")
        Dim rtfFile As String = Path.ChangeExtension(htmlFile, ".rtf")

        ' Assume that we already have HTML as string object.
        Dim htmlString As String = File.ReadAllText(htmlFile)

        ' Set BaseUrl to transform relative paths into absolute.
        h.BaseURL = htmlDir.FullName

        ' Convert HTML to RTF in memory.
        Dim rtfString As String = h.ConvertString(htmlString)

        If rtfString IsNot Nothing Then
            File.WriteAllText(rtfFile, rtfString)
            Console.WriteLine("Converted successfully!")
            System.Diagnostics.Process.Start(rtfFile)
        Else
            Console.WriteLine("Converting failed!")
            Console.ReadLine()
        End If
    End Sub
End Module

See Also