Search code examples
c#regexrichtextboxrtf

Keep text positions after saving richtextbox to RTF file


i'm trying to create some RTF files from richtextbox content ( which is load from RTF files ) After opening file by some code and applying some changes and saving back new file (output) to another location. i find that positions text have changed and also font colors. please see attached captures for more clarifications. input:

input

output: output

desired ouput

enter image description here

I think i need to talk a bit about what i apply to input from the code: well i need to replace every variables begining by $ by some variables from databases:

I used this portion of code for that:

foreach (Match match in Regex.Matches(richTextBox1.Text, "(\\$\\w+)"))
                    {
                        if (match.Groups[1].Value.Substring(1).Equals("Add1"))
                            richTextBox1.Text = richTextBox1.Text.Replace(match.Groups[1].Value, getAdress1(nums_res[j].ToString()));
                        if (match.Groups[1].Value.Substring(1).Equals("Add2"))
                            richTextBox1.Text = richTextBox1.Text.Replace(match.Groups[1].Value, getAdress2(nums_res[j].ToString()));
                        if (match.Groups[1].Value.Substring(1).Equals("Add3"))
                            richTextBox1.Text = richTextBox1.Text.Replace(match.Groups[1].Value, getAdress3(nums_res[j].ToString()));
                        if (match.Groups[1].Value.Substring(1).Equals("Add4"))
                            richTextBox1.Text = richTextBox1.Text.Replace(match.Groups[1].Value, getLand(nums_res[j].ToString()));
                        if (match.Groups[1].Value.Substring(1).Equals("Rechnr"))
                            richTextBox1.Text = richTextBox1.Text.Replace(match.Groups[1].Value, nums_res[j]);
                        if (match.Groups[1].Value.Substring(1).Equals("Datum"))
                            richTextBox1.Text = richTextBox1.Text.Replace(match.Groups[1].Value, getDatum(nums_res[j].ToString()));
                        if (match.Groups[1].Value.Substring(1).Equals("resname"))
                            richTextBox1.Text = richTextBox1.Text.Replace(match.Groups[1].Value, getName1(nums_res[j].ToString()));
                        if (match.Groups[1].Value.Substring(1).Equals("resvorname"))
                            richTextBox1.Text = richTextBox1.Text.Replace(match.Groups[1].Value, getVorname(nums_res[j].ToString()));
                        if (match.Groups[1].Value.Substring(1).Equals("resroom"))
                            richTextBox1.Text = richTextBox1.Text.Replace(match.Groups[1].Value, getZimmer(nums_res[j].ToString()));
                        if (match.Groups[1].Value.Substring(1).Equals("anz"))
                            richTextBox1.Text = richTextBox1.Text.Replace(match.Groups[1].Value, getAnz(nums_res[j].ToString()));
                    }

                    int indexToText = richTextBox1.Find(getAdress1(nums_res[j].ToString()));
                    int endIndex = richTextBox1.Find(getAdress1(nums_res[j].ToString()));
                    if(indexToText > 0 && endIndex > 0)
                        richTextBox1.Select(indexToText, endIndex); richTextBox1.SelectionAlignment = System.Windows.Forms.HorizontalAlignment.Center;

                    int indexToText2 = richTextBox1.Find(getLand(nums_res[j].ToString()));
                    int endIndex2 = richTextBox1.Find(getLand(nums_res[j].ToString()));
                    if (indexToText2 > 0 && endIndex2 > 0)
                        richTextBox1.Select(indexToText2, endIndex2);  richTextBox1.SelectionAlignment = System.Windows.Forms.HorizontalAlignment.Center;
                    richTextBox1.SaveFile("d:\\ryadmogadoroutput" + nums_res[j].ToString());
                    i = i + 1;
                    richTextBox1.Clear();

for now i dont replace all of them but just some and i want to know where the difference comes between my output and desired output. ( as u can see, i tried to center first variables from the code)

i'm wondering if theres a way to keep original format of the file after applying the changed i talked about

Thank you

EDIT: After changing the property .text to rtf here the improved output. still some details to fix: font color, and some deplaced ( i dont know why ) strings, please see date:$datum enter image description here

EDIT: solution suggestion by @TaW: output: enter image description here

compilation error: enter image description here


Solution

  • I Think you should change the text format to RTF Format by changing "richTextBox1.Text" to "richTextBox1.RTF"