What is Aspose?
Aspose, a file formats API provider, provides .NET, Java, Android, SharePoint, Reporting Services, and cloud-based APIs for document generation, conversion, and automation. Unlike any 3rd party DLL, you can use Aspose and play with your office documents along with PDF in a much simpler way.
The very best part of this I found is mail merge. To generate any PDF, you have to continue either item by item or you can create your HTML and then convert it into a PDF. Here, in Aspose, we have a method named Merge() through which we can use a Word template and from there, we can generate the PDF with our desired values. And twe can do this by writing only 3-4 lines. It’s that easy.
After using this, the only disappointing thing I found is, Aspose is not free :(
For more Aspose-related information, you can visit here.
So, let’s see how to integrate Aspose in our .NET application and use it to generate PDF from a template.
Integrate Aspose in your .NET application
Aspose is available in NuGet Package Manager library. So, you can install it either from the Package Manager console.
There is one DLL named Aspose.Total, which includes all packages but you can also get the license of individual items like (Aspose.Words, Aspose.PDF, Aspose.HTML etc.). Here, we will use Aspose.Words.
Go to your Package Manager window or search for “Aspose.Words”.
Or, if you want to install it from NuGet Package Manager console, then open the console and copy paste the below command.
PM> Install-Package Aspose.Words -Version 19.5.0
After successful integration, you will find a reference in your application.
Create Word Template
Now, coming to the template part, you have to create a Word document which will contain the mail merge fields. Please go through the provided link to create a Word document using mail merge. Mail Merge in Microsoft Word.
Using Code
Create a solution of any type in Visual Studio. I am taking VS 2017 and creating a console application. Make sure you have already installed the Aspose.Words in your application.
Now, create a Util namespace and add a new class named AsposeHelper and paste the below code snippet.
- using System;
- using System.IO;
- using WORDS = Aspose.Words;
- using ASPOSE_Demo.Util.Handlers;
- namespace ASPOSE_Demo.Util
- {
- public class ASPOSEHelper
- {
- public static MemoryStream GenerateMemoryStreamFromWordTemplate(string templateLink,
- string[] placeholders,
- object[] values)
- {
- try
- {
- MemoryStream dstStream = null;
- // Create the document obejct of the template file
- var doc = new Aspose.Words.Document(templateLink);
- try
- {
- // Handling the HTML values, which will be replaced in the word template
- doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertHtml();
- // Replace the placeholders of inputted word template
- doc.MailMerge.Execute(placeholders, values);
- // create Memory stream
- dstStream = new MemoryStream();
- // save the word file in desired format
- doc.Save(dstStream, GetSaveFormat("Pdf"));
- }
- catch
- {
- // log
- throw;
- }
- finally
- {
- doc = null;
- }
- return dstStream;
- }
- catch
- {
- throw;
- }
- }
- private static WORDS.SaveFormat GetSaveFormat(string outputType)
- {
- return (WORDS.SaveFormat)Enum.Parse(typeof(WORDS.SaveFormat), outputType.ToString());
- }
- }
- }

Nikunj ChoksiPosted Sep 30, 2022, 6:49 AM
After applied above code but data display twice sometime..so please help below is the where duplicate data adding else if ((e.TableName == "NotificationReport") && (e.FieldName == "CommunicationByMemberHTML")) { if (_formatcode == "DS:1" || _formatcode == "ES:2" || _formatcode == "ES:1") { builder.InsertHtml(val, true); } }