Converts HTML, XHTML, ASPX string to RTF or Text string. This method can be used together with the RTF-to-HTML DLL .Net to convert RTF -> HTML -> RTF with images in memory.

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

Syntax

      
 C#  Visual Basic 
public string ConvertString(
	string htmlString,
	ArrayList ImageList
)
Public Function ConvertString ( _
	htmlString As String, _
	ImageList As ArrayList _
) As String

Parameters

htmlString
String
Any string in HTML format, even piece of HTML code
ImageList
ArrayList
ArrayList which contains images as SautinImage objects

Return Value

String with RTF or Text document, depends of OutputFormat
null - in case of converting failed

Remarks



Examples

CopyHow to convert HTML to RTF and place all images in Arraylist using C#
using System;
using System.IO;
using System.Collections;

namespace Sample
{
    class Test
    {

        static void Main(string[] args)
        {

            //Convert HTML to RTF and place all images in Arraylist
            //If you need more information about HTML-to-RTF Pro DLL .Net email us at:
            //support[at]sautinsoft.com            

            SautinSoft.HtmlToRtf objH = new SautinSoft.HtmlToRtf();
            //objH.Serial = "XXXXXXXXXXXXXXX";


            //set some options
            objH.PageStyle.PageSize.Letter();
            objH.PageStyle.PageMarginLeft.mm(20f);

            string html="";
            string word="";

            //1. Read contents of HTML document
            html = ReadFileString(@"..\..\..\..\..\..\Testing HTMLs\pic.htm");

            //Create Arraylist for storing images
            ArrayList arImages = new ArrayList();

            //Set 'BaseUrl' that component can find a full path to .css and images
            objH.BaseURL = @"..\..\..\..\..\..\Testing HTMLs\";

            //3. Convert HTML to RTF in memory            
            word = objH.ConvertString(html, arImages);

            if(word!="")
            {
                //3. Save to RTF document
                int ret = WriteToFile(@"..\..\..\..\..\..\Testing HTMLs\Word document.rtf",word);
                if (ret==0)
                    System.Diagnostics.Process.Start(@"..\..\..\..\..\..\Testing HTMLs\Word document.rtf");
            }
        }
        public static string ReadFileString(string path)
        {
            FileStream fo = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
            byte[] bHtmlIn = new byte[((int)fo.Length)];
            try
            {
                fo.Read(bHtmlIn, 0, (int)fo.Length);
                fo.Close();
                char[] arCharRes = new char[bHtmlIn.Length];
                for (int i = 0; i < bHtmlIn.Length; i++)
                {
                    arCharRes[i] = (char)bHtmlIn[i];
                }
                return new string(arCharRes);
            }
            catch
            {
                return "";
            }
        }
        public static int WriteToFile(string fileName, string contents)
        {
            try
            {
                StreamWriter sw = new StreamWriter(fileName);
                sw.Write(contents);
                sw.Close();                
            }
            catch
            {
                return 2;
            }
            return 0;
        }

    }
}
CopyHow to convert HTML to RTF and place all images in Arraylist using VB.Net
Imports System
Imports System.IO
Imports System.Text

Module Module1
    Sub Main()
        'Convert HTML to RTF and place all images in Arraylist
        'If you need more information about HTML-to-RTF Pro DLL .Net email us at:
        'support[at]sautinsoft.com            


        Dim objH As New SautinSoft.HtmlToRtf
        'objH.Serial = "XXXXXXXXXXXXXXX";



        'set some options
        objH.PageStyle.PageSize.Letter()
        objH.PageStyle.PageMarginLeft.mm(20.0F)

        Dim html As String = ""
        Dim word As String = ""

        '1. Read contents of HTML document
        html = ReadFromFile("..\..\..\..\..\Testing HTMLs\pic.htm")

        'Create Arraylist for storing images
        Dim arImages As New ArrayList

        'Set 'BaseUrl' that component can find a full path to .css and images
        objH.BaseURL = "..\..\..\..\..\Testing HTMLs\"

        '3. Convert HTML to RTF in memory            
        word = objH.ConvertString(html, arImages)

        If word <> "" Then
            '3. Save to RTF document
            Dim ret As Integer = WriteToFile("..\..\..\..\..\Testing HTMLs\Word document.rtf", word)
            If ret = 0 Then
                System.Diagnostics.Process.Start("..\..\..\..\..\Testing HTMLs\Word document.rtf")
            End If
        End If
    End Sub
    Public Function ReadFromFile(ByVal fileName As String) As String

        Dim fileString As String = ""
        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
                fileString = New String(arCharRes)
            End If
            fs.Close()
            Return fileString
        Catch
            Return ""
        End Try
    End Function


    Public Function WriteToFile(ByVal fileName As String, ByVal fileStr As String) As Integer
        Try
            Dim sw As New StreamWriter(fileName, False)
            sw.Write(fileStr)
            sw.Close()
        Catch
            Return 2
        End Try
        Return 0
    End Function
End Module

See Also