如何将字符串转换为RTF在C#中? [英] How to convert a string to RTF in C#?
问题描述
问
我如何将字符串EUROPEEN转换为RTF格式的字符串EUROP \\'e9en?
[TestMethod的]
公共无效Convert_A_Word_To_Rtf()
{
//安排
串词=EUROPEEN;
预计字符串=EUROP \\'e9en
串实际=的String.Empty; //法案
//实际= ... //如何? //断言
Assert.AreEqual(预期,实际值);
}
我发现迄今
的的RichTextBox 的
的RichTextBox可用于某些事情。例如:
的RichTextBox RichTextBox的=新的RichTextBox();
richTextBox.Text =EUROPEEN;
字符串rtfFormattedString = richTextBox.Rtf;
但后来rtfFormattedString原来是整个RTF格式的文档,而不仅仅是字符串EUROP \\'e9en。
的#1 的
的谷歌的
我还发现在网络上一堆其他资源,但没有相当解决了我的问题。
答
的 Brad佳士得的答案 的
必须添加 TRIM()
来删除结果preceeding空间
。除此之外,布拉德科视的解决方案似乎工作。
我就用这个解决方案现在即使我有不好的直觉,因为我们要子和Trim的挫折感出来的RichTextBox中获得RTF格式的字符串运行。
测试用例:
[TestMethod的]
公共无效Test_To_Verify_Brad_Christies_Stackoverflow_Answer()
{
Assert.AreEqual(@EUROP \\'e9en,EUROPEEN.ConvertToRtf());
Assert.AreEqual(@D \\'e9finitif,définitif.ConvertToRtf());
Assert.AreEqual(@\\E0,A.ConvertToRtf());
Assert.AreEqual(@H \\'e4user,豪瑟.ConvertToRtf());
Assert.AreEqual(@T \\'fcren,土人(turen).ConvertToRtf());
Assert.AreEqual(@B \\'f6den,博登.ConvertToRtf());
}
逻辑作为扩展方式:
公共静态类StringExtensions
{
公共静态字符串ConvertToRtf(此字符串值)
{
RichTextBox中的RichTextBox =新的RichTextBox();
richTextBox.Text =价值;
偏移为int = richTextBox.Rtf.IndexOf(@\\ F0 \\ fs17)+ 8; //偏移量= 118;
INT LEN = richTextBox.Rtf.LastIndexOf(@\\杆) - 偏移;
字符串结果= richTextBox.Rtf.Substring(偏移,LEN).Trim();
返回结果;
}
}
不的RichTextBox
总是有相同的页眉/页脚?你可以只读取内容的基础上抵销位置,并继续使用它来解析。 (我想?请纠正我,如果我错了)
有可用的库,但我从来没有过的好运气与他们个人(虽然总是刚刚找到另一种方法完全耗尽的可能性之前)。此外,大多数更好的的通常包括工本费。
修改结果
一个黑客攻击的一种,但这应该让你通过,你需要打通(我希望)什么:
RichTextBox的丰富=新的RichTextBox();
Console.Write(rich.Rtf);的String []字= {EUROPEEN,苹果,红萝卜,德勤,恢复,A EUROPEEN吃一个苹果,而写他的简历,德勤! };
的foreach(文字串字)
{
rich.Text =字;
INT32偏移量= rich.Rtf.IndexOf(@\\ F0 \\ fs17)+ 8;
INT32 LEN = rich.Rtf.LastIndexOf(@\\杆) - 偏移;
Console.WriteLine({0,-15}:{1},字,rich.Rtf.Substring(偏移,LEN).Trim());
}
编辑2
在codeS的击穿RTF控制code 如下:
- 标题
-
\\ F0 code>
- 使用0指数字体(第一个字体列表中,这是典型的微软无衬线(在标题的字体表所示:{ \\ fonttbl {\\ F0 \\ fnil \\ fcharset0 Microsoft无衬线;}}
)) -
\\ fs17
- 字体格式,指定大小为17(17半分是)
-
- 页脚
-
\\杆
是指定这是一个段落的结束。
-
希望这会清除一些东西了。 ; - )
Question
How do I convert the string "Européen" to the RTF-formatted string "Europ\'e9en"?
[TestMethod]
public void Convert_A_Word_To_Rtf()
{
// Arrange
string word = "Européen";
string expected = "Europ\'e9en";
string actual = string.Empty;
// Act
// actual = ... // How?
// Assert
Assert.AreEqual(expected, actual);
}
What I have found so far
RichTextBox
RichTextBox can be used for certain things. Example:
RichTextBox richTextBox = new RichTextBox();
richTextBox.Text = "Européen";
string rtfFormattedString = richTextBox.Rtf;
But then rtfFormattedString turns out to be the entire RTF-formatted document, not just the string "Europ\'e9en".
Stackoverflow
- Insert string with special characters into RTF
- How to output unicode string to RTF (using C#)
- Output RTF special characters to Unicode
- Convert Special Characters for RTF (iPhone)
I've also found a bunch of other resources on the web, but nothing quite solved my problem.
Answer
Had to add Trim()
to remove the preceeding space in result
. Other than that, Brad Christie's solution seems to work.
I'll run with this solution for now even though I have a bad gut feeling since we have to SubString and Trim the heck out of RichTextBox to get a RTF-formatted string.
Test case:
[TestMethod]
public void Test_To_Verify_Brad_Christies_Stackoverflow_Answer()
{
Assert.AreEqual(@"Europ\'e9en", "Européen".ConvertToRtf());
Assert.AreEqual(@"d\'e9finitif", "définitif".ConvertToRtf());
Assert.AreEqual(@"\'e0", "à".ConvertToRtf());
Assert.AreEqual(@"H\'e4user", "Häuser".ConvertToRtf());
Assert.AreEqual(@"T\'fcren", "Türen".ConvertToRtf());
Assert.AreEqual(@"B\'f6den", "Böden".ConvertToRtf());
}
Logic as an extension method:
public static class StringExtensions
{
public static string ConvertToRtf(this string value)
{
RichTextBox richTextBox = new RichTextBox();
richTextBox.Text = value;
int offset = richTextBox.Rtf.IndexOf(@"\f0\fs17") + 8; // offset = 118;
int len = richTextBox.Rtf.LastIndexOf(@"\par") - offset;
string result = richTextBox.Rtf.Substring(offset, len).Trim();
return result;
}
}
Doesn't RichTextBox
always have the same header/footer? You could just read the content based on off-set location, and continue using it to parse. (I think? please correct me if I'm wrong)
There are libraries available, but I've never had good luck with them personally (though always just found another method before fully exhausting the possibilities). In addition, most of the better ones are usually include a nominal fee.
EDIT
Kind of a hack, but this should get you through what you need to get through (I hope):
RichTextBox rich = new RichTextBox();
Console.Write(rich.Rtf);
String[] words = { "Européen", "Apple", "Carrot", "Touché", "Résumé", "A Européen eating an apple while writing his Résumé, Touché!" };
foreach (String word in words)
{
rich.Text = word;
Int32 offset = rich.Rtf.IndexOf(@"\f0\fs17") + 8;
Int32 len = rich.Rtf.LastIndexOf(@"\par") - offset;
Console.WriteLine("{0,-15} : {1}", word, rich.Rtf.Substring(offset, len).Trim());
}
EDIT 2
The breakdown of the codes RTF control code are as follows:
- Header
- Footer
\par
is specifying that it's the end of a paragraph.
Hopefully that clears some things up. ;-)
这篇关于如何将字符串转换为RTF在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!