This article is going to introduce how to create, write and save word document in WPF via Spire.Doc for WPF.
Spire.Doc for WPF enables users to do a large range of manipulations (such as create, write, open, edit, convert and save, etc.) on word with high performance and efficiency. In addition, as a powerful and independent library, it doesn’t require Microsoft Office or any other 3rd party tools to be installed on system.
Note: please download and install Spire.Doc correctly and add the dll file from the installation folder as reference.
First, let’s begin to create a word document in WPF.
Use namespace:
using System.Windows; using Spire.Doc; using Spire.Doc.Documents;
Step 1: Create a new word document instance, next add a section and a paragraph to it.
//Create New Word Document doc = new Document(); //Add Section Section section = doc.AddSection(); //Add Paragraph Paragraph Para = section.AddParagraph();
Second, we’re going to write something into the document.
Step 2: Append some text to it.
//Append Text Para.AppendText("Hello! " + "I was created by Spire.Doc for WPF, it's a professional .NET Word component " + "which enables developers to perform a large range of tasks on Word document (such as create, open, write, edit, save and convert " + "Word document) without installing Microsoft Office and any other third-party tools on system.");
Third, save the generated document.
Step 3: Save and launch the document.
//Save and launch doc.SaveToFile("MyWord.docx", FileFormat.Docx); System.Diagnostics.Process.Start("MyWord.docx");
Output:
Full codes:
using Spire.Doc; using Spire.Doc.Documents; using System.Windows; namespace WpfApplication1 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { //Create New Word Document doc = new Document(); //Add Section Section section = doc.AddSection(); //Add Paragraph Paragraph Para = section.AddParagraph(); //Append Text Para.AppendText("Hello! " + "I was created by Spire.Doc for WPF, it's a professional .NET Word component " + "which enables developers to perform a large range of tasks on Word document (such as create, open, write, edit, save and convert " + "Word document) without installing Microsoft Office and any other third-party tools on system."); //Save and launch doc.SaveToFile("MyWord.docx", Spire.Doc.FileFormat.Docx); System.Diagnostics.Process.Start("MyWord.docx"); } } }