Skip to content

Commit e9b1669

Browse files
Merge pull request #238 from VijayadharshiniMathiyalagan/main
ES-865324 - Added Github sample for the Knowledge Base
2 parents 364554a + 545d35f commit e9b1669

File tree

17 files changed

+435
-0
lines changed

17 files changed

+435
-0
lines changed

Mail-Merge/Modify-font-during-mail-merge/.NET/Modify-font-during-mail-merge/Program.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,15 @@ static void Main(string[] args)
3535
/// </summary>
3636
private static void ModifyFont(object sender, MergeFieldEventArgs args)
3737
{
38+
// Sets the font name to Arial for the selected text range.
3839
args.TextRange.CharacterFormat.FontName = "Arial";
40+
// Sets the font size to 18 points for the selected text range.
3941
args.TextRange.CharacterFormat.FontSize = 18;
42+
// Applies bold formatting to the selected text range.
4043
args.TextRange.CharacterFormat.Bold = true;
44+
// Applies italic formatting to the selected text range.
4145
args.TextRange.CharacterFormat.Italic = true;
46+
// Applies single underline to the selected text range.
4247
args.TextRange.CharacterFormat.UnderlineStyle = UnderlineStyle.Single;
4348
}
4449

@@ -47,17 +52,26 @@ private static void ModifyFont(object sender, MergeFieldEventArgs args)
4752
/// </summary>
4853
private static DataTable GetDataTable()
4954
{
55+
// Creates a new DataTable with the name "Employee".
5056
DataTable dataTable = new DataTable("Employee");
57+
// Adds a column "EmployeeName" to the DataTable.
5158
dataTable.Columns.Add("EmployeeName");
59+
// Adds a column "EmployeeNumber" to the DataTable.
5260
dataTable.Columns.Add("EmployeeNumber");
5361

62+
// Loops 20 times to add rows to the DataTable.
5463
for (int i = 0; i < 20; i++)
5564
{
65+
// Creates a new DataRow for the DataTable.
5666
DataRow datarow = dataTable.NewRow();
67+
// Adds the newly created DataRow to the DataTable.
5768
dataTable.Rows.Add(datarow);
69+
// Sets the value of the first column (EmployeeName) for the current row.
5870
datarow[0] = "Employee" + i.ToString();
71+
// Sets the value of the second column (EmployeeNumber) for the current row.
5972
datarow[1] = "EMP" + i.ToString();
6073
}
74+
// Returns the populated DataTable.
6175
return dataTable;
6276
}
6377
#endregion
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.8.34322.80
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Remove-empty-column-after-mail-merge", "Remove-empty-column-after-mail-merge\Remove-empty-column-after-mail-merge.csproj", "{5945DC82-CDAA-4847-BDA7-9FABCD9F18AF}"
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+
{5945DC82-CDAA-4847-BDA7-9FABCD9F18AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{5945DC82-CDAA-4847-BDA7-9FABCD9F18AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{5945DC82-CDAA-4847-BDA7-9FABCD9F18AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{5945DC82-CDAA-4847-BDA7-9FABCD9F18AF}.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 = {E854CAE3-189F-4690-A15C-FBBCE7C8789C}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using Syncfusion.Drawing;
4+
using System;
5+
using System.Data;
6+
using System.IO;
7+
8+
namespace Remove_empty_column_after_mail_merge
9+
{
10+
class Program
11+
{
12+
//Boolean to check whether the merge field has value
13+
public static bool hasCostValue = false;
14+
//Cell index of the merge field
15+
public static int cellIndex;
16+
static void Main(string[] args)
17+
{
18+
//Creates new Word document instance for Word processing
19+
using (WordDocument document = new WordDocument())
20+
{
21+
//Opens the Word template document
22+
Stream docStream = File.OpenRead(Path.GetFullPath(@"Data/Template.docx"));
23+
document.Open(docStream, FormatType.Docx);
24+
docStream.Dispose();
25+
//Get the table
26+
WTable table = GetColumnIndex(document);
27+
//Get the data set
28+
DataSet ds = GetData();
29+
//Using Merge events to do conditional formatting during runtime
30+
document.MailMerge.MergeField += new MergeFieldEventHandler(MergeField_TaskCost);
31+
//Execute Mail Merge with groups
32+
document.MailMerge.ExecuteGroup(ds.Tables["Task_CostList"]);
33+
if (!hasCostValue)
34+
{
35+
//Remove the empty column
36+
RemoveColumn(table);
37+
}
38+
//Saves and closes the Word document
39+
docStream = File.Create(Path.GetFullPath(@"Output/Output.docx"));
40+
document.Save(docStream, FormatType.Docx);
41+
docStream.Dispose();
42+
}
43+
}
44+
45+
#region Helper Methods
46+
/// <summary>
47+
/// Get the column index
48+
/// </summary>
49+
private static WTable GetColumnIndex(WordDocument document)
50+
{
51+
WTable table = null;
52+
//Get the merge field
53+
WMergeField mergeField = document.FindItemByProperty(EntityType.MergeField, "FieldName", "Cost") as WMergeField;
54+
if (mergeField != null)
55+
{
56+
//Check whether the merge field is present inside a table cell
57+
if (mergeField.OwnerParagraph.IsInCell)
58+
{
59+
WTableCell cell = mergeField.OwnerParagraph.OwnerTextBody as WTableCell;
60+
//Get the column index
61+
cellIndex = cell.GetCellIndex();
62+
table = cell.OwnerRow.Owner as WTable;
63+
}
64+
}
65+
return table;
66+
}
67+
/// <summary>
68+
/// Gets the data to perform mail merge
69+
/// </summary>
70+
private static DataSet GetData()
71+
{
72+
// Create a DataSet.
73+
DataSet ds = new DataSet();
74+
//List of Syncfusion products name
75+
string[] products = { "Task 1", "Task 2", "Task 3", "Task 4", "Task 5" };
76+
//Add new Tables to the data set
77+
DataRow row;
78+
ds.Tables.Add();
79+
ds.Tables.Add();
80+
//Add fields to the Task_CostList table.
81+
ds.Tables[0].TableName = "Task_CostList";
82+
ds.Tables[0].Columns.Add("Task");
83+
ds.Tables[0].Columns.Add("Cost");
84+
int count = 0;
85+
//Insert values to the table row.
86+
foreach (string product in products)
87+
{
88+
row = ds.Tables["Task_CostList"].NewRow();
89+
row["Task"] = product;
90+
ds.Tables["Task_CostList"].Rows.Add(row);
91+
count++;
92+
}
93+
return ds;
94+
}
95+
/// <summary>
96+
/// Remove the column
97+
/// </summary>
98+
private static void RemoveColumn(WTable table)
99+
{
100+
//Iterate through all rows
101+
for (int i = table.Rows.Count - 1; i >= 0; i--)
102+
{
103+
//Remove the cell present in the cellIndex
104+
table.Rows[i].Cells.RemoveAt(cellIndex);
105+
}
106+
}
107+
#endregion
108+
109+
#region Event Handlers
110+
/// <summary>
111+
/// Method to handle MergeImageField event.
112+
/// </summary>
113+
private static void MergeField_TaskCost(object sender, MergeFieldEventArgs args)
114+
{
115+
if ( args.FieldName == "Cost" && hasCostValue && args.FieldValue != null
116+
&& args.FieldValue != DBNull.Value && args.FieldValue != string.Empty)
117+
{
118+
hasCostValue = true;
119+
}
120+
}
121+
#endregion
122+
}
123+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Remove_empty_column_after_mail_merge</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\Template.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Output\.gitkeep">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
</ItemGroup>
23+
24+
</Project>
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.8.34322.80
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Apply-indentation-for-import-content", "Apply-indentation-for-import-content\Apply-indentation-for-import-content.csproj", "{53DCEB0F-A67D-46ED-BCBB-31F7C434A4F3}"
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+
{53DCEB0F-A67D-46ED-BCBB-31F7C434A4F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{53DCEB0F-A67D-46ED-BCBB-31F7C434A4F3}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{53DCEB0F-A67D-46ED-BCBB-31F7C434A4F3}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{53DCEB0F-A67D-46ED-BCBB-31F7C434A4F3}.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 = {D0E03655-7880-457C-B4E4-35D46A0B5B00}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Apply_indentation_for_import_content</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\SourceDocument.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Data\DestinationDocument.docx">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
<None Update="Output\.gitkeep">
23+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
24+
</None>
25+
</ItemGroup>
26+
27+
</Project>

0 commit comments

Comments
 (0)