We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to create Word document in Xamarin

Platform: Xamarin.Forms |
Control: DocIO |
Published Date: December 17, 2018 |
Last Revised Date: May 14, 2019

Syncfusion Essential DocIO is a Xamarin Word library used to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can create a Word document in Xamarin.

Steps to create Word document programmatically:

  1. Create a new Xamarin.Forms application project.

Create new Xamarin.Forms application in Visual Studio

  1. Select a project template and required platforms to deploy the application. In this application the portable assemblies to be shared across multiple platforms, the .NET Standard code sharing strategy has been selected. For more details about code sharing refer here.
Note:

If .NET Standard is not available in the code sharing strategy, the Portable Class Library (PCL) can be selected.

Select project template, required platforms, and code sharing strategy

  1. Install Syncfusion.Xamarin.DocIO NuGet package as a reference to the .NET Standard project in your application from NuGet.org.

Add DocIO NuGet package reference to the project

  1. Add new Forms XAML page in portable project. If there is no XAML page is defined in the App class. Otherwise proceed to the next step.

i)To add the new XAML page, right click on the project and select Add > New Item and add a Forms XAML Page from the list. Name it as MainXamlPage.

ii)In App class of portable project (App.cs), replace the existing constructor of App class with the code snippet given below which invokes the MainXamlPage.

C#

public App()
{
    // The root page of your application
    MainPage = new MainXamlPage();
}
  1. In the MainXamlPage.xaml add new button as shown below.

XAML

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="GettingStarted. MainXamlPage">
<StackLayout VerticalOptions="Center">
<Button Text="Generate Document" Clicked="OnButtonClicked" HorizontalOptions="Center"/>
</StackLayout> 
</ContentPage>
  1. Include the following namespace in the MainXamlPage.xaml.cs file.

C#

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
  1. Include the below code snippet in the click event of the button in MainXamlPage.xaml.cs, to create Word document and save it in a stream.

C#

void OnButtonClicked(object sender, EventArgs args)
{
    //Creates a new instance of WordDocument (Empty Word Document)
    using (WordDocument document = new WordDocument())
    {
        //Adds a section and a paragraph to the document
        document.EnsureMinimal();
 
        //Appends text to the last paragraph of the document
        document.LastParagraph.AppendText("Hello World");
 
        MemoryStream stream = new MemoryStream();
 
        //Saves the Word document to  MemoryStream
        document.Save(stream, FormatType.Docx);
 
        
        //Save the stream as a file in the device and invoke it for viewing
        Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Result.docx", "application/msword", stream);
    }
}
  1. Download the helper files from this link and add them into the mentioned project. These helper files allow you to save the stream as a physical file and open the file for viewing.

Project

File Name

Summary

Protable project

ISave.cs

Represent the base interface for save operation

iOS Project

SaveIOS.cs

Save implementation for iOS device

PreviewControllerDS.cs

Helper class for viewing the Word document in iOS device

Android project

SaveAndroid.cs

Save implementation for Android device

WinPhone project

SaveWinPhone.cs

Save implementation for Windows phone device

UWP project

SaveWindows.cs

Save implementation for UWP device.

Windows (8.1) project

SaveWindows81.cs

Save implementation for WinRT device.

  1. Compile and execute the application. Now this application creates a Word document.

A complete working example of how to create Word document in Xamarin can be downloaded from Create-Word-file.zip.

By executing the program, you will get the Word document as follows.

Generated or created Word document

Take a moment to peruse the documentation, where you can find basic Word document processing options along with features like mail merge, merge and split documents, find and replace text in the Word document, and most importantly PDF conversions with code examples.

Explore more about the rich set of Syncfusion Word Framework features.

An online example to generate or create Word document.

See Also:

Create Word document in Xamarin.Android

Create Word document in Windows Forms

Create Word document in WPF

Create Word document in ASP.NET Core

Create Word document in ASP.NET MVC

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile