2013-04-15 3 views
0

문자열에 str의 RTF 데이터가 있고 그 데이터가 MS 워드 개체로로드되기를 바랍니다. Documents.Open() 메서드를 본 적이 있지만 특정 파일을 읽는 데 실제 파일 경로가 필요하며 그러한 파일이 없습니다.C# Interop Word - 문자열에서 RTF 데이터 읽기

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 }"; 

나는 단어에 일부 서식을하고있을 것입니다하지만 applicationClass는 wordapp.Visible = false;

업데이트해야합니다 : 내가 시스템에 저장 될 파일을 선호하지 않을 것입니다. 물리적 메모리에 저장하지 않고 읽고 작업 할 수있는 방법이 없습니까?

+1

내가 당신의 포인트가 올바르게, 당신은 실제 파일없이 RTF 형식의 데이터를 형식화하는 작업을 수행 할 얻었다합니다. 이 –

+0

@faheemkhan RichTextBox 컨트롤을 확인할 수 있습니다. 좋은 시작입니다. richtextbox에서 str을 읽을 수 있습니다. 이제 doc라는 단어의 새로운 인스턴스에서로드하려고 시도 할 것입니다. –

답변

0

RTF 텍스트를 Word에로드하는 대신 RTF 파일이 일반 텍스트이므로 텍스트 파일에로드하는 것이 더 좋습니다. 그런 다음 원시 RTF 텍스트 파일을 새 Word 문서로 엽니 다. 이로 인해 Word에서 RTF 텍스트를 처리하고 글꼴, 여백 등이 포함 된 Word 문서 형식으로 표시됩니다. RTF 텍스트를 그대로 보지 않아도됩니다. 이 새 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); 
+0

문서를 저장하거나 읽는 것을 원하지 않습니다. –

+0

인쇄하거나 사용자에게 보여주고 저장하지 않고 닫는 것과 같은 것을 목표로하고 있습니까? 또는 더 쉬운 방식으로 RTF의 서식을 조작 한 다음 궁극적으로 그 서식을 나타내는 RTF 데이터를 검색 할 수 있습니까? 말하자면, RTF를 손으로 직접 조작 할 필요가 없도록 Word를 인터페이스로 사용하여 글꼴/서식 지정/등을 설정하는 것과 같은 일을하는 것입니다. –