First I'll have to admit that my problem is not actually in converting FlowDocument ToUpper, but it's verry similar. In my language we have two scripts (alphabets) that are equal, and are a thing of user preference. Converting between them is simple since each character has it's replacement in the other alphabet and vice versa. So it's similar problem to converting a text to use all capital letters.
Anyway I want to provide my users with the ability to convert the contents of the RichTextBox between these scripts (or if it's easier for you to understand, between cases), WITHOUT loosing the formatting. Unfortunately the code I re-purposed from the Internet is not working properly.
FlowDocument forConversion = MakeACopy(TxtBoxOrig.Document); /* Get plaintext ranges */ List<TextRange> textRanges = new List<TextRange>(); TextPointer previousPointer = forConversion.ContentStart; for (TextPointer pointer = forConversion.ContentStart; pointer != null && pointer.CompareTo(forConversion.ContentEnd) <= 0; pointer = pointer.GetPositionAtOffset(1, LogicalDirection.Forward)) { var contextAfter = pointer.GetPointerContext(LogicalDirection.Forward); var contextBefore = pointer.GetPointerContext(LogicalDirection.Backward); if (contextBefore != TextPointerContext.Text && contextAfter == TextPointerContext.Text) { previousPointer = pointer; } if (contextBefore == TextPointerContext.Text && contextAfter != TextPointerContext.Text && previousPointer != pointer) { textRanges.Add(new TextRange(previousPointer, pointer)); } } /* Do the conversion */ foreach (var str in textRanges) { str.Text = str.Text.ToUpper(); //Convertor.FromLatinScript(str.Text); } TxtBoxEnd.Document = forConversion;
It converts a FlowDocument like this:
<FlowDocument PagePadding="5,0,5,0" AllowDrop="True" NumberSubstitution.CultureSource="User" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"><Paragraph>
<Span FontFamily="Calibri" FontSize="18.6666666666667" xml:lang="sr-latn-rs" xml:space="preserve">This is </Span>
<Span FontFamily="Calibri" FontWeight="Bold" FontSize="18.6666666666667" xml:lang="sr-latn-rs">a test</Span>
<Span FontFamily="Calibri" FontSize="18.6666666666667" xml:lang="sr-latn-rs">. Test.</Span>
</Paragraph></FlowDocument>
into this:
<FlowDocument PagePadding="5,0,5,0" AllowDrop="True" NumberSubstitution.CultureSource="User" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Paragraph>
<Span FontFamily="Calibri" FontSize="18.6666666666667" xml:lang="sr-latn-rs">THIS IS A TEST. TEST.</Span>
<Span FontFamily="Calibri" FontWeight="Bold" FontSize="18.6666666666667" xml:lang="sr-latn-rs" />
<Span FontFamily="Calibri" FontSize="18.6666666666667" xml:lang="sr-latn-rs" />
</Paragraph></FlowDocument>
Notice how all text has been put into one span and thus the formatting is lost. And it's not what I told it to do, since I only replaced the plain text bits.
Sooo, where did I mess up, and what are my options.
As allways I'm attaching a test project to this question.