简体   繁体   English

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

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

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

Please see discussion on this topic: 请参阅有关此主题的讨论:

Cleaning up RTF text 清理RTF文本

Doing anything with RTF is pretty difficult unless you're using the windows forms. 除非您使用Windows窗体,否则使用RTF执行任何操作都非常困难。 As stated above, using forms is the easiest way to go. 如上所述,使用表单是最简单的方法。

You could write something yourself, but the RTF spec is pretty complicated. 你可以自己写一些东西,但RTF规范非常复杂。
http://www.biblioscape.com/rtf15_spec.htm http://www.biblioscape.com/rtf15_spec.htm

Or you could use a conversion DLL / ActiveX object of which there is a large number available. 或者您可以使用转换DLL / ActiveX对象,其中有大量可用。 http://www.sautinsoft.com/ http://www.sautinsoft.com/

Or - If you're doing this from Linux, there are also tools available. 或者 - 如果您是从Linux执行此操作,则还有可用的工具。 A cursory glance throws up UnRTF http://www.gnu.org/software/unrtf/unrtf.html 粗略的一瞥引发了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规范正确地处理和格式化文本。

I think you should just shake this feeling of "odd". 我想你应该动摇这种“奇怪”的感觉。 There's nothing odd about it. 它没什么奇怪的。

There is nothing wrong with using an user-interface control in a console application or even in a web application. 在控制台应用程序甚至Web应用程序中使用用户界面控件没有任何问题。 The Windows controls are part of the .NET Framework, might as well use them. Windows控件是.NET Framework的一部分,也可以使用它们。 These controls do not need to be hosted in "forms" in order to work. 为了工作,这些控件不需要以“表单”托管。

Reinventing the wheel, using DLL/ActiveX/OCX, and using Linux are simply not practical answers to your question. 重新发明轮子,使用DLL / ActiveX / OCX,并使用Linux根本不是你的问题的实际答案。 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. 实际上,使用现有的框架方法然后使用建议的替代方案可以获得性能和维护优势。

It depends on what you mean by 'better'. 这取决于你对'更好'的意思。 You are already using the simplest and easiest way of doing it. 您已经在使用最简单,最简单的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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