Search code examples
c#stringdatatableconcatenationrtf

How to concatenate text to rtf formatted string


I have a SQL Server table which has a column of SQL data type text. This column contains rtf formatted text.

I read the table as a DataTable.

I'm trying to get the column value as a string and append it with additional text.

    string rtfContent = SourceRow["TEXT"].ToString();
    rtfContent += "more text";

But somehow, the string does not change. The value more text is not being added.

I tried all possible types of concatenations (that I am aware of):

    string str = string.Concat(rtfContent, "more text");
    str = rtfContent + "more text";
    str = $"{rtfContent} more text";
    str = string.Format("{0}{1}", rtfContent, "more text");

I can add text before the actual rtf string, e.g.:

    string str = "more text" + rtfContent;

But in my case I need both options.

This is my rtfContent:

{\rtf1\ansi\ansicpg1252\deff0\deflang1031{\fonttbl{\f0\fnil\fcharset0 Arial Unicode MS;}}
\viewkind4\uc1\pard\tx568\tx1136\tx1704\tx2272\tx2840\tx3408\tx3976\tx4544\tx5112\tx5680\f0\fs20 Produktbeschreibung:\par
\b Fahrzeugleitung nach DIN 72551\par
\b0 Typ FLRY\par
Isolationsreduziert\par
2,5 mm\'b2\par
5, 25, 100 m Ring\par
rot\par
Hersteller: FREI\par
Verpackungsgewicht: 0.649kg\par
RoHS: konform\par
\par
}

In Visual Studio while debugging:

    //rtfContent: "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1031{\\fonttbl{\\f0\\fnil\\fcharset0 Arial Unicode MS;}}\r\n\\viewkind4\\uc1\\pard\\tx568\\tx1136\\tx1704\\tx2272\\tx2840\\tx3408\\tx3976\\tx4544\\tx5112\\tx5680\\f0\\fs20 Produktbeschreibung:\\par\r\n\\b Fahrzeugleitung nach DIN 72551\\par\r\n\\b0 Typ FLRY\\par\r\nIsolationsreduziert\\par\r\n2,5 mm\\'b2\\par\r\n5, 25, 100 m Ring\\par\r\nrot\\par\r\nHersteller: FREI\\par\r\nVerpackungsgewicht: 0.649kg\\par\r\nRoHS: konform\\par\r\n\\par\r\n}\r\n\0"

Does anyone have an idea why this is not working as expected? Thanks.


Solution

  • After further investigation I found out that the rtf string I retrieved from the database is terminated with "\0" at the end of the string which prevents me from adding any more characters to the string (or at least not showing them visually).

    I got it to work as follows:

    string rtfContent = SourceRow["TEXT"].ToString();
    rtfContent = rtfContent.TrimEnd('\0');
    rtfContent += "more text";
    // result: "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1031{\\fonttbl{\\f0\\fnil\\fcharset0 Arial Unicode MS;}} \\viewkind4\\uc1\\pard\\tx568\\tx1136\\tx1704\\tx2272\\tx2840\\tx3408\\tx3976\\tx4544\\tx5112\\tx5680\\f0\\fs20 Produktbeschreibung:\\par \\b Fahrzeugleitung nach DIN 72551\\par \\b0 Typ FLRY\\par Isolationsreduziert\\par 2,5 mm\\'b2\\par 5, 25, 100 m Ring\\par rot\\par Hersteller: FREI\\par Verpackungsgewicht: 0.649kg\\par RoHS: konform\\par \\par }  more text"