How to mail merge Word document in Xamarin.iOS
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.iOS.
Steps to Mail merge Word document programmatically:
- Create a new C# Xamarin iOS application project.
- Select a project template and select minimum iOS version and Device Support for the application.
- Install Syncfusion.Xamarin.DocIO NuGet package as a reference to the .NET Standard project in your Xamarin application from NuGet.org.
- In project add new UIViewController class.
i)In AppDelegate.cs add the following code on FinishedLaunching() to load the UIViewController1 at top of the window.
C#
//Load the UIViewController for UI Window Window.RootViewController = new UIViewController1();
ii)In UIViewController1.cs add the following code in the ViewDidLoad() method to add the button in the UIView.
C#
var btn = UIButton.FromType(UIButtonType.System); btn.Frame = new CoreGraphics.CGRect(20, 200, 280, 44); btn.SetTitle("Generate Word Document", UIControlState.Normal); btn.TouchUpInside += OnButtonClicked; View.AddSubview(btn);
- Include the following namespace in the UIViewController1.cs file.
C#
using Syncfusion.DocIO; using Syncfusion.DocIO.DLS; using System.Reflection;
- In the click event method (OnButtonClicked) add the following code to mail merge Word document and save it in a stream.
C#
//"UIViewController1" is the class of Portable project. Assembly assembly= typeof(UIViewController1).GetTypeInfo().Assembly; //Opens the Word template document using (WordDocument document = new WordDocument(assembly.GetManifestResourceStream("Blank.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); //Create a new memory stream to save the image MemoryStream stream = new MemoryStream(); //Save Word file into memory stream document.Save(stream, FormatType.Docx); //Create an instance for SaveIOS SaveIOS saveIOS = new SaveIOS(); saveIOS.SaveAndView("Result.docx", "application/msword", stream); }
- Add the SaveIOS class to the project where the stream will be saved to a physical file and the file can be opened for viewing.
The code for SaveIOS class has been given below.
C#
class SaveIOS { //Method to save document as a file and view the saved document public async Task SaveAndView(string filename, string contentType, MemoryStream stream) { //Get the root path in iOS device. string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal); string filePath = Path.Combine(path, filename); //Create a file and write the stream into it. FileStream fileStream = File.Open(filePath, FileMode.Create); stream.Position = 0; stream.CopyTo(fileStream); fileStream.Flush(); fileStream.Close(); //Invoke the saved document for viewing UIViewController currentController = UIApplication.SharedApplication.KeyWindow.RootViewController; while (currentController.PresentedViewController != null) currentController = currentController.PresentedViewController; UIView currentView = currentController.View; QLPreviewController qlPreview = new QLPreviewController(); QLPreviewItem item = new QLPreviewItemBundle(filename, filePath); qlPreview.DataSource = new PreviewControllerDS(item); currentController.PresentViewController(qlPreview, true, null); } }
To preview the Word document in QuickLook, PreviewControllerDS, QLPreviewItemFileSystem and QLPreviewItemBundle classes must be added in the project. The code for these helper classes can be found from this link (iOS -> PreviewControllerDS.cs).
- Compile and execute the application. Now this application mail merge Word document.
A complete working example to mail merge Word document in Xamarin.iOS can be downloaded from mail merge Word document.zip.
By executing the program, you will get the Word document as follows.
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.
See Also:
Mail Merge Word document in Xamarin.Android
Mail Merge Word document in Xamarin
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 MVC
Mail Merge Word document in ASP.NET
Mail Merge Word document in ASP.NET Core
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.