vez25
vez25

Reputation: 49

Problem replacing string in RTF file - C#

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);

Upvotes: 0

Views: 684

Answers (1)

Jiale Xue - MSFT
Jiale Xue - MSFT

Reputation: 3670

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

Demo:

![enter image description here

Output:

![enter image description here

Upvotes: 1

Related Questions