Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.36518.9 d17.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MailMerge-Results-in-Two-Columns", "MailMerge-Results-in-Two-Columns\MailMerge-Results-in-Two-Columns.csproj", "{C8E45973-4066-41AA-B528-B1A1758C58A7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C8E45973-4066-41AA-B528-B1A1758C58A7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C8E45973-4066-41AA-B528-B1A1758C58A7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C8E45973-4066-41AA-B528-B1A1758C58A7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C8E45973-4066-41AA-B528-B1A1758C58A7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {612CB899-E289-486E-A23E-49216C64A54F}
EndGlobalSection
EndGlobal
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>MailMerge_Results_in_Two_Columns</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="Data\Template.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;

namespace MailMerge_Results_in_Two_Columns
{
class Program
{
public static void Main(string[] args)
{
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read))
{
using (WordDocument document = new WordDocument(fileStream, FormatType.Docx))
{
// Get student data
List<StudentsGroup> studentsGroupList = GetStudentData();
//Create mail merge data table
MailMergeDataTable dataTable = new MailMergeDataTable("StudentsGroup", studentsGroupList);
// Execute nested mail merge
document.MailMerge.ExecuteNestedGroup(dataTable);
// Split the document into sections based on tables
List<WSection> sections = SplitSectionsByTable(document);
// Clear existing sections in the document
document.Sections.Clear();
//Added newly created sections into the document.
foreach (WSection section in sections)
{
document.Sections.Add(section);
}
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
{
//Saves the Word document to file stream.
document.Save(outputFileStream, FormatType.Docx);
}
}
}
}
/// <summary>
/// Splits the given Word document section into multiple sections based on tables.
/// </summary>
/// <param name="document">The Word document to split</param>
/// <returns>A list of sections created from the document.</returns>
private static List<WSection> SplitSectionsByTable(WordDocument document)
{
// Initialize a list to hold the new sections
List<WSection> sections = new List<WSection>();
// Iterate through all sections in the document
foreach (WSection section in document.Sections)
{
// Clone the current section.
WSection clonedSection = section.Clone();
// Clear child entities from the cloned section.
clonedSection.Body.ChildEntities.Clear();
// Create a new section from the cloned section.
WSection newSection = clonedSection.Clone();
// Get the text body of the current section.
WTextBody textBody = section.Body;
// Iterate through each child entity in the Text Body
for (int i = 0; i < textBody.ChildEntities.Count; i++)
{
//Accesses the body items (should be either paragraph, table or block content control) as IEntity
IEntity bodyItemEntity = textBody.ChildEntities[i];
// Decides the element type by using EntityType
switch (bodyItemEntity.EntityType)
{
case EntityType.Paragraph:
// Clone and add the paragraph to the new section
WParagraph paragraph = bodyItemEntity as WParagraph;
newSection.Body.ChildEntities.Add(paragraph.Clone());
break;
case EntityType.Table:
// Mark the first row of the table as a header
(bodyItemEntity as WTable).Rows[0].IsHeader = true;
// Add a paragraph to separate sections
newSection.AddParagraph();
// Add the current section to the collection
sections.Add(newSection);
// Create a new section for the table
newSection = clonedSection.Clone();
newSection.BreakCode = SectionBreakCode.NoBreak;
// Setup columns (optional)
float spacing = 20;
float colWidth = newSection.PageSetup.ClientWidth / 2 - spacing;
newSection.AddColumn(colWidth, spacing);
newSection.Columns[0].Width = colWidth;
// Clone and add the table to the new section
newSection.Body.ChildEntities.Add(bodyItemEntity.Clone());
sections.Add(newSection);
// Reset newSection for further processing
newSection = clonedSection.Clone();
break;
case EntityType.BlockContentControl:
// Clone and add the block content control to the new section
BlockContentControl blockContentControl = bodyItemEntity as BlockContentControl;
newSection.Body.ChildEntities.Add((BlockContentControl)blockContentControl.Clone());
break;
}
}
}
// Return the list of newly created sections
return sections;
}
/// <summary>
/// Gets the Student details to perform mail merge
/// </summary>
/// <returns></returns>
public static List<StudentsGroup> GetStudentData()
{
List<Student> students = new List<Student>();
List<Student> students1 = new List<Student>();
List<Student> students2 = new List<Student>();
for (int i = 1; i <= 45; i++)
{
students.Add(new Student
{
RollNo = $"{i}",
AdmissionNo = $"ADM{i:000}",
StudentName = $"Class 1A Student {i}",
Marks = $"M{i:000}",
});
}
for (int i = 1; i <= 45; i++)
{
students1.Add(new Student
{
RollNo = $"{i}",
AdmissionNo = $"ADM{i:000}",
StudentName = $"Class 2A Student {i}",
Marks = $"M{i:000}",
});
}
for (int i = 1; i <= 45; i++)
{
students2.Add(new Student
{
RollNo = $"{i}",
AdmissionNo = $"ADM{i:000}",
StudentName = $"Class 2B Student {i}",
Marks = $"M{i:000}",
});
}
List<StudentsGroup> parentList = new List<StudentsGroup>
{
new StudentsGroup {
Class="1#A",
Exam="MidTerm",
Students = students
},
new StudentsGroup {
Class="2#A",
Exam="MidTerm",
Students = students1
},
new StudentsGroup {
Class="2#B",
Exam="MidTerm",
Students = students2
}
};

return parentList;
}
}
/// <summary>
/// Represents a class to maintain Student details
/// </summary>
public class Student
{
public string RollNo { get; set; }
public string AdmissionNo { get; set; }
public string StudentName { get; set; }
public string Marks { get; set; }
}
public class StudentsGroup
{
public string Class { get; set; }
public string Exam { get; set; }
public List<Student> Students { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ public static void Main(string[] args)
{
//Load the Word document
WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Input.docx"));
int sectionsCount = document.Sections.Count;
// Loop through all sections in the document
for (int i = 0; i < document.Sections.Count; i++)
for (int i = 0; i < sectionsCount; i++)
{
// Get the current section
IWSection section = document.Sections[i];
// Set the top margin based on whether it's the first or last section
if (i == 0 || i == document.Sections.Count -1)
if (i == 0 || i == sectionsCount - 1)
section.PageSetup.Margins.Top = 200; // Apply a top margin of 200 for the first section and last section
else
section.PageSetup.Margins.Top = 90; // Apply a top margin of 90 for all other sections
Expand Down
Loading