简体   繁体   English

将RichTextBox保存到RTF文件后,保持文本位置

[英]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. 我正在尝试通过richtextbox内容(从RTF文件加载)创建一些RTF文件,并通过一些代码打开文件并应用一些更改并将新文件(输出)保存回另一个位置。 i find that positions text have changed and also font colors. 我发现位置文字已更改,字体颜色也已更改。 please see attached captures for more clarifications. 请查看附件截图以获取更多说明。 input: 输入:

输入

output: 输出: 输出

desired ouput 期望的输出

在此处输入图片说明

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. 编辑:将属性.text更改为rtf之后,此处将输出改善。 still some details to fix: font color, and some deplaced ( i dont know why ) strings, please see date:$datum 仍然有一些要修复的细节:字体颜色和一些替换的字符串(我不知道为什么),请参见日期:$ datum 在此处输入图片说明

EDIT: solution suggestion by @TaW: output: 编辑:@TaW的解决方案建议:输出: 在此处输入图片说明

compilation error: 编译错误: 在此处输入图片说明

我认为您应该通过将“ richTextBox1.Text”更改为“ richTextBox1.RTF”来将文本格式更改为RTF格式

You must never change the Text directly after any formatting has been applied to the RichText. 在将任何格式应用于RichText之后, 切勿直接更改Text Instead you need to work strictly on the SelectedText using Copy , Paste , Append and the other specialized methods. 相反,您需要使用CopyPasteAppend和其他专门方法严格处理SelectedText

So at the very least you need to write an appropriate Replace function! 因此,至少您需要编写一个适当的Replace函数!

Maybe, with a little luck, not working on the Text but the Rtf property will also help, but it may be interwoven with formatting stuff that might be in the way.. 也许有点运气,虽然不能在Text上工作,但Rtf属性也会有所帮助,但是它可能与可能妨碍格式化的东西交织在一起。

Here is a function that will to do it : 这是一个可以做到的功能:

void RtfReplace(RichTextBox RTB, Match match, string replace,  ref int offset)
{
    RTB.Select(match.Index + offset, match.Length);
    RTB.SelectedText = replace;
    offset += replace.Length - match.Length;
}

And a few respective calls 还有几个电话

int offset = 0;
foreach (Match match in Regex.Matches(richTextBox1.Text, "(\\$\\w+)"))
{
    if (match.Groups[1].Value.Substring(1).Equals("Add1"))
        RtfReplace(richTextBox1, match, getAdress1(nums_res[j].ToString()), ref offset); 
    if (match.Groups[1].Value.Substring(1).Equals("Add2"))
        RtfReplace(richTextBox1, match, getAdress2(nums_res[j].ToString()), ref offset); 
    //..

Update : Since each replacement is likely to change the length of the replaced piece of text we will need to adjust all positions in the matches collection by that difference (or repeatedly call the whole replacement code until no more matches are found.) The fix is simple, though.. 更新 :由于每次替换都有可能更改替换文本的长度,因此我们需要根据该差异来调整匹配集合中的所有位置(或重复调用整个替换代码,直到找不到更多匹配为止。)不过简单

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM