如何在 C# 中从纯文本(或字符串)创建 RTF?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12347587/
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
How to create RTF from plain text (or string) in C#?
提问by Jerry
Could anyone please help me to create RTF from string in C#?
任何人都可以帮我从 C# 中的字符串创建 RTF 吗?
I save all the formats (bold, italic, etc) in a customized class.
我将所有格式(粗体、斜体等)保存在自定义类中。
Jerry
杰瑞
采纳答案by Peter
I'd use an external library instead of coding your own. Check these projects:
我会使用外部库而不是自己编写代码。检查这些项目:
- http://sourceforge.net/projects/netrtfwriter/
- http://www.codeproject.com/Articles/11306/NRTFTree-A-class-library-for-RTF-processing-in-C
- http://sourceforge.net/projects/netrtfwriter/
- http://www.codeproject.com/Articles/11306/NRTFTree-A-class-library-for-RTF-processing-in-C
You might also be able to use the Rtf property of the RichTextBox control(if your users are entering data in such a control).
您也许还可以使用RichTextBox 控件的Rtf 属性(如果您的用户正在此类控件中输入数据)。
回答by Freeman
A good library for working/Creating/Editing RTF files can be found here:
一个很好的用于工作/创建/编辑 RTF 文件的库可以在这里找到:
http://sourceforge.net/projects/netrtfwriter/
http://sourceforge.net/projects/netrtfwriter/
its free, and just needs a bit of documentation, also you can use the NuGet package manager to find lots of alternatives.
它是免费的,只需要一些文档,你也可以使用 NuGet 包管理器来找到很多替代品。
回答by Dmytro
As the simplest option, you can use RichTextBoxControl in winforms application.
作为最简单的选择,您可以在 winforms 应用程序中使用 RichTextBoxControl。
richTextBox1.SaveFile(@"C:\temp\test.rtf", RichTextBoxStreamType.RichText);
回答by Jhollman
I know i'm NECRO an old question but, anyways, here is my apport:
我知道我是 NECRO 一个老问题,但无论如何,这是我的报告:
//This Instances a new RichTextBox Control and uses it so save the Text
private void Save_RTF_file(string pFilePath, string pRTFText)
{
try
{
using (RichTextBox RTB = new RichTextBox())
{
RTB.Rtf = pRTFText;
RTB.SaveFile(pFilePath, RichTextBoxStreamType.RichText);
}
}
catch (Exception ex)
{
throw ex;
}
}
Now you pass a file_path and the RTF formattedText:
现在你传递一个 file_path 和RTF 格式的文本:
//This is a simple 1 line 'Hello World' RTF text
string RTF = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang14346{\fonttbl{\f0\fnil\fcharset0 Calibri;}} {\*\generator Riched20 10.0.10586}\viewkind4\uc1 \pard\sa200\sl276\slmult1\f0\fs22\lang10 Hello World.\par }";
Save_RTF_file(@"C:\temp\my_rtf_file.rtf"), RTF);
Hope it helps.
希望能帮助到你。

