Public Sub RenderAll() RenderColorTable(Me.ColorTable) End Sub Public Sub RenderColorTable(ByVal table As ColorTable) Dim newTable As String = table.ToString 'ColorTableの存在チェック Dim match = System.Text.RegularExpressions.Regex.Match(Me.Rtf, ColorTable.RegExPattern) If match.Success Then 'カラーテーブルが存在する場合は置換 Me.Rtf = System.Text.RegularExpressions.Regex.Replace(Me.Rtf, ColorTable.RegExPattern, newTable) Else '存在しない場合は挿入 Dim pos As String = Me.Rtf.IndexOf("}}") 'これでいいんだろうか?RTFの仕様がよくわからない。 Me.Rtf = Me.Rtf.Insert(pos + 2, newTable) End If End Sub Public Overrides Function ToString() As String RenderAll() Return Me.Rtf End Function Shared Narrowing Operator CType(ByVal value As Rtf.Document) As String Return value.ToString End Operator End Class 'ColorTableの例 ' {\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255; '\red0\green255\blue0; '\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0; '\red255\green255\blue255;\red0\green0\blue128;\red0\green128\blue128; '\red0\green128\blue0;\red128\green0\blue128;\red128\green0\blue0; '\red128\green128\blue0;\red128\green128\blue128;\red192\green192\blue192;} Public Class ColorTable Inherits List(Of Color) Public Const RegExPattern As String = "\{\\colortbl\s*?;(.*?)\}" 'Public Colors As New List(Of Color) Public Sub New(ByVal rtf As String) 'ColorTable部分の抜出 Dim pattern As String = RegExPattern Dim match = System.Text.RegularExpressions.Regex.Match(rtf, pattern) If Not match.Success Then Return End If Dim entries As New List(Of String)(Split(match.Groups(1).Value, ";")) pattern = "\\red(?<red>[0-9]+)\\green(?<green>[0-9]+)\\blue(?<blue>[0-9]+)" For Each entry In entries If Len(entry) = 0 Then Continue For End If 'このときentryは次のような形式 '\red0\green255\blue0 Dim params = System.Text.RegularExpressions.Regex.Match(entry, pattern) Me.Add(Color.FromArgb(params.Groups("red").Value, params.Groups("green").Value, params.Groups("blue").Value)) Next End Sub Public Overrides Function ToString() As String '{\colortbl ;\red0\green0\blue255;\red255\green0\blue0;} Dim result As New System.Text.StringBuilder(16) For Each Color In Me result.Append(";\red" & Color.R & "\green" & Color.G & "\blue" & Color.B) Next If result.Length = 0 Then Return "" End If Return "{\colortbl " & result.ToString & ";}" End Function End Class End Namespace