diff --git a/Mail-Merge/MailMerge-Results-in-Two-Columns/.NET/MailMerge-Results-in-Two-Columns.sln b/Mail-Merge/MailMerge-Results-in-Two-Columns/.NET/MailMerge-Results-in-Two-Columns.sln new file mode 100644 index 00000000..b4132c1f --- /dev/null +++ b/Mail-Merge/MailMerge-Results-in-Two-Columns/.NET/MailMerge-Results-in-Two-Columns.sln @@ -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 diff --git a/Mail-Merge/MailMerge-Results-in-Two-Columns/.NET/MailMerge-Results-in-Two-Columns/Data/Template.docx b/Mail-Merge/MailMerge-Results-in-Two-Columns/.NET/MailMerge-Results-in-Two-Columns/Data/Template.docx new file mode 100644 index 00000000..55137af1 Binary files /dev/null and b/Mail-Merge/MailMerge-Results-in-Two-Columns/.NET/MailMerge-Results-in-Two-Columns/Data/Template.docx differ diff --git a/Mail-Merge/MailMerge-Results-in-Two-Columns/.NET/MailMerge-Results-in-Two-Columns/MailMerge-Results-in-Two-Columns.csproj b/Mail-Merge/MailMerge-Results-in-Two-Columns/.NET/MailMerge-Results-in-Two-Columns/MailMerge-Results-in-Two-Columns.csproj new file mode 100644 index 00000000..65b54b5d --- /dev/null +++ b/Mail-Merge/MailMerge-Results-in-Two-Columns/.NET/MailMerge-Results-in-Two-Columns/MailMerge-Results-in-Two-Columns.csproj @@ -0,0 +1,24 @@ + + + + Exe + net8.0 + MailMerge_Results_in_Two_Columns + enable + enable + + + + + + + + + Always + + + Always + + + + diff --git a/Mail-Merge/MailMerge-Results-in-Two-Columns/.NET/MailMerge-Results-in-Two-Columns/Output/.gitkeep b/Mail-Merge/MailMerge-Results-in-Two-Columns/.NET/MailMerge-Results-in-Two-Columns/Output/.gitkeep new file mode 100644 index 00000000..5f282702 --- /dev/null +++ b/Mail-Merge/MailMerge-Results-in-Two-Columns/.NET/MailMerge-Results-in-Two-Columns/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Mail-Merge/MailMerge-Results-in-Two-Columns/.NET/MailMerge-Results-in-Two-Columns/Output/Result.docx b/Mail-Merge/MailMerge-Results-in-Two-Columns/.NET/MailMerge-Results-in-Two-Columns/Output/Result.docx new file mode 100644 index 00000000..ef39f580 Binary files /dev/null and b/Mail-Merge/MailMerge-Results-in-Two-Columns/.NET/MailMerge-Results-in-Two-Columns/Output/Result.docx differ diff --git a/Mail-Merge/MailMerge-Results-in-Two-Columns/.NET/MailMerge-Results-in-Two-Columns/Program.cs b/Mail-Merge/MailMerge-Results-in-Two-Columns/.NET/MailMerge-Results-in-Two-Columns/Program.cs new file mode 100644 index 00000000..14d8552c --- /dev/null +++ b/Mail-Merge/MailMerge-Results-in-Two-Columns/.NET/MailMerge-Results-in-Two-Columns/Program.cs @@ -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 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 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); + } + } + } + } + /// + /// Splits the given Word document section into multiple sections based on tables. + /// + /// The Word document to split + /// A list of sections created from the document. + private static List SplitSectionsByTable(WordDocument document) + { + // Initialize a list to hold the new sections + List sections = new List(); + // 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; + } + /// + /// Gets the Student details to perform mail merge + /// + /// + public static List GetStudentData() + { + List students = new List(); + List students1 = new List(); + List students2 = new List(); + 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 parentList = new List + { + 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; + } + } + /// + /// Represents a class to maintain Student details + /// + 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 Students { get; set; } + } +} \ No newline at end of file diff --git a/Word-to-PDF-Conversion/Set-Different-Top-Margins-And-Convert-To-PDF/.NET/Set-Different-Top-Margins-And-Convert-To-PDF/Program.cs b/Word-to-PDF-Conversion/Set-Different-Top-Margins-And-Convert-To-PDF/.NET/Set-Different-Top-Margins-And-Convert-To-PDF/Program.cs index 834a73aa..5f2e32ee 100644 --- a/Word-to-PDF-Conversion/Set-Different-Top-Margins-And-Convert-To-PDF/.NET/Set-Different-Top-Margins-And-Convert-To-PDF/Program.cs +++ b/Word-to-PDF-Conversion/Set-Different-Top-Margins-And-Convert-To-PDF/.NET/Set-Different-Top-Margins-And-Convert-To-PDF/Program.cs @@ -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