简体   繁体   中英

c# using library

This is quite a noob question, so please bear with me.

I am creating a software which allows email to be sent.

The sending part works flawlessly.

The problem is that the body of the email is saved in a mssql database in rtf format. When I retrieve the rtf (stored in a string), the email is sent as plain text, with the rtf code considered as text.

I need to convert the rtf text to html and I am trying to use this library: http://www.codeproject.com/KB/recipes/RtfConverter.aspx

Thing is I have no clue on how to use it. When I extract the library there are hundreds of files. I managed to add as reference rtf.interpreter, rtf.parser, rtf.converter.html through dlls found in the biin/release folder. But now I dont know the next step.

How can I use this in my project to convert the rtf string to html?

Can anyone guide me through this? Thank you.

From looking at the article, I believe the class you are looking for is RtfHtmlConverter .

The easiest way would be to copy all .dll files from the Release folder into a folder inside your project (you can put all other dependencies here also). Then, right click the References folder in your project (in VS Solution Explorer), and (when the dialog opens) use "Browse" to find appropriate assemblies.

Then, you can use the example provided in the article to convert an RTF input stream to a HTML string:

Taken from the article you specified , all credits to the author (although slightly modified to return the value, instead of printing it to console):

public string ConvertRtf2Html(Stream rtfStream)
{
    // logger
    RtfInterpreterListenerFileLogger logger =
      new RtfInterpreterListenerFileLogger(@"c:\temp\RtfInterpreter.log");

    // image converter
    // convert all images to JPG
    RtfVisualImageAdapter imageAdapter =
       new RtfVisualImageAdapter(ImageFormat.Jpeg);
    RtfImageConvertSettings imageConvertSettings =
                   new RtfImageConvertSettings(imageAdapter);
    imageConvertSettings.ScaleImage = true; // scale images
    RtfImageConverter imageConverter =
            new RtfImageConverter(imageConvertSettings);

    // interpreter
    IRtfDocument rtfDocument = RtfInterpreterTool.Interpret(rtfStream,
                                              logger, imageConverter);

    // html converter
    RtfHtmlConvertSettings htmlConvertSettings =
           new RtfHtmlConvertSettings(imageAdapter);
    htmlConvertSettings.StyleSheetLinks.Add("default.css");
    RtfHtmlConverter htmlConverter = new RtfHtmlConverter(rtfDocument,
                                                 htmlConvertSettings);
    return htmlConverter.Convert();
}

You can add an overload which accepts a string:

public string ConvertRtfToHtml(string rtfString)
{
    string sourceRtf = "some rtf";
    byte[] data = ASCIIEncoding.Default.GetBytes(sourceRtf);
    using (MemoryStream ms = new MemoryStream(data))
    {
         // call the method above
         return ConvertRtfToHtml(ms);
    }
}

You will also need to add several using clauses to the beginning of your source file, to include the necessary namespaces:

using Itenso.Rtf.Interpreter;
using Itenso.Rtf.Converter.Html;
using Itenso.Rtf.Converter.Image;

Here a minimal sample:

  // ----------------------------------------------------------------------
  private static string ConvertRtfToHtml()
  {
    const string sampleRtfText = @"{\rtf1foobar}";

    IRtfDocument rtfDocument = RtfInterpreterTool.BuildDoc( sampleRtfText );

    RtfHtmlConvertSettings settings = new RtfHtmlConvertSettings();
    settings.ConvertScope = RtfHtmlConvertScope.Content;

    RtfHtmlConverter htmlConverter = new RtfHtmlConverter( rtfDocument, settings );
    return htmlConverter.Convert();
  } // ConvertRtfToHtml

Check out also the included samples RtfWinForms (winForms) or RtfWindows (WPF).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM