如何使用特殊字符将字符串编程入RTF? 我有rtf模板我加载到字符串,然后用数据替换所有 $ MY_VARIABLE $ 。 数据包含像'ąęść'这样的特殊字符,问题是在结果文件中这些字符被替换为'?'。编码有问题吗?
我的代码看起来像:
StreamReader reader = new StreamReader(template.rtf); StringBuilder form = new StringBuilder(reader.ReadToEnd()); //这里我用数据替换rtf中的变量 Encoding srcEncoding = new UTF8Encoding(); 编码dstEncoding = new ASCIIEncoding(); byte [] utf = srcEncoding.GetBytes(form.ToString()); byte [] asci = Encoding.Convert(Encoding.UTF8,Encoding.ASCII,utf); return dstEncoding.GetString(asci);解决方案
请检查答案到这个问题。
已编辑添加
如上所述,上述答案适用于RTF到PlainText的转换,根据 RTF规范1.6 你使用 \\\☚ 显示ą, \\\⠞ ę。
语法是 \u Nd ,其中 N 是字符的十进制Unicode值,而 d 是ASCII近似值。
修改为澄清
对于你说的话,你在RTF中有一些占位符,对吗?
你需要做的是在替换占位符时添加经过一点研究,我想你可能会使用这样的东西:
公共函数GetRtfString(ByVal text As String)As String Dim sb As New Text.StringBuilder()为每个c As Char在text Dim code = Convert.ToInt32(c) If(Char.IsLetter(c)AndAlso code& H80)Then sb.Append(c) Else sb.AppendFormat(CultureInfo.InvariantCulture,\ {0} {1},代码,RemoveDiacritics(c)) End If Next 返回sb.ToString() 结束函数 公共函数RemoveDiacritics(ByVal text As String)As String Dim formD = text.Normalize(System.Text。 NormalizationForm.FormD) Dim sb As New Text.StringBuilder() 对于每个c As Char在formD If(CharUnicodeInfo.GetUnicodeCategory(c)< UnicodeCategory.NonSpacingMark)然后 sb.Append(c) End If Next 返回sb.ToString()。Normalize(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
发布评论