What is the best solution for converting RichTextFormat info to HTML in C#?

女生的网名这么多〃 提交于 2019-12-04 06:07:45
Tony Peterson

I recently used a RTF to HTML conRTverter that worked great, called DocFrac.

It can be used with a GUI to convert files, but it also is a DLL.

I converted over 400 RTF files to HTML in a few minutes so performance is good too. I used the GUI so I don't have the details on the DLL. According to the site the DLL works with .NET however.

DocFrac at SourceForge

Update: fixed link, because www.docfrac.net doesn't exist anymore.

Try to use this library RTF to HTML .Net. It supports RTF to HTML and text to HTML converting ways. Full version not free but there is a free trial.

This code maybe useful:

        SautinSoft.RtfToHtml r = new SautinSoft.RtfToHtml();

        //specify some options
        r.OutputFormat = SautinSoft.RtfToHtml.eOutputFormat.XHTML_10;
        r.Encoding = SautinSoft.RtfToHtml.eEncoding.UTF_8;

        string rtfFile = @"d:\test.rtf";
        string htmlFile = @"d:\test.html";
        string rtfString = null;
        ReadFromFile(rtfFile,ref rtfString);

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

    public static int ReadFromFile(string fileName,ref string fileStr)
    {
        try
        {
            FileInfo fi = new FileInfo(fileName);
            StreamReader strmRead = fi.OpenText();
            fileStr = strmRead.ReadToEnd();
            strmRead.Close();
            return 0;
        }
        catch 
        {
            //error open file
            System.Console.WriteLine("Error in open file");
            return 1;
        }
    }

ScroogeXHTML, a small library for RTF to HTML / XHTML conversion, might be useful. However it only supports a subset of the RTF standard. For reports with tables and other advanced layout, there are other libraries like the Logictran R2Net converter.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!