Home > Back-end >  Problem replacing string in RTF file - C#
Problem replacing string in RTF file - C#

Time:09-30

i'm facing a problem with Rich Text Format. I need to replace some Tags with their values. In my case i need to replace the Tag \itemNo\ with the word ITEM0003

Instead of writing ITEM0003, it writes an empty value! I tried to remove the backslashes from Tag and the result is \ITEM0003\ . So i think that the problem are backslashes

But i can't remove that, because i have hundreds of file with the same tag.

Any idea? Here the code

string input = @"\itemNo\";
string value = "ITEM0003";

string pathTemplate = @"c:\temp\template\CAT.rtf";
string pathGenerazione = @"c:\temp\generated\CAT.rtf";
       
RichTextBox _rtf = new RichTextBox();
_rtf.LoadFile(pathTemplate);
_rtf.Rtf = _rtf.Rtf.Replace(input, value);  
_rtf.SaveFile(pathGenerazione);

CodePudding user response:

Please use string input = @"\\itemNo\\";.

Demo:

![enter image description here

Output:

![enter image description here

  • Related