将字符串包含特殊字符转换为RTF [英] Insert string with special characters into RTF
问题描述
如何编程有特殊字符转换为RTF插入字符串?
我有RTF模板我装载到字符串,然后用数据替换所有 $ MY_VARIABLE $
。
数据中包含特殊字符,如AESC,问题是,在结果文件这些字符替换为?。这件事情错编码,但什么?
我的code如下:
StreamReader的读者=新的StreamReader(template.rtf);
StringBuilder的形式=新的StringBuilder(reader.ReadToEnd());
//这里我替换变量与数据RTF
编码srcEncoding =新UTF8Encoding();
编码dstEncoding =新ASCIIEncoding();
字节[] = UTF srcEncoding.GetBytes(form.ToString());
字节[] = ASCI Encoding.Convert(Encoding.UTF8,Encoding.ASCII,UTF);
返回dstEncoding.GetString(ASCI);
请检查<一href=\"http://stackoverflow.com/questions/1310694/output-rtf-special-characters-to-uni$c$c/1315997#1315997\">the回答以这个问题。
编辑补充
至于你说的是,以上的答案适用于RTF明文的转换,根据的 RTF规范1.6 您使用 \\ u261a
显示 A
, \\ u281e
为电子
...
语法 \\ U 的Nd ,其中 N 的是十进制的Uni code的值字符,和的 D 的是ASCII逼近。
编辑,以澄清
有关你说什么,你在RTF一些占位符,对吧?
您需要做的是有一个函数,替换占位符时,添加适当的RTF EN codeD字符。
研究一点点之后,我想你可以使用这样的:
公共功能GetRtfString(BYVAL文本作为字符串)作为字符串 昏暗某人作为新Text.StringBuilder()
对于每个C为CHAR在文本
昏暗code = Convert.ToInt32(C)
如果(Char.IsLetter(三)AndAlso运算code&LT;&安培; H80)然后
sb.Append(三)
其他
sb.AppendFormat(CultureInfo.InvariantCulture,\\ü{0} {1},code,RemoveDiacritics(C))
万一
下一个
返回sb.ToString()结束功能公共职能RemoveDiacritics(BYVAL文本作为字符串)作为字符串 昏暗formD = text.Normalize(System.Text.NormalizationForm.FormD)
昏暗某人作为新Text.StringBuilder() 对于每个C为CHAR在formD
如果(CharUni codeInfo.GetUni codeCategory(三)LT;&GT;统一codeCategory.NonSpacingMark)然后
sb.Append(三)
万一
下一个 返回sb.ToString()。规范化(System.Text.NormalizationForm.FormC)结束功能
How to programatically insert string with special characters into RTF?
I have rtf template I load to string and then replace all $MY_VARIABLE$
with data.
Data contains special chars like 'ąęść' and the problem is that in result file these characters are replaced with '?'. It's something wrong with encoding but what?
My code looks like:
StreamReader reader = new StreamReader("template.rtf");
StringBuilder form = new StringBuilder(reader.ReadToEnd());
// here I replace variables in rtf with data
Encoding srcEncoding = new UTF8Encoding();
Encoding dstEncoding = new ASCIIEncoding();
byte[] utf = srcEncoding.GetBytes(form.ToString());
byte[] asci = Encoding.Convert(Encoding.UTF8, Encoding.ASCII, utf);
return dstEncoding.GetString(asci);
Please, check the answer to this question.
Edited to Add
As you say that the above answer applies to the conversion of RTF to PlainText, according to RTF Specification 1.6 you use \u261a
to display ą
, \u281e
for ę
...
The syntax is \uNd where N is the decimal Unicode value for the character, and d is the ASCII approximation.
Edited to Clarify
For what you say, you have some placeholders in the RTF, right?
What you need to do is to have a function that, when replacing the placeholders, add the proper RTF encoded characters.
After a little bit of research, I think you may use something like this:
Public Function GetRtfString(ByVal text As String) As String
Dim sb As New Text.StringBuilder()
For Each c As Char In text
Dim code = Convert.ToInt32(c)
If (Char.IsLetter(c) AndAlso code < &H80) Then
sb.Append(c)
Else
sb.AppendFormat(CultureInfo.InvariantCulture, "\u{0}{1}", code, RemoveDiacritics(c))
End If
Next
Return sb.ToString()
End Function
Public Function RemoveDiacritics(ByVal text As String) As String
Dim formD = text.Normalize(System.Text.NormalizationForm.FormD)
Dim sb As New Text.StringBuilder()
For Each c As Char In formD
If (CharUnicodeInfo.GetUnicodeCategory(c) <> UnicodeCategory.NonSpacingMark) Then
sb.Append(c)
End If
Next
Return sb.ToString().Normalize(System.Text.NormalizationForm.FormC)
End Function
这篇关于将字符串包含特殊字符转换为RTF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!