如何将RTF字符串转换为Markdown字符串(并反向转换)(C#.NET Core或JS) [英] How do I convert an RTF string to a Markdown string (and back) (C# .NET Core, or JS)

查看:284
本文介绍了如何将RTF字符串转换为Markdown字符串(并反向转换)(C#.NET Core或JS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C#或JS中将RTF字符串转换为Markdown字符串(并反过来),理想情况下不包装exe?

How do I convert an RTF string to a Markdown string (and back) either in C# or JS, ideally without wrapping an exe?

我有一个使用.NET的RichTextBox控件的旧产品.使用它的表单将其输出保存为Microsoft专有的 RTF格式.这是它可以生成的输出的一个示例:

I have a legacy product that uses .NET's RichTextBox control. Forms that use it save their output in Microsoft's proprietary RTF format. Here is a small example of the output it can generate:

{\\rtf1\\ansi\\ansicpg1252\\uc1\\htmautsp\\deff2{\\fonttbl{\\f0\\fcharset0 Times New Roman;}{\\f2\\fcharset0 GenericSansSerif;}}{\\colortbl\\red0\\green0\\blue0;\\red255\\green255\\blue255;}\\loch\\hich\\dbch\\pard\\plain\\ltrpar\\itap0{\\lang1033\\fs18\\f2\\cf0 \\cf0\\ql{\\f2 {\\ltrch Some content here }\\li0\\ri0\\sa0\\sb0\\fi0\\ql\\par}\r\n}\r\n}

我的C#.NET Core Web App必须能够使用此存储的RTF在网页上显示富文本编辑器",具有更新值的能力并保存为仍可使用的格式通过旧产品.

My C# .NET Core Web App needs to be able to use this stored RTF to display a "Rich Text Editor" on a web page, have the ability to update the value, and save in a format that can still be used by the legacy product.

不幸的是,我在查找可以将RTF用作输入的现有/现代Web组件时遇到了麻烦.大多数似乎使用markdown或自定义JSON格式.

Unfortunately, I am having trouble finding existing/modern web components that can use RTF as input. Most appear to use markdown or a custom JSON format.

  1. 使用以下任一方法将现有的RTF转换为Markdown:
    • 服务器端,使用C#
    • 客户端,使用JS
  1. Convert the existing RTF to Markdown using either:
    • Server side, using C#
    • Client side, using JS

到目前为止,我已经尝试过:

  • 按照此CodeProject的说明创建自定义RTF-> HTML转换器:编写您自己的RTF转换器
    • 我可以使它在.NET Framework项目中工作,但不能在.NET Core中工作
    • So far, I have tried:

      • Following this CodeProject write-up for creating a custom RTF -> HTML converter: Writing Your Own RTF Converter
        • I can get it to work in a .NET Framework project, but not .NET Core
          • 在.NET Core项目中引发空引用错误
          • 仅支持RTF的一小部分,创建整个HTML文档而不是字符串/子集,并破坏了我的特定示例

          注意:我尝试过的操作来自RTF-> HTML,因为我找不到适合RTF-> Markdown的任何东西.我的希望是,如果必须的话,我可以做:RTF-> HTML-> Markdown(反之亦然).

          Note: The things I've tried are from RTF -> Html because I couldn't find anything for RTF -> Markdown specifically. My hope was that I could, if I had to, do: RTF -> HTML -> Markdown (and in reverse) as a last resort.

          推荐答案

          很抱歉您使用遇到的空引用错误RtfPipe 和.Net Core.现在,在项目上记录了解决这些错误的方法,其中涉及包括NuGet软件包System.Text.Encoding.CodePages并注册代码页提供程序.

          Sorry for the null reference errors you had with RtfPipe and .Net Core. A resolution to these errors is now documented on the project and involves including the NuGet package System.Text.Encoding.CodePages and registering the code page provider.

          #if NETCORE
            // Add a reference to the NuGet package System.Text.Encoding.CodePages for .Net core only
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
          #endif
          var html = Rtf.ToHtml(rtf);
          

          由于HTML在技术上是Markdown,因此您可以在此处停止.否则,您也可以使用我的 BracketPipe 库将HTML转换为Markdown.代码看起来像这样.

          Since HTML is technically Markdown, you can stop here. Otherwise, you can convert the HTML to Markdown as well using my BracketPipe library. The code would look something like.

          using BracketPipe;
          using RtfPipe;
          
          private string RtfToMarkdown(string source)
          {
            using (var w = new System.IO.StringWriter())
            using (var md = new MarkdownWriter(w))
            {
              Rtf.ToHtml(source, md);
              md.Flush();
              return w.ToString();
            }
          }
          

          Markdig 是从Markdown到HTML的很好的库.但是,对于从HTML到RTF的获取,我没有任何好的建议.

          Markdig is a good library for getting from Markdown to HTML. However, I don't have any good suggestions for getting from HTML to RTF.

          免责声明:我是 RtfPipe

          Disclaimer: I am the author of the RtfPipe and BracketPipe open source projects

          这篇关于如何将RTF字符串转换为Markdown字符串(并反向转换)(C#.NET Core或JS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆