Articles in this section
Category / Section

How to mail merge Word document in Xamarin.Forms

3 mins read

Mail merge is a process of merging data from a data source to a Word template document. Syncfusion Essential DocIO is a Xamarin Word library used to generate reports like invoice, payroll, letter, etc., by performing mail merge faster in a batch process without Microsoft Word or interop dependencies. Using this library, you can mail merge Word document in Xamarin.

Steps to Mail merge Word document programmatically:

  1. Create a new Xamarin.Forms application project. Create new Xamarin.Forms application in Visual Studio
  2. 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.
    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
  2. 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;
using System.Reflection;
  1. Include the below code in the click event of the button in MainXamlPage.xaml.cs, to mail merge Word document and save it in a stream.

C#

void OnButtonClicked(object sender, EventArgs args)
{
    //"App" is the class of Portable project.
    Assembly assembly = typeof(App).GetTypeInfo().Assembly;
    //Opens the Word template document
    using (WordDocument document = new WordDocument(assembly.GetManifestResourceStream("GettingStarted.Assets.Letter Formatting.docx"), FormatType.Docx))
    {
         string[] fieldNames = { "ContactName", "CompanyName", "Address", "City", "Country", "Phone"};
         string[] fieldValues = { "Nancy Davolio", "Syncfusion", "507 - 20th Ave. E.Apt. 2A", "Seattle, WA", "USA", "(206) 555-9857-x5467" };
         //Performs the mail merge
         document.MailMerge.Execute(fieldNames, fieldValues);
        MemoryStream stream = new MemoryStream();
        //Saves the Word file 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

Portable 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 mail merge Word document.

A complete working example of mail merge Word document in Xamarin can be downloaded from mail merge Word document.zip.

By executing the program, you will get the Word document as follows. Mail merge output 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 perform mail merge in Word document.

See Also:

Mail Merge Word document in Xamarin.Android

Mail Merge Word document in Xamarin.iOS

Mail Merge Word document in C#

Mail Merge Word document in Windows Forms

Mail Merge Word document in WPF

Mail Merge Word document in UWP

Mail Merge Word document in ASP.NET

Mail Merge Word document in ASP.NET MVC

Mail Merge Word document in ASP.NET Core

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.

 

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