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

Is it possible to have Word template with merge fields in multiple rows to perform Mail merge and convert it as PDF?

Platform: ASP.NET MVC |
Control: DocIO

Yes, it is possible to have Word template with merge fields in multiple rows to perform Mail merge and convert it as PDF.

Please refer the below UG links to know more about working with Mail merge and Word to PDF conversion.

Working with Mail merge

Word document to PDF conversion

Please refer the below screenshot of Word document template used in this demo.

Word document

The following code example shows how to perform Mail merge in Word document template which has merge fields in multiple rows and convert it as PDF.

C#

// Create a new Word document.
WordDocument wordDocument = new WordDocument();
// Loads the template using created Word document.
wordDocument.Open(filePath, FormatType.Doc);
// Execute Mail merge with group of order details.
DataView orderDetails = new DataView(GetTestOrderDetails());
orderDetails.Sort = "ExtendedPrice DESC"; wordDocument.MailMerge.ExecuteGroup(orderDetails);
// Creates an instance of the DocToPDFConverter.
DocToPDFConverter converter = new DocToPDFConverter();
// Converts Word document into PDF document.
PdfDocument pdfDocument = converter.ConvertToPDF(wordDocument);
// Closes the instance of WordDocument objects.
wordDocument.Close();
// Saves the PDF file.
pdfDocument.Save("Sample.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save);
// Closes the instance of PdfDocument objects.
pdfDocument.Close(true);

 

VB

' Create a new Word document.
Dim wordDocument As WordDocument = New WordDocument()
' Loads the template using created Word document.
wordDocument.Open(filePath, FormatType.Doc)
' Execute Mail merge with group of order details.
Dim orderDetails As DataView = New DataView(GetTestOrderDetails())
orderDetails.Sort = "ExtendedPrice DESC"
wordDocument.MailMerge.ExecuteGroup(orderDetails)
' Creates an instance of the DocToPDFConverter.
Dim converter As DocToPDFConverter = New DocToPDFConverter()
' Converts Word document into PDF document.
Dim pdfDocument As PdfDocument = converter.ConvertToPDF(wordDocument)
' Closes the instance of WordDocument objects.
wordDocument.Close()
' Saves the PDF file.
pdfDocument.Save("Sample.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save)
' Closes the instance of PdfDocument objects.
pdfDocument.Close(True)

 

The following code example provides supporting method for the above code.

C#

private DataTable GetTestOrderDetails()
{
    // Sets data with SQL in current domain.
    AppDomain.CurrentDomain.SetData("SQLServerCompactEditionUnderWebHosting", true);
    // Creates new data table.
    DataTable table = new DataTable();
    table.TableName = "Order";
    // Creates new SQL connection.
    SqlCeConnection conn = new SqlCeConnection();
    // Sets connection string with data source.
    if (conn.ServerVersion != null && conn.ServerVersion.StartsWith("3.5"))
        conn.ConnectionString = "Data Source = NorthwindIO_3.5.sdf";
    else
        conn.ConnectionString = "Data Source = NorthwindIO.sdf";
    // Opens connection.
    conn.Open();
    // Selects data using SQL adapter.
    SqlCeDataAdapter adapter = new SqlCeDataAdapter("SELECT * FROM SyncOrderDetails WHERE OrderId = 10248 ORDER BY ProductID", conn);
    // Fills data table with selected data.
    adapter.Fill(table);
    // Closes adapter object.
    adapter.Dispose();
    // Closes connection.
    conn.Close();
    // Returns data table.
    return table;
}

 

VB

Private Function GetTestOrderDetails() As DataTable
    ' Sets data with SQL in current domain.
    AppDomain.CurrentDomain.SetData("SQLServerCompactEditionUnderWebHosting", True)
    ' Creates new data table.
    Dim table As DataTable = New DataTable()
    table.TableName = "Order"
    ' Creates new SQL connection.
    Dim conn As SqlCeConnection = New SqlCeConnection()
    ' Sets connection string with data source.
    If conn.ServerVersion = Nothing And conn.ServerVersion.StartsWith("3.5") Then
        conn.ConnectionString = "Data Source = NorthwindIO_3.5.sdf"
    Else
        conn.ConnectionString = "Data Source = 'NorthwindIO.sdf"
    End If
    ' Opens connection.
    conn.Open()
    ' Selects data using SQL adapter.
    Dim adapter As SqlCeDataAdapter = New SqlCeDataAdapter("SELECT * FROM SyncOrderDetails WHERE OrderId = 10248 ORDER BY ProductID", conn)
    ' Fills data table with selected data.
    adapter.Fill(table)
    ' Closes adapter object.
    adapter.Dispose()
    ' Closes connection.
    conn.Close()
    ' Returns data table.
    Return table
End Function

 

The resultant document looks as follows.

Resultant documentTable

Description automatically generated

The following sample with above codes demonstrates on how to perform Mail merge in Word document template which has merge fields in multiple rows and convert it as PDF.

Sample

Note:

Please refer assemblies required section to run the sample without any error.

 

2X faster development

The ultimate ASP.NET MVC 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