ComponentOne Word for WPF
Quick Start: Word for WPF / Step 2 of 3: Adding Simple Text
In This Topic
    Step 2 of 3: Adding Simple Text
    In This Topic

    While you are still in code view in the Visual Studio project, add the following code within the btnText_Click event:

    ' create document
    Dim word = New C1WordDocument()
    word.Clear()
    
    ' measure and show some text 
    Dim text = "Hello!! This is a sample text."
    Dim font = New Font("Segoe UI Light", 20, RtfFontStyle.Italic)
    
    ' add paragraph
    word.AddParagraph(text, font, Colors.BlueViolet, RtfHorizontalAlignment.Justify)
    
    ' get stream to save to
    Dim dlg = New SaveFileDialog()
    dlg.FileName = "document"
    dlg.DefaultExt = ".docx"
    Dim dr = dlg.ShowDialog()
    If Not dr.HasValue OrElse Not dr.Value Then
            Return
    End If
    
    ' save document
    Using stream = dlg.OpenFile()
            word.Save(stream, If(dlg.FileName.ToLower().EndsWith(".docx"), FileFormat.OpenXml, FileFormat.Rtf))
    End Using
    MessageBox.Show("Word Document saved to " + dlg.SafeFileName)
    
    // create document
    var word = new C1WordDocument();
    word.Clear();
    
    // measure and show some text 
    var text = "Hello!! This is a sample text.";
    var font = new Font("Segoe UI Light", 20, RtfFontStyle.Italic);
    
    // add paragraph
    word.AddParagraph(text, font, Colors.BlueViolet, RtfHorizontalAlignment.Justify);
    
    // get stream to save to
    var dlg = new SaveFileDialog();
    dlg.FileName = "document";
    dlg.DefaultExt = ".docx";
    var dr = dlg.ShowDialog();
    if (!dr.HasValue || !dr.Value) {
      return;
    }
    
    // save document
    using(var stream = dlg.OpenFile()) {
      word.Save(stream, dlg.FileName.ToLower().EndsWith(".docx") ? FileFormat.OpenXml : FileFormat.Rtf);
    }
    MessageBox.Show("Word Document saved to " + dlg.SafeFileName);
    

    In the above code, a word document is created with some text is added to it using AddParagraph method. It allows you to save the created document to the location of your choice. You can select the desired location and if you want, you can change the name of the document as well. The document is then saved with the name, document.rtf or the name you provided it.