Articles in this section
Category / Section

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

3 mins read

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.

 

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