C#使用库

发布于 2024-11-07 06:18:00 字数 544 浏览 1 评论 0原文

这是一个相当菜鸟的问题,所以请耐心等待。

我正在创建一个允许发送电子邮件的软件。

发送部分工作完美。

问题在于电子邮件正文以 rtf 格式保存在 mssql 数据库中。当我检索 rtf(存储在字符串中)时,电子邮件以纯文本形式发送,而 rtf 代码被视为文本。

我需要将 rtf 文本转换为 html,并且我正在尝试使用此库: http:// /www.codeproject.com/KB/recipes/RtfConverter.aspx

问题是我不知道如何使用它。当我提取库时,有数百个文件。我设法通过在 biin/release 文件夹中找到的 dll 添加 rtf.interpreter、rtf.parser、rtf.converter.html 作为参考。但现在我不知道下一步该怎么走。

我如何在我的项目中使用它来将 rtf 字符串转换为 html?

谁能指导我完成这个?谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

︶葆Ⅱㄣ 2024-11-14 06:18:01

这里是一个最小的示例:

  // ----------------------------------------------------------------------
  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

另请查看包含的示例 RtfWinForms (winForms) 或 RtfWindows (WPF)。

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).

水波映月 2024-11-14 06:18:01

从这篇文章来看,我相信您正在寻找的类是RtfHtmlConverter

最简单的方法是将所有 .dll 文件从 Release 文件夹复制到项目内的文件夹中(您也可以将所有其他依赖项放在这里)。然后,右键单击项目中的 References 文件夹(在 VS 解决方案资源管理器中),然后(当对话框打开时)使用“浏览”来查找适当的程序集。

然后,您可以使用本文中提供的示例将 RTF 输入流转换为 HTML 字符串:

取自 您指定的文章,全部归功于作者(尽管稍加修改以返回值,而不是将其打印到控制台):

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();
}

您可以添加接受字符串的重载:

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);
    }
}

您还需要添加在源文件的开头添加几个 using 子句,以包含必要的命名空间:

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

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文