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: 4.1.7.15

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
1 - can't open input HTML file, check the input path
2 - can't create output file, check the output path
3 - converting failed

Examples

CopyHow to convert HTML string to RTF file in C#
using System;
using System.IO;
using System.Text;

namespace SampleConvert
{
    class Class1
    {
        static void Main(string[] args)
        {
            SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf();

            //this property is necessary only for registered version
            //h.Serial = "XXXXXXXXXXXXX";

            //specify some options
            h.OutputFormat = SautinSoft.HtmlToRtf.eOutputFormat.Rtf;
            h.Encoding = SautinSoft.HtmlToRtf.eEncoding.AutoSelect;
            h.PageStyle.PageSize.Letter();            

            string htmlFile = @"..\..\..\..\..\Test.htm";
            string rtfFile = @"..\..\..\..\..\Test.rtf";
            string htmlString = "";

            ReadFromFile(htmlFile,ref htmlString);

            int i = h.ConvertStringToFile(htmlString,rtfFile);
            if (i == 0)
            {
                System.Console.WriteLine("Converted successfully!");
                System.Diagnostics.Process.Start(rtfFile);
            }
            else
                System.Console.WriteLine("Converting Error!");
        }

        public static int ReadFromFile(string fileName,ref string fileStr)
        {
            try
            {                
                System.IO.FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                byte[] b = new byte[(int)fs.Length];
                if (fs.Read(b, 0, (int)fs.Length) > 0)
                {
                    char[] arCharRes = new char[fs.Length];
                    for (int i = 0; i < fs.Length; i++)
                    {
                        arCharRes[i] = (char)b[i];
                    }
                    fileStr =  new string(arCharRes);

                }
                fs.Close();                
                return 0;
            }
            catch 
            {
                //error open file
                System.Console.WriteLine("Error in open file");
                return 1;
            }
        }
    }
}
CopyHow to convert HTML string to RTF file in VB.Net
Imports System
Imports System.IO
Imports System.Text

Module Module1
    Sub Main()

        Dim h As New SautinSoft.HtmlToRtf

        'this property is necessary only for registered version
        'h.Serial = "XXXXXXXXXXXXX";


        'specify some options
        h.OutputFormat = SautinSoft.HtmlToRtf.eOutputFormat.Rtf
        h.Encoding = SautinSoft.HtmlToRtf.eEncoding.AutoSelect
        h.PageStyle.PageSize.Letter()

        Dim htmlFile As String = "..\..\..\..\Test.htm"
        Dim rtfFile As String = "..\..\..\..\Test.rtf"
        Dim htmlString As String = ""

        ReadFromFile(htmlFile, htmlString)

        Dim i As Integer = h.ConvertStringToFile(htmlString, rtfFile)
        If i = 0 Then
            System.Console.WriteLine("Converted successfully!")
            System.Diagnostics.Process.Start(rtfFile)
        Else
            System.Console.WriteLine("Converting Error!")
        End If
    End Sub
    Public Function ReadFromFile(ByVal fileName As String, ByRef fileStr As String) As Integer
        Try
            Dim fs As System.IO.FileStream = New FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)
            Dim b(CInt(Fix(fs.Length)) - 1) As Byte
            If fs.Read(b, 0, CInt(Fix(fs.Length))) > 0 Then
                Dim arCharRes(fs.Length - 1) As Char
                For i As Integer = 0 To fs.Length - 1
                    arCharRes(i) = ChrW(b(i))
                Next i
                fileStr = New String(arCharRes)

            End If
            fs.Close()
            Return 0
        Catch
            'error open file
            System.Console.WriteLine("Error in open file")
            Return 1
        End Try
    End Function
End Module

See Also