none
Display Rich Text Format data in word

    Question

  • I have data saved in a SQL database in an nvarchar(max) field type. The stored data is as follow:

    Summary

    {\rtf1\deff0{\fonttbl{\f0 Times New Roman;}}{\colortbl\red0\green0\blue0 ;\red0\green0\blue255 ;}{\*\listtable {\list\listtemplateid-1\listhybrid{\listlevel\levelnfc0\levelnfcn0\leveljc0\leveljcn0\levelfollow0\levelstartat1{\leveltext\leveltemplateid75266416 \'02\'00.;}{\levelnumbers\'01;}\levellegal0\levelnorestart0\cf0\fi-360\li720\lin720}{\listlevel\levelnfc0\levelnfcn0\leveljc0\leveljcn0\levelfollow0\levelstartat1{\leveltext\leveltemplateid960614247 \'02\'01.;}{\levelnumbers\'01;}\levellegal0\levelnorestart0\cf0\fi-360\li1440\lin1440}{\listlevel\levelnfc0\levelnfcn0\leveljc0\leveljcn0\levelfollow0\levelstartat1{\leveltext\leveltemplateid645492893 \'02\'02.;}{\levelnumbers\'01;}\levellegal0\levelnorestart0\cf0\fi-360\li2160\lin2160}{\listlevel\levelnfc0\levelnfcn0\leveljc0\leveljcn0\levelfollow0\levelstartat1{\leveltext\leveltemplateid1294513133 \'02\'03.;}{\levelnumbers\'01;}\levellegal0\levelnorestart0\cf0\fi-360\li2880\lin2880}{\listlevel\levelnfc0\levelnfcn0\leveljc0\leveljcn0\levelfollow0\levelstartat1{\leveltext\leveltemplateid453938919 \'02\'04.;}{\levelnumbers\'01;}\levellegal0\levelnorestart0\cf0\fi-360\li3600\lin3600}{\listlevel\levelnfc0\levelnfcn0\leveljc0\leveljcn0\levelfollow0\levelstartat1{\leveltext\leveltemplateid2028470431 \'02\'05.;}{\levelnumbers\'01;}\levellegal0\levelnorestart0\cf0\fi-360\li4320\lin4320}{\listlevel\levelnfc0\levelnfcn0\leveljc0\leveljcn0\levelfollow0\levelstartat1{\leveltext\leveltemplateid173129563 \'02\'06.;}{\levelnumbers\'01;}\levellegal0\levelnorestart0\cf0\fi-360\li5040\lin5040}{\listlevel\levelnfc0\levelnfcn0\leveljc0\leveljcn0\levelfollow0\levelstartat1{\leveltext\leveltemplateid931962313 \'02\'07.;}{\levelnumbers\'01;}\levellegal0\levelnorestart0\cf0\fi-360\li5760\lin5760}{\listlevel\levelnfc0\levelnfcn0\leveljc0\leveljcn0\levelfollow0\levelstartat1{\leveltext\leveltemplateid1075949278 \'02\'08.;}{\levelnumbers\'01;}\levellegal0\levelnorestart0\cf0\fi-360\li6480\lin6480}{\listname ;}\listid450374152}}{\*\listoverridetable {\listoverride\listid450374152\listoverridecount0\ls1}}{\stylesheet {\ql\cf0 Normal;}{\*\cs1\cf0 Default Paragraph Font;}{\*\cs2\sbasedon1\cf0 Line Number;}{\*\cs3\ul\cf1 Hyperlink;}}\sectd{\listtext\pard 1.\tab }\pard\plain\ilvl0\ls1\ql\fi-360\li720\lin720{\cf0 This is a test}\par{\listtext\pard 2.\tab }\pard\plain\ilvl0\ls1\ql\fi-360\li720\lin720{\b\i\cf0 this is line 2}\b\i\par}

    I am trying to display the content of this field in a word 2010 document using VS office development 2010. I have added the dataset to my project and then dragged the above field after setting it's type to rich text, but when I run the project the output display the data above instead of the proper rich text format.

    Tuesday, November 29, 2011 10:59 PM

Answers

  • Hi ckchouk

    I'm sure I wrote an answer to this a few hours ago. But it seems to have gotten lost in the forum "black hole"... <sigh> I'll try to recreate it

    RTF is not (and never has been) Word's native file format. A converter is required in order to bring RTF content into Word as formatted content. The converter only kicks in when opening or inserting a file, or when pasting from the clipboard.

    It's not clear what you mean by "a field type of rich text", but I'm assuming you mean a Rich Text content control. This does not support RTF. "Rich text" in this context means "formatted text" as opposed to "plain text". But Word's formatted text is in Word's native file format, not RTF.

    Word's native file format for 2007 and 2010 is Word Open XML. Valid Word Open XML can be inserted directly into a document, from memory, using the InsertXML method of the Range or Seleciton object.

    So one thing you could do would be to dynamically transform (XSLT) the RTF to Word Open XML, then insert it into the document.

    Another approach would be to convert all the RTF in the database to Word Open XML. This could be done by inserting it using the converter, then extracting the result using the Range.WordOpenXML property and writing the result back to the database.

    The only other possibility would be to embed a Windows Forms RTF control on the surface of the document. But I have my doubts whether that will be a satisfactory solution...


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bruce Song Wednesday, December 07, 2011 10:33 AM
    Wednesday, November 30, 2011 11:18 PM

All replies

  • Write the text to a text file with extension .rtf, then open the .rtf file in Word (and save it as .doc or .docx, or copy its contents to another document, etc.)
    Regards, Hans Vogelaar
    Tuesday, November 29, 2011 11:22 PM
  • Not sure if that's the best option for what I am trying to accomplish. All content in this word document is bound to a SQL datasource and needs to be automatically created without any saving as or copying and pasting. If there is a field type of rich text when configuring your objects in a datasource of an VS office project, then you would expect the data to display as rich text, I am sure I am missing a step, but I am also sure there must be a way to display rich text data in a word document.

    Wednesday, November 30, 2011 2:09 PM
  • Hi ckchouk

    I'm sure I wrote an answer to this a few hours ago. But it seems to have gotten lost in the forum "black hole"... <sigh> I'll try to recreate it

    RTF is not (and never has been) Word's native file format. A converter is required in order to bring RTF content into Word as formatted content. The converter only kicks in when opening or inserting a file, or when pasting from the clipboard.

    It's not clear what you mean by "a field type of rich text", but I'm assuming you mean a Rich Text content control. This does not support RTF. "Rich text" in this context means "formatted text" as opposed to "plain text". But Word's formatted text is in Word's native file format, not RTF.

    Word's native file format for 2007 and 2010 is Word Open XML. Valid Word Open XML can be inserted directly into a document, from memory, using the InsertXML method of the Range or Seleciton object.

    So one thing you could do would be to dynamically transform (XSLT) the RTF to Word Open XML, then insert it into the document.

    Another approach would be to convert all the RTF in the database to Word Open XML. This could be done by inserting it using the converter, then extracting the result using the Range.WordOpenXML property and writing the result back to the database.

    The only other possibility would be to embed a Windows Forms RTF control on the surface of the document. But I have my doubts whether that will be a satisfactory solution...


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bruce Song Wednesday, December 07, 2011 10:33 AM
    Wednesday, November 30, 2011 11:18 PM