Reputation: 49
I am trying to merge several RTF strings into one. I want to keep the format of every input string
Here is my code:
string input1 = "{\\rtf1\\ansi\\ansicpg1250\\deff0\\deflang1029{\\fonttbl{\\f0\\fnil\\fcharset238 Tahoma;}}\r\n{\\*\\generator Riched20 12.0.6556.5000;}\\viewkind4\\uc1\r\n\\pard\\f0\\fs16 St\\'f8edisko 222, ppokji\\'9a\\'9dovna Wustenrot PU 15.20022.0037626\\par\r\nPlatba na fakturu, p\\'f8epolsat na poji\\'9a\\'9dovnu a pln\\'ecn\\'ed na \\'fa\\'e8et klienta.\\par\r\n\\'c8\\'edsklo \\'fa\\'e8tu bude dod\\'e1no\\par\r\n}\r\n\0";
string input2 = "{\\rtf1\\ansi\\ansicpg1250\\uc1\\deff0\\deflang0\\deflangfe0{\\fonttbl{\\f0\\fnil Times New Roman;}{\\f1\\fnil Arial;}{\\f2\\fnil Verdana;}{\\f3\\fnil Webdings;}}{\\colortbl;\\red0\\green0\\blue0;\\red0\\green0\\blue255;\\red0\\green255\\blue255;\\red0\\green255\\blue0;\\red0\\green200\\blue150;\\red255\\green0\\blue255;\\red255\\green0\\blue0;\\red255\\green255\\blue0;\\red255\\green255\\blue255;\\red0\\green0\\blue128;\\red0\\green128\\blue128;\\red0\\green128\\blue0;\\red128\\green0\\blue128;\\red128\\green0\\blue0;\\red128\\green128\\blue0;\\red128\\green128\\blue128;\\red192\\green192\\blue192;\\red255\\green128\\blue64;}\r\n\\pard\\sb0\\sa50\\plain\\f1\\cf1\\b0\\i0\\fs18 Při demontáži lišty LZ dveří zjištěna vada lakování.\\par}";
var richTxtBox = new RichTextBox();
var richTxtBox2 = new RichTextBox();
RichTextBox ricTxtArray = new RichTextBox(); ;
//richTxtBox.Sel
foreach (string text in result)
{
if (text.StartsWith("{")) {
if (!string.IsNullOrWhiteSpace(richTxtBox.Text)) {
richTxtBox.AppendText(Environment.NewLine);
}
richTxtBox2.Rtf = text;
richTxtBox.AppendText(richTxtBox2.Text);
}
else {
if (!string.IsNullOrWhiteSpace(richTxtBox.Text)) {
richTxtBox.AppendText(Environment.NewLine);
}
richTxtBox.AppendText(text);
}
}
return richTxtBox.Rtf;
Currently I am getting something like this on output:
"{\\rtf1\\ansi\\ansicpg1250\\deff0\\nouicompat\\deflang1029{\\fonttbl{\\f0\\fnil\\fcharset238 Microsoft Sans Serif;}{\\f1\\fnil\\fcharset0 Microsoft Sans Serif;}}\r\n{\\*\\generator Riched20 10.0.17763}\\viewkind4\\uc1 \r\n\\pard\\f0\\fs17 St\\'f8edisko 222, ppokji\\'9a\\'9dovna Wustenrot PU 15.20022.0037626\\par\r\nPlatba na fakturu, p\\'f8epolsat na poji\\'9a\\'9dovnu a pln\\'ecn\\f1\\lang1033\\'ed na \\'fa\\f0\\'e8et klienta.\\par\r\n\\'c8\\f1\\'edsklo \\'fa\\f0\\'e8tu bude dod\\f1\\'e1no\\par\r\n\\f0\\lang1029 P\\'f8i demont\\f1\\lang1033\\'e1\\f0\\'9ei li\\'9aty LZ dve\\'f8\\f1\\'ed zji\\f0\\'9at\\'ecna vada lakov\\f1\\'e1n\\'ed.\\f0\\lang1029\\par\r\n}\r\n"
As you can see I had font set to Tahoma in input1, but it's completely missing in the output.
My idea was to create separate paragraphs each with its own formating, but how to do that?
Upvotes: 2
Views: 470
Reputation: 42444
You can't use the AppendText method to add a string with Rtf formatting. As the AppendRtf method doesn't exist, we have to mimic that with setting the SelectionStart
, SelectionLength
and SelectedRtf
.
Here is the complete code to achieve what you need:
string MergeRtf(params string[] input)
{
var richTextBox1 = new RichTextBox();
if (input!=null && input.Length>0)
{
richTextBox1.Rtf = input[0]; // first input
for(int i=1; i<input.Length; i++)
{
richTextBox1.AppendText("\r\n"); // add a paragraph break
// set the Selection to the end of the Control
richTextBox1.SelectionStart = richTextBox1.TextLength;
// Nothing is selected
richTextBox1.SelectionLength = 0;
// AppenRtf
richTextBox1.SelectedRtf = input[i]; // any subsequent input
}
}
return richTextBox1.Rtf;
}
You can test it from LinqPad with this testrig:
void Main()
{
string input1 = "{\\rtf1\\ansi\\ansicpg1250\\deff0\\deflang1029{\\fonttbl{\\f0\\fnil\\fcharset238 Tahoma;}}\r\n{\\*\\generator Riched20 12.0.6556.5000;}\\viewkind4\\uc1\r\n\\pard\\f0\\fs16 St\\'f8edisko 222, ppokji\\'9a\\'9dovna Wustenrot PU 15.20022.0037626\\par\r\nPlatba na fakturu, p\\'f8epolsat na poji\\'9a\\'9dovnu a pln\\'ecn\\'ed na \\'fa\\'e8et klienta.\\par\r\n\\'c8\\'edsklo \\'fa\\'e8tu bude dod\\'e1no\\par\r\n}\r\n\0";
string input2 = "{\\rtf1\\ansi\\ansicpg1250\\uc1\\deff0\\deflang0\\deflangfe0{\\fonttbl{\\f0\\fnil Times New Roman;}{\\f1\\fnil Arial;}{\\f2\\fnil Verdana;}{\\f3\\fnil Webdings;}}{\\colortbl;\\red0\\green0\\blue0;\\red0\\green0\\blue255;\\red0\\green255\\blue255;\\red0\\green255\\blue0;\\red0\\green200\\blue150;\\red255\\green0\\blue255;\\red255\\green0\\blue0;\\red255\\green255\\blue0;\\red255\\green255\\blue255;\\red0\\green0\\blue128;\\red0\\green128\\blue128;\\red0\\green128\\blue0;\\red128\\green0\\blue128;\\red128\\green0\\blue0;\\red128\\green128\\blue0;\\red128\\green128\\blue128;\\red192\\green192\\blue192;\\red255\\green128\\blue64;}\r\n\\pard\\sb0\\sa50\\plain\\f1\\cf1\\b0\\i0\\fs18 Při demontáži lišty LZ dveří zjištěna vada lakování.\\par}";
var total = MergeRtf(input1,input2);
total.Dump("Rtf");
}
And this will be your output:
{\rtf1\ansi\ansicpg1250\deff0\deflang1029{\fonttbl{\f0\fnil\fcharset238 Tahoma;}{\f1\fnil\fcharset0 Arial;}{\f2\fnil Arial;}} {\colortbl ;\red0\green0\blue0;} \viewkind4\uc1\pard\f0\fs16 St\'f8edisko 222, ppokji\'9a\'9dovna Wustenrot PU 15.20022.0037626\par Platba na fakturu, p\'f8epolsat na poji\'9a\'9dovnu a pln\'ecn\'ed na \'fa\'e8et klienta.\par \'c8\'edsklo \'fa\'e8tu bude dod\'e1no\par \pard\sa50\cf1\lang0\f1\fs18 Pri demont\'e1\'9ei li\'9aty LZ dver\'ed zji\'9atena vada lakov\'e1n\'ed.\f2\par \pard\cf0\lang1029\f0\fs16\par }
Notice how the fonttbl only contains Tahoma and Arial in the final output. The extra fonts that are present in input2 are not preserved because only the Arial font is selected for text in the Rtf that is found in your input2 variable.
Upvotes: 3