Converts HTML, XHTML, ASPX string to RTF or Text file. Output file will be overwritten.
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
- outputFile
- String
Local path to save output RTF or Text file
Return Value
0 - converting successfully2 - can't create output file, check the output path
3 - converting failed
Remarks
Examples
using System; using System.IO; namespace Sample { class Test { static void Main(string[] args) { //Convert HTML string RTF file SautinSoft.HtmlToRtf objH = new SautinSoft.HtmlToRtf(); string html = ""; string pathRtf = @"..\..\..\..\..\..\Testing HTMLs\Word document.rtf"; string pathHtml = @"..\..\..\..\..\..\Testing HTMLs\UTF-8 Sampler.htm"; //1. Read contents of HTML document html = ReadFileString(pathHtml); //Set 'BaseUrl' that component can find a full path to .css and images objH.BaseURL = System.IO.Path.GetDirectoryName(System.IO.Path.GetFullPath(pathHtml)); //2. Convert HTML string to RTF file int ret = objH.ConvertStringToFile(html, pathRtf); // 0 - converting successfully // 1 - can't open input file or URL, check the input path // 2 - can't create output file, check the output path // 3 - converting failed if(ret==0 ) { //Show produced RTF file System.Diagnostics.Process.Start(pathRtf); } } 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 ""; } } } }
Imports System Imports System.IO Imports System.Text Module Module1 Sub Main() 'Convert HTML string to RTF file Dim objH As New SautinSoft.HtmlToRtf Dim html As String = "" Dim htmlPath As String = "..\..\..\..\..\Testing HTMLs\UTF-8 Sampler.htm" Dim rtfPath As String = "..\..\..\..\..\Testing HTMLs\Word document.rtf" '1. Read contents of HTML document html = ReadFromFile(htmlPath) 'Set 'BaseUrl' that component can find a full path to .css and images objH.BaseURL = System.IO.Path.GetDirectoryName(System.IO.Path.GetFullPath(htmlPath)) '2. Convert HTML string to RTF file Dim ret As Integer = objH.ConvertStringToFile(html, rtfPath) ' 0 - converting successfully ' 1 - can't open input file or URL, check the input path ' 2 - can't create output file, check the output path ' 3 - converting failed If ret = 0 Then 'Show produced RTF file System.Diagnostics.Process.Start(rtfPath) 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 End Module