作成日: 2016/03/03 最終更新日: 2016/05/18
文書種別
不具合
状況
修正済み
詳細
半角カナを含む文字列をRtfFilterのConvertToDocumentメソッドで変換すると、正しく変換されない場合があります。
回避方法
この問題はバージョン4.0.20161.507で修正されました。
修正版の適用方法については、アップデートの方法を参照してください。
修正版を適用しない場合の回避方法は次の通りです。
以下のような関数を使用し、Shift-JIS文字コード形式ではなく、半角カナ文字を直接RTFテキスト内に記述します。
◎サンプルコード(Visual Basic)
◎サンプルコード(C#)
修正版の適用方法については、アップデートの方法を参照してください。
修正版を適用しない場合の回避方法は次の通りです。
以下のような関数を使用し、Shift-JIS文字コード形式ではなく、半角カナ文字を直接RTFテキスト内に記述します。
◎サンプルコード(Visual Basic)
Dim regex As Regex = New Regex("(¥¥¥¥¥'(?8[1-9A-Fa-f]|9[0-9A-Fa-f]|[eE][0-9A-Fa-f]|[fF][0-9A-Ca-c])¥¥¥¥¥'(?[0-9A-Fa-f][0-9A-Fa-f]))|(¥¥¥¥¥'(?[0-7][0-9A-Fa-f]|80|[A-Da-d][0-9A-Fa-f]|[Ff][D-Fd-f]))", RegexOptions.Compiled)
Public Function ConvertDBCS(rtf As String) As String
Dim enc As Encoding = Encoding.GetEncoding(932)
Dim dic1 As Dictionary(Of String, String) = New Dictionary(Of String, String)
Dim dic2 As Dictionary(Of String, String) = New Dictionary(Of String, String)
Dim matches = regex.Matches(rtf)
Dim bytes As Byte()
For Each m As Match In matches
Dim b1 = m.Groups("b1").Value
Dim b2 = m.Groups("b2").Value
If b2 <> "" Then
bytes = New Byte() {Convert.ToByte(b1, 16), Convert.ToByte(b2, 16)}
If Not dic1.ContainsKey(m.Value) Then
dic1.Add(m.Value, "¥¥f0¥¥fs32" + New String(enc.GetChars(bytes)))
End If
Else
bytes = New Byte() {Convert.ToByte(b1, 16)}
If Not dic2.ContainsKey(m.Value) Then
dic2.Add(m.Value, "¥¥f0¥¥fs32" + New String(enc.GetChars(bytes)))
End If
End If
Next
For Each key As String In dic1.Keys
rtf = rtf.Replace(key, dic1(key))
Next
For Each key As String In dic2.Keys
rtf = rtf.Replace(key, dic2(key))
Next
Return rtf
End Function
Public Function ConvertDBCS(rtf As String) As String
Dim enc As Encoding = Encoding.GetEncoding(932)
Dim dic1 As Dictionary(Of String, String) = New Dictionary(Of String, String)
Dim dic2 As Dictionary(Of String, String) = New Dictionary(Of String, String)
Dim matches = regex.Matches(rtf)
Dim bytes As Byte()
For Each m As Match In matches
Dim b1 = m.Groups("b1").Value
Dim b2 = m.Groups("b2").Value
If b2 <> "" Then
bytes = New Byte() {Convert.ToByte(b1, 16), Convert.ToByte(b2, 16)}
If Not dic1.ContainsKey(m.Value) Then
dic1.Add(m.Value, "¥¥f0¥¥fs32" + New String(enc.GetChars(bytes)))
End If
Else
bytes = New Byte() {Convert.ToByte(b1, 16)}
If Not dic2.ContainsKey(m.Value) Then
dic2.Add(m.Value, "¥¥f0¥¥fs32" + New String(enc.GetChars(bytes)))
End If
End If
Next
For Each key As String In dic1.Keys
rtf = rtf.Replace(key, dic1(key))
Next
For Each key As String In dic2.Keys
rtf = rtf.Replace(key, dic2(key))
Next
Return rtf
End Function
◎サンプルコード(C#)
Regex regex = new Regex("(¥¥¥¥¥'(?8[1-9A-Fa-f]|9[0-9A-Fa-f]|[eE][0-9A-Fa-f]|[fF][0-9A-Ca-c])¥¥¥¥¥'(?[0-9A-Fa-f][0-9A-Fa-f]))|(¥¥¥¥¥'(?[0-7][0-9A-Fa-f]|80|[A-Da-d][0-9A-Fa-f]|[Ff][D-Fd-f]))", RegexOptions.Compiled);
public string ConvertDBCS(string rtf)
{
Encoding enc = Encoding.GetEncoding(932);
Dictionary dic1 = new Dictionary();
Dictionary dic2 = new Dictionary();
var matches = regex.Matches(rtf);
byte[] bytes;
foreach(Match m in matches)
{
var b1 = m.Groups["b1"].Value;
var b2 = m.Groups["b2"].Value;
if (b2 != "")
{
bytes = new byte[] { Convert.ToByte(b1, 16), Convert.ToByte(b2, 16) };
if (!dic1.ContainsKey(m.Value))
dic1.Add(m.Value, "¥¥f0¥¥fs32" + new string(enc.GetChars(bytes)));
}
else
{
bytes = new byte[] { Convert.ToByte(b1, 16) };
if (!dic2.ContainsKey(m.Value))
dic2.Add(m.Value, "¥¥f0¥¥fs32" + new string(enc.GetChars(bytes)));
}
}
foreach (string key in dic1.Keys)
rtf = rtf.Replace(key, dic1[key]);
foreach (string key in dic2.Keys)
rtf = rtf.Replace(key, dic2[key]);
return rtf;
}
public string ConvertDBCS(string rtf)
{
Encoding enc = Encoding.GetEncoding(932);
Dictionary
Dictionary
var matches = regex.Matches(rtf);
byte[] bytes;
foreach(Match m in matches)
{
var b1 = m.Groups["b1"].Value;
var b2 = m.Groups["b2"].Value;
if (b2 != "")
{
bytes = new byte[] { Convert.ToByte(b1, 16), Convert.ToByte(b2, 16) };
if (!dic1.ContainsKey(m.Value))
dic1.Add(m.Value, "¥¥f0¥¥fs32" + new string(enc.GetChars(bytes)));
}
else
{
bytes = new byte[] { Convert.ToByte(b1, 16) };
if (!dic2.ContainsKey(m.Value))
dic2.Add(m.Value, "¥¥f0¥¥fs32" + new string(enc.GetChars(bytes)));
}
}
foreach (string key in dic1.Keys)
rtf = rtf.Replace(key, dic1[key]);
foreach (string key in dic2.Keys)
rtf = rtf.Replace(key, dic2[key]);
return rtf;
}
旧文書番号
81585