C#互操作字-从字符串读取RTF数据

C#互操作字-从字符串读取RTF数据,c#,ms-word,load,interop,word-2003,C#,Ms Word,Load,Interop,Word 2003,我有一个字符串str中的RTF数据,我希望将该数据加载到MS word对象中。我见过Documents.Open()方法,但它需要一个物理文件路径来读取特定文件,而我没有这样的文件 如何打开Word的新实例并在其中加载RTF数据 Microsoft.Office.Interop.Word.ApplicationClass wordapp = new ApplicationClass(); wordapp.Visible = false; string str = @"{\rtf1\ansi\a

我有一个字符串
str
中的RTF数据,我希望将该数据加载到MS word对象中。我见过
Documents.Open()
方法,但它需要一个物理文件路径来读取特定文件,而我没有这样的文件

如何打开Word的新实例并在其中加载RTF数据

Microsoft.Office.Interop.Word.ApplicationClass wordapp = new ApplicationClass();
wordapp.Visible = false;

string str = @"{\rtf1\ansi\ansicpg1252\uc1\deff0{\fonttbl{\f0\fnil\fcharset0\fprq2 Arial;}{\f1\fswiss\fcharset0\fprq2 Arial;}{\f2\froman\fcharset2\fprq2 Symbol;}}
{\colortbl;}{\stylesheet{\s0\itap0\nowidctlpar\f0\fs24 [Normal];}{\*\cs10\additive Default Paragraph Font;}}{\*\generator TX_RTF32 17.0.540.502;}
\paperw12240\paperh15840\margl1138\margt1138\margr1138\margb1138\deftab1134\widowctrl\formshade\sectd\headery720\footery720\pgwsxn12240\pghsxn15840\marglsxn1138\margtsxn1138\margrsxn1138\margbsxn1138\pgbrdropt32\pard\itap0\nowidctlpar\plain\f1\fs20 test1\par }";
我将在word中进行一些格式化,但是applicationClass应该是
wordapp.Visible=false


更新:我不希望文件保存在系统上。在不将RTF文本保存在物理内存中的情况下,是否仍可以读取和工作?

与其尝试将RTF文本加载到Word中,不如将其加载到文本文件中(因为RTF文件无论如何都是纯文本),然后将原始RTF文本文件作为新的Word文档打开。这会导致Word处理RTF文本并将其格式化为Word文档(带有字体/边距/等等),我认为这是您要查找的,而不是逐字查看RTF文本。一旦它是一个新的Word文档,您可以进行操作,然后保存到Word文档文件:

        string filePath = @"c:\rawRtfText.rtf";

        //write the raw RTF string to a text file.
        System.IO.StreamWriter rawTextFile = new System.IO.StreamWriter(filePath, false);
        string str = @"{\rtf1\ansi\ansicpg1252\uc1\deff0{\fonttbl{\f0\fnil\fcharset0\fprq2 Arial;}{\f1\fswiss\fcharset0\fprq2 Arial;}{\f2\froman\fcharset2\fprq2 Symbol;}}{\colortbl;}{\stylesheet{\s0\itap0\nowidctlpar\f0\fs24 [Normal];}{\*\cs10\additive Default Paragraph Font;}}{\*\generator TX_RTF32 17.0.540.502;}\paperw12240\paperh15840\margl1138\margt1138\margr1138\margb1138\deftab1134\widowctrl\formshade\sectd\headery720\footery720\pgwsxn12240\pghsxn15840\marglsxn1138\margtsxn1138\margrsxn1138\margbsxn1138\pgbrdropt32\pard\itap0\nowidctlpar\plain\f1\fs20 test1\par }";
        rawTextFile.Write(str);
        rawTextFile.Close();

        //now open the RTF file using word.
        Microsoft.Office.Interop.Word.Application msWord = new Microsoft.Office.Interop.Word.Application();
        msWord.Visible = false;
        Microsoft.Office.Interop.Word.Document wordDoc = msWord.Documents.Open(filePath);

        //after manipulating the word doc, save it as a word doc.
        object oMissing = System.Reflection.Missing.Value;
        wordDoc.SaveAs(@"c:\RtfConvertedToWord.doc", ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

如果我理解正确,您希望在不使用物理文件的情况下对Rtf数据进行格式化。您可以选中RichTextBox控件以获取this@faheemkhan这是一个好的开始。我可以在richtextbox中读取str。现在我将尝试在word文档的新实例中加载它。您的目标是打印它,还是向用户显示它,然后在不保存的情况下关闭它?或者以更简单的方式操作RTF的格式,然后最终检索表示该格式的RTF数据?也就是说,使用一个单词作为接口来编写RTF,这样您就不必手动操作RTF了,您可以进行诸如设置字体/格式等操作吗?