Converts HTML, XHTML, ASPX string to RTF or Text string
Namespace:
SautinSoftAssembly: SautinSoft.HtmlToRtf (in SautinSoft.HtmlToRtf.dll) Version: 4.6.10.19
Syntax
| C# | Visual Basic |
Parameters
- htmlString
- String
Any string in HTML format, even piece of HTML code
Return Value
String with RTF or Text document, depends of OutputFormatnull - in case of converting failed
Remarks
Examples
using System; using System.IO; namespace Sample { class Test { static void Main(string[] args) { //Convert HTML to Word-RTF in memory //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\UTF-8 Sampler.htm"); //Set 'BaseUrl' that component can find a full path to .css and images objH.BaseURL = @"..\..\..\..\..\..\Testing HTMLs\"; //2. Convert HTML to RTF in memory word = objH.ConvertString(html); 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; } } }
Imports System Imports System.IO Imports System.Text Module Module1 Sub Main() 'Convert HTML to Word-RTF in memory '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\UTF-8 Sampler.htm") 'Set 'BaseUrl' that component can find a full path to .css and images objH.BaseURL = "..\..\..\..\..\Testing HTMLs\" '2. Convert HTML to RTF in memory word = objH.ConvertString(html) 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