Wpf 将文本替换为 RichTextBox
Wpf Replace Text into RichTextBox
我在 WPF C# 工作,我想将文本替换为 RichtextBox
我加载了包含图片的 rtf 文件,它工作正常。
如果我使用这个:
ReposLettresFusion m_ReposLettresFusion = new ReposLettresFusion();
TextRange range = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
FileStream fStream = new FileStream(@pPath, FileMode.Create);
range.Text = m_ReposLettresFusion.Fusion(pPkGuidLettre, range.Text);
range.Save(fStream, DataFormats.Rtf);
fStream.Close();
rtb.LoadRtf(pPath);
m_ReposLettresFusion = null;
range = null;
它改变了我的文字,但我丢失了我的图片和字体的所有格式。
我如何替换文本并保留 Rtf 的所有格式
谢谢
试试这个:
public void Fusion(ref Xceed.Wpf.Toolkit.RichTextBox pRichTextControl)
{
foreach (KeyValuePair<string, string> entry in LettreFusion)
{
string keyword = entry.Key;
string newString = entry.Value;
TextRange text = new TextRange(pRichTextControl.Document.ContentStart, pRichTextControl.Document.ContentEnd);
TextPointer current = text.Start.GetInsertionPosition(LogicalDirection.Forward);
while (current != null)
{
string textInRun = current.GetTextInRun(LogicalDirection.Forward);
if (!string.IsNullOrWhiteSpace(textInRun))
{
int index = textInRun.IndexOf(keyword);
if (index != -1)
{
TextPointer selectionStart = current.GetPositionAtOffset(index, LogicalDirection.Forward);
TextPointer selectionEnd = selectionStart.GetPositionAtOffset(keyword.Length, LogicalDirection.Forward);
TextRange selection = new TextRange(selectionStart, selectionEnd);
selection.Text = newString;
pRichTextControl.Selection.Select(selection.Start, selection.End);
pRichTextControl.Focus();
}
}
current = current.GetNextContextPosition(LogicalDirection.Forward);
}
}
}
我在 WPF C# 工作,我想将文本替换为 RichtextBox 我加载了包含图片的 rtf 文件,它工作正常。
如果我使用这个:
ReposLettresFusion m_ReposLettresFusion = new ReposLettresFusion();
TextRange range = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
FileStream fStream = new FileStream(@pPath, FileMode.Create);
range.Text = m_ReposLettresFusion.Fusion(pPkGuidLettre, range.Text);
range.Save(fStream, DataFormats.Rtf);
fStream.Close();
rtb.LoadRtf(pPath);
m_ReposLettresFusion = null;
range = null;
它改变了我的文字,但我丢失了我的图片和字体的所有格式。
我如何替换文本并保留 Rtf 的所有格式
谢谢
试试这个:
public void Fusion(ref Xceed.Wpf.Toolkit.RichTextBox pRichTextControl)
{
foreach (KeyValuePair<string, string> entry in LettreFusion)
{
string keyword = entry.Key;
string newString = entry.Value;
TextRange text = new TextRange(pRichTextControl.Document.ContentStart, pRichTextControl.Document.ContentEnd);
TextPointer current = text.Start.GetInsertionPosition(LogicalDirection.Forward);
while (current != null)
{
string textInRun = current.GetTextInRun(LogicalDirection.Forward);
if (!string.IsNullOrWhiteSpace(textInRun))
{
int index = textInRun.IndexOf(keyword);
if (index != -1)
{
TextPointer selectionStart = current.GetPositionAtOffset(index, LogicalDirection.Forward);
TextPointer selectionEnd = selectionStart.GetPositionAtOffset(keyword.Length, LogicalDirection.Forward);
TextRange selection = new TextRange(selectionStart, selectionEnd);
selection.Text = newString;
pRichTextControl.Selection.Select(selection.Start, selection.End);
pRichTextControl.Focus();
}
}
current = current.GetNextContextPosition(LogicalDirection.Forward);
}
}
}