Skip to content

Commit 15e63c1

Browse files
committed
Added Modify-font-during-mail-merge
1 parent b614bfb commit 15e63c1

File tree

7 files changed

+190
-0
lines changed

7 files changed

+190
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.6.33829.357
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Modify-font-during-mailmerge", "Modify-font-during-mailmerge\Modify-font-during-mailmerge.csproj", "{3D28E16F-1E2A-404B-AF7B-E678EE856510}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{3D28E16F-1E2A-404B-AF7B-E678EE856510}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{3D28E16F-1E2A-404B-AF7B-E678EE856510}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{3D28E16F-1E2A-404B-AF7B-E678EE856510}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{3D28E16F-1E2A-404B-AF7B-E678EE856510}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {CB80D9B4-7874-45F4-B214-8BDA9A7D9D1D}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
5+
</startup>
6+
</configuration>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{3D28E16F-1E2A-404B-AF7B-E678EE856510}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>Modify_font_during_mailmerge</RootNamespace>
10+
<AssemblyName>Modify-font-during-mailmerge</AssemblyName>
11+
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
<Deterministic>true</Deterministic>
15+
<TargetFrameworkProfile />
16+
</PropertyGroup>
17+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
18+
<PlatformTarget>AnyCPU</PlatformTarget>
19+
<DebugSymbols>true</DebugSymbols>
20+
<DebugType>full</DebugType>
21+
<Optimize>false</Optimize>
22+
<OutputPath>bin\Debug\</OutputPath>
23+
<DefineConstants>DEBUG;TRACE</DefineConstants>
24+
<ErrorReport>prompt</ErrorReport>
25+
<WarningLevel>4</WarningLevel>
26+
</PropertyGroup>
27+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
28+
<PlatformTarget>AnyCPU</PlatformTarget>
29+
<DebugType>pdbonly</DebugType>
30+
<Optimize>true</Optimize>
31+
<OutputPath>bin\Release\</OutputPath>
32+
<DefineConstants>TRACE</DefineConstants>
33+
<ErrorReport>prompt</ErrorReport>
34+
<WarningLevel>4</WarningLevel>
35+
</PropertyGroup>
36+
<ItemGroup>
37+
<Reference Include="Syncfusion.Compression.Base, Version=28.1462.33.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
38+
<HintPath>..\packages\Syncfusion.Compression.Base.28.1.33\lib\net462\Syncfusion.Compression.Base.dll</HintPath>
39+
</Reference>
40+
<Reference Include="Syncfusion.DocIO.Base, Version=28.1462.33.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
41+
<HintPath>..\packages\Syncfusion.DocIO.WinForms.28.1.33\lib\net462\Syncfusion.DocIO.Base.dll</HintPath>
42+
</Reference>
43+
<Reference Include="Syncfusion.Licensing, Version=28.1462.33.0, Culture=neutral, PublicKeyToken=632609b4d040f6b4, processorArchitecture=MSIL">
44+
<HintPath>..\packages\Syncfusion.Licensing.28.1.33\lib\net462\Syncfusion.Licensing.dll</HintPath>
45+
</Reference>
46+
<Reference Include="Syncfusion.OfficeChart.Base, Version=28.1462.33.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
47+
<HintPath>..\packages\Syncfusion.OfficeChart.Base.28.1.33\lib\net462\Syncfusion.OfficeChart.Base.dll</HintPath>
48+
</Reference>
49+
<Reference Include="System" />
50+
<Reference Include="System.Core" />
51+
<Reference Include="System.Drawing" />
52+
<Reference Include="System.Xml.Linq" />
53+
<Reference Include="System.Data.DataSetExtensions" />
54+
<Reference Include="Microsoft.CSharp" />
55+
<Reference Include="System.Data" />
56+
<Reference Include="System.Net.Http" />
57+
<Reference Include="System.Xml" />
58+
</ItemGroup>
59+
<ItemGroup>
60+
<Compile Include="Program.cs" />
61+
<Compile Include="Properties\AssemblyInfo.cs" />
62+
</ItemGroup>
63+
<ItemGroup>
64+
<None Include="App.config" />
65+
<None Include="packages.config" />
66+
</ItemGroup>
67+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
68+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using Syncfusion.DocIO.DLS;
2+
using Syncfusion.DocIO;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
using System.Drawing;
10+
using System.Data;
11+
12+
namespace Modify_font_during_mailmerge
13+
{
14+
internal class Program
15+
{
16+
static void Main(string[] args)
17+
{
18+
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"../../Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite))
19+
{
20+
//Opens the template document.
21+
using (WordDocument document = new WordDocument(fileStream, FormatType.Docx))
22+
{
23+
//Uses the mail merge events to perform the conditional formatting during runtime.
24+
document.MailMerge.MergeField += new MergeFieldEventHandler(ModifyFont);
25+
//Executes Mail Merge with groups.
26+
document.MailMerge.ExecuteGroup(GetDataTable());
27+
//Creates file stream.
28+
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"../../Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
29+
{
30+
//Saves the Word document to file stream.
31+
document.Save(outputStream, FormatType.Docx);
32+
}
33+
}
34+
}
35+
}
36+
#region Helper methods
37+
/// <summary>
38+
/// Represents the method that handles the MergeField event.
39+
/// </summary>
40+
private static void ModifyFont(object sender, MergeFieldEventArgs args)
41+
{
42+
// Sets the font name to Arial for the selected text range.
43+
args.TextRange.CharacterFormat.FontName = "Arial";
44+
// Sets the font size to 18 points for the selected text range.
45+
args.TextRange.CharacterFormat.FontSize = 18;
46+
// Applies bold formatting to the selected text range.
47+
args.TextRange.CharacterFormat.Bold = true;
48+
// Applies italic formatting to the selected text range.
49+
args.TextRange.CharacterFormat.Italic = true;
50+
// Applies single underline to the selected text range.
51+
args.TextRange.CharacterFormat.UnderlineStyle = UnderlineStyle.Single;
52+
}
53+
54+
/// <summary>
55+
/// Gets the data to perform mail merge.
56+
/// </summary>
57+
private static DataTable GetDataTable()
58+
{
59+
// Creates a new DataTable with the name "Employee".
60+
DataTable dataTable = new DataTable("Employee");
61+
// Adds a column "EmployeeName" to the DataTable.
62+
dataTable.Columns.Add("EmployeeName");
63+
// Adds a column "EmployeeNumber" to the DataTable.
64+
dataTable.Columns.Add("EmployeeNumber");
65+
66+
// Loops 20 times to add rows to the DataTable.
67+
for (int i = 0; i < 20; i++)
68+
{
69+
// Creates a new DataRow for the DataTable.
70+
DataRow datarow = dataTable.NewRow();
71+
// Adds the newly created DataRow to the DataTable.
72+
dataTable.Rows.Add(datarow);
73+
// Sets the value of the first column (EmployeeName) for the current row.
74+
datarow[0] = "Employee" + i.ToString();
75+
// Sets the value of the second column (EmployeeNumber) for the current row.
76+
datarow[1] = "EMP" + i.ToString();
77+
}
78+
// Returns the populated DataTable.
79+
return dataTable;
80+
}
81+
#endregion
82+
}
83+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="Syncfusion.Compression.Base" version="28.1.33" targetFramework="net462" />
4+
<package id="Syncfusion.DocIO.WinForms" version="28.1.33" targetFramework="net462" />
5+
<package id="Syncfusion.Licensing" version="28.1.33" targetFramework="net462" />
6+
<package id="Syncfusion.OfficeChart.Base" version="28.1.33" targetFramework="net462" />
7+
</packages>

0 commit comments

Comments
 (0)