How to insert text into a text document from Rich Text Box

I have a rich text box in my C # application and it contains different lines of text (from one to many lines). I want to get these lines and paste them into a Word document with the font and font size that I want. Here's how it should look.

NOTES:

  • Line 1 from rich text box
  • Line 2 of a rich text box
  • Line 3 of a rich text box

I want this to happen using the interop library.

I need some sample code for this since I'm a beginner

+3
source share
1 answer

You can use the OpenXML SDK:

http://www.microsoft.com/en-us/download/details.aspx?id=5124

And then do the following:

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

const string fileName = @"C:\YourFile.docx";
string dataToInsert = txtYourRichTextBox.Text;

using (var document = WordprocessingDocument.Open(fileName, true))
{
   var doc = document.MainDocumentPart.Document;
   document.Body.Append(dataToInsert);
   document.Save();
}
0
source

All Articles