Open and Save Word document in WinUI
8 Dec 202310 minutes to read
Syncfusion DocIO is a WinUI Word library used to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can open and save a Word document in WinUI.
Prerequisites:
To use the WinUI 3 project templates, install the Windows App SDK extension for Visual Studio. For more details, refer here.
WinUI Desktop app
Step 1: Create a new C# WinUI Desktop app. Select Blank App, Packaged with WAP (WinUI 3 in Desktop) from the template and click the Next button.
Step 2: Enter the project name and click Create.
Step 3: Set the Target version to Windows 10, version 2004 (build 19041) and the Minimum version to Windows 10, version 1809 (build 17763) and then click OK.
Step 4: Install the Syncfusion.DocIO.NET NuGet package as a reference to your project from the NuGet.org.
NOTE
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, you also have to add “Syncfusion.Licensing” assembly reference and include a license key in your projects. Please refer to this link to know about registering a Syncfusion license key in your application to use our components.
Step 5: Add a new button to the MainWindow.xaml as shown below.
<Window
x:Class="Open_and_save_Word_document.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Open_and_save_Word_document"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button x:Name="button" Click="OpenAndSaveDocument">Open and save Word document</Button>
</StackPanel>
</Window>
Step 6: Include the following namespaces in the MainWindow.xaml.cs file.
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
Step 7: Add a new action method OpenAndSaveDocument in MainWindow.xaml.cs and include the below code snippet to open an existing Word document in WinUI Desktop app.
private async void OnButtonClicked(object sender, RoutedEventArgs e)
{
//Load an existing Word document.
Assembly assembly = typeof(App).GetTypeInfo().Assembly;
using (WordDocument document = new WordDocument(assembly.GetManifestResourceStream("Open_and_save_Word_document.Assets.Input.docx"), FormatType.Docx))
{
}
}
Step 8: Add below code example to add a paragraph in the Word document.
//Access the section in a Word document.
IWSection section = document.Sections[0];
//Add a new paragraph to the section.
IWParagraph paragraph = section.AddParagraph();
paragraph.ParagraphFormat.FirstLineIndent = 36;
paragraph.BreakCharacterFormat.FontSize = 12f;
IWTextRange text = paragraph.AppendText("In 2000, Adventure Works Cycles bought a small manufacturing plant, Importadores Neptuno, located in Mexico. Importadores Neptuno manufactures several critical subcomponents for the Adventure Works Cycles product line. These subcomponents are shipped to the Bothell location for final product assembly. In 2001, Importadores Neptuno, became the sole manufacturer and distributor of the touring bicycle product group.");
text.CharacterFormat.FontSize = 12f;
Step 9: Add below code example to save the Word document in WinUI Desktop app. Refer the helper methods to save the document in WinUI Desktop App from here.
//Save a Word document to the stream.
using MemoryStream outputStream = new();
document.Save(outputStream, FormatType.Docx);
//Save the stream as a Word document file in the local machine.
Save(outputStream, "Sample.docx");
You can download a complete working sample from GitHub.
By executing the program, you will get the Word document as follows.
Click here to explore the rich set of Syncfusion Word library (DocIO) features.
WinUI UWP app
Step 1: Create a new C# WinUI UWP app. Select Blank App (WinUI 3 in UWP)from the template and click the Next button.
NOTE
To get the UWP Experimental project templates and build UWP apps with WinUI 3, you should download the Windows App SDK Experimental Extension for Visual Studio.
Step 2: Enter the project name and click Create.
Step 3: Set the Target version to Windows 10, version 2004 (build 19041) and the Minimum version to Windows 10, version 1809 (build 17763) and then click OK.
Step 4: Install the Syncfusion.DocIO.NET NuGet package as a reference to your project from the NuGet.org.
NOTE
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, you also have to add “Syncfusion.Licensing” assembly reference and include a license key in your projects. Please refer to this link to know about registering a Syncfusion license key in your application to use our components.
Step 5: Add a new button in the MainPage.xaml as shown below.
<Page
x:Class="Open_and_save_Word_document.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Open_and_save_Word_document"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button x:Name="button" Click="OpenAndSaveDocument">Open and save Word document</Button>
</StackPanel>
</Page>
Step 6: Include the following namespaces in the MainPage.xaml.cs file.
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIO;
Step 7: Add a new action method OpenAndSaveDocument in MainPage.xaml.cs and include the below code snippet to open an existing Word document in WinUI UWP app.
private async void OnButtonClicked(object sender, RoutedEventArgs e)
{
//Load an existing Word document.
Assembly assembly = typeof(App).GetTypeInfo().Assembly;
using (WordDocument document = new WordDocument(assembly.GetManifestResourceStream("Open_and_save_Word_document.Assets.Input.docx"), FormatType.Docx))
{
}
}
Step 8: Add below code example to add a paragraph in the Word document.
//Access the section in a Word document.
IWSection section = document.Sections[0];
//Add a new paragraph to the section.
IWParagraph paragraph = section.AddParagraph();
paragraph.ParagraphFormat.FirstLineIndent = 36;
paragraph.BreakCharacterFormat.FontSize = 12f;
IWTextRange text = paragraph.AppendText("In 2000, Adventure Works Cycles bought a small manufacturing plant, Importadores Neptuno, located in Mexico. Importadores Neptuno manufactures several critical subcomponents for the Adventure Works Cycles product line. These subcomponents are shipped to the Bothell location for final product assembly. In 2001, Importadores Neptuno, became the sole manufacturer and distributor of the touring bicycle product group.");
text.CharacterFormat.FontSize = 12f;
Step 9: Add below code example to save the Word document in WinUI UWP app.
//Save the Word document to the MemoryStream.
using (MemoryStream stream = new MemoryStream())
{
document.Save(stream, FormatType.Docx);
//Save the stream as a Word document file in the local machine.
Save(stream, "Sample.docx");
}
Step 10: Add below code example to save the Word document as a physical file and open the file for viewing.
async void Save(MemoryStream streams, string filename)
{
streams.Position = 0;
StorageFile stFile;
if (!(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")))
{
FileSavePicker savePicker = new FileSavePicker();
savePicker.DefaultFileExtension = ".docx";
savePicker.SuggestedFileName = filename;
savePicker.FileTypeChoices.Add("Word Documents", new List<string>() { ".docx" });
stFile = await savePicker.PickSaveFileAsync();
}
else
{
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
stFile = await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
}
if (stFile != null)
{
using (IRandomAccessStream zipStream = await stFile.OpenAsync(FileAccessMode.ReadWrite))
{
//Write compressed data from memory to file.
using (Stream outstream = zipStream.AsStreamForWrite())
{
byte[] buffer = streams.ToArray();
outstream.Write(buffer, 0, buffer.Length);
outstream.Flush();
}
}
}
//Launch the saved Word file.
await Windows.System.Launcher.LaunchFileAsync(stFile);
}
By executing the program, you will get the Word document as follows.
Click here to explore the rich set of Syncfusion Word library (DocIO) features.