如何在 C# 中将文本提取/插入到 RTF 字符串中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-01 09:10:39  来源:igfitidea点击:

How do I extract/insert text into RTF string in C#

提问by adeel825

In a C# console app I have the need to extract the text from an RTF string, add some more text to it, and then convert it back into RTF. I have been able to do this using the System.Windows.Forms.RichTextBox class, but I find it a bit odd to use a Forms control in a non-Forms app. Any better way to do this?

在 C# 控制台应用程序中,我需要从 RTF 字符串中提取文本,向其中添加更多文本,然后将其转换回 RTF。我已经能够使用 System.Windows.Forms.RichTextBox 类来做到这一点,但我发现在非表单应用程序中使用表单控件有点奇怪。有没有更好的方法来做到这一点?

采纳答案by Vincent

Please see discussion on this topic:

请参阅有关此主题的讨论:

Cleaning up RTF text

清理 RTF 文本

回答by samjudson

It depends on what you mean by 'better'. You are already using the simplest and easiest way of doing it.

这取决于你所说的“更好”是什么意思。您已经在使用最简单、最简单的方法。

回答by seanyboy

Doing anything with RTF is pretty difficult unless you're using the windows forms. As stated above, using forms is the easiest way to go.

除非您使用 Windows 窗体,否则使用 RTF 做任何事情都非常困难。如上所述,使用表单是最简单的方法。

You could write something yourself, but the RTF spec is pretty complicated.
http://www.biblioscape.com/rtf15_spec.htm

您可以自己编写一些东西,但是 RTF 规范非常复杂。
http://www.biblioscape.com/rtf15_spec.htm

Or you could use a conversion DLL / ActiveX object of which there is a large number available. http://www.sautinsoft.com/

或者您可以使用转换 DLL / ActiveX 对象,其中有大量可用。 http://www.sautinsoft.com/

Or - If you're doing this from Linux, there are also tools available. A cursory glance throws up UnRTF http://www.gnu.org/software/unrtf/unrtf.html

或者 - 如果您是从 Linux 执行此操作,也有可用的工具。粗略一瞥,UnRTF http://www.gnu.org/software/unrtf/unrtf.html

I haven't included stuff to turn text back to RTF because I think the RTF specification treats and formats text correctly.

我没有包含将文本转回 RTF 的内容,因为我认为 RTF 规范正确处理和格式化文本。

回答by Svend

I think you should just shake this feeling of "odd". There's nothing odd about it.

我认为你应该摆脱这种“奇怪”的感觉。这没有什么奇怪的。

回答by AMissico

There is nothing wrong with using an user-interface control in a console application or even in a web application. The Windows controls are part of the .NET Framework, might as well use them. These controls do not need to be hosted in "forms" in order to work.

在控制台应用程序甚至 Web 应用程序中使用用户界面控件并没有错。Windows 控件是 .NET Framework 的一部分,不妨使用它们。这些控件不需要托管在“表单”中才能工作。

Reinventing the wheel, using DLL/ActiveX/OCX, and using Linux are simply not practical answers to your question. The better way is...do what you know. There is actually a performance and maintainence benefit to using existing framework methods then using the suggested alternatives.

重新发明轮子、使用 DLL/ActiveX/OCX 和使用 Linux 根本不是您问题的实用答案。更好的方法是……做你所知道的。使用现有框架方法然后使用建议的替代方法实际上有性能和维护方面的好处。