Articles in this section
Category / Section

How to create Word document in Xamarin?

3 mins read

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
  2. .
    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.DocI 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 mergemerge 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.


Conclusion

I hope you enjoyed learning about how to create Word document in Xamarin.

You can refer to our Xamarin feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!


 












Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied