Skip to content

Commit bbb854a

Browse files
Added remove empty column
1 parent ba81f67 commit bbb854a

File tree

5 files changed

+173
-0
lines changed

5 files changed

+173
-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.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>net6.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="24.1.44" />
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>

0 commit comments

Comments
 (0)