diff --git a/Word-document/Insert-PageBreak-Before-Heading-Paragraphs/.NET/Insert-PageBreak-Before-Heading-Paragraphs.sln b/Word-document/Insert-PageBreak-Before-Heading-Paragraphs/.NET/Insert-PageBreak-Before-Heading-Paragraphs.sln new file mode 100644 index 000000000..05a70f48e --- /dev/null +++ b/Word-document/Insert-PageBreak-Before-Heading-Paragraphs/.NET/Insert-PageBreak-Before-Heading-Paragraphs.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}") = "Insert-PageBreak-Before-Heading-Paragraphs", "Insert-PageBreak-Before-Heading-Paragraphs\Insert-PageBreak-Before-Heading-Paragraphs.csproj", "{739A7FFC-EC2E-4483-97D1-AB32A6EA775F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {739A7FFC-EC2E-4483-97D1-AB32A6EA775F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {739A7FFC-EC2E-4483-97D1-AB32A6EA775F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {739A7FFC-EC2E-4483-97D1-AB32A6EA775F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {739A7FFC-EC2E-4483-97D1-AB32A6EA775F}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B7C6B523-03D5-4B12-9AEE-636BD9C3E7E4} + EndGlobalSection +EndGlobal diff --git a/Word-document/Insert-PageBreak-Before-Heading-Paragraphs/.NET/Insert-PageBreak-Before-Heading-Paragraphs/Data/Input.docx b/Word-document/Insert-PageBreak-Before-Heading-Paragraphs/.NET/Insert-PageBreak-Before-Heading-Paragraphs/Data/Input.docx new file mode 100644 index 000000000..77e92191f Binary files /dev/null and b/Word-document/Insert-PageBreak-Before-Heading-Paragraphs/.NET/Insert-PageBreak-Before-Heading-Paragraphs/Data/Input.docx differ diff --git a/Word-document/Insert-PageBreak-Before-Heading-Paragraphs/.NET/Insert-PageBreak-Before-Heading-Paragraphs/Insert-PageBreak-Before-Heading-Paragraphs.csproj b/Word-document/Insert-PageBreak-Before-Heading-Paragraphs/.NET/Insert-PageBreak-Before-Heading-Paragraphs/Insert-PageBreak-Before-Heading-Paragraphs.csproj new file mode 100644 index 000000000..ad024cb95 --- /dev/null +++ b/Word-document/Insert-PageBreak-Before-Heading-Paragraphs/.NET/Insert-PageBreak-Before-Heading-Paragraphs/Insert-PageBreak-Before-Heading-Paragraphs.csproj @@ -0,0 +1,24 @@ + + + + Exe + net8.0 + Insert_PageBreak_Before_Heading_Paragraphs + enable + enable + + + + + + + + + Always + + + Always + + + + diff --git a/Word-document/Insert-PageBreak-Before-Heading-Paragraphs/.NET/Insert-PageBreak-Before-Heading-Paragraphs/Output/.gitkeep b/Word-document/Insert-PageBreak-Before-Heading-Paragraphs/.NET/Insert-PageBreak-Before-Heading-Paragraphs/Output/.gitkeep new file mode 100644 index 000000000..5f282702b --- /dev/null +++ b/Word-document/Insert-PageBreak-Before-Heading-Paragraphs/.NET/Insert-PageBreak-Before-Heading-Paragraphs/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Word-document/Insert-PageBreak-Before-Heading-Paragraphs/.NET/Insert-PageBreak-Before-Heading-Paragraphs/Program.cs b/Word-document/Insert-PageBreak-Before-Heading-Paragraphs/.NET/Insert-PageBreak-Before-Heading-Paragraphs/Program.cs new file mode 100644 index 000000000..08a440d2d --- /dev/null +++ b/Word-document/Insert-PageBreak-Before-Heading-Paragraphs/.NET/Insert-PageBreak-Before-Heading-Paragraphs/Program.cs @@ -0,0 +1,54 @@ +using Syncfusion.DocIO.DLS; + +namespace Insert_PageBreak_Before_Heading_Paragraphs +{ + class Program + { + public static void Main(string[] args) + { + // Load the existing Word document + WordDocument document = new WordDocument(Path.GetFullPath("Data/Input.docx")); + // Find all paragraphs with the style "Heading 1" + List entities = document.FindAllItemsByProperty(EntityType.Paragraph, "StyleName", "Heading 1"); + + if (entities == null) + { + Console.WriteLine("No paragraphs with the style 'Heading 1' found."); + return; + } + foreach (Entity entity in entities) + { + // Check if the entity is a paragraph + WParagraph paragraph = entity as WParagraph; + // Get the index of the current paragraph in its parent text body + int index = paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph); + // Continue only if there is a previous entity + if (index > 0) + { + // Get the previous entity and cast it to a paragraph + WParagraph previousParagraph = paragraph.OwnerTextBody.ChildEntities[index - 1] as WParagraph; + bool hasPageBreak = false; + // Check if the previous paragraph ends with a page break + if (previousParagraph != null && previousParagraph.ChildEntities.Count > 0) + { + // Get the last item in the previous paragraph + ParagraphItem lastItem = previousParagraph.ChildEntities[previousParagraph.ChildEntities.Count - 1] as ParagraphItem; + // Enable the boolean if the last item is a page break + if (lastItem != null && lastItem is Break && (lastItem as Break).BreakType == BreakType.PageBreak) + hasPageBreak = true; + } + // If no page break is found, insert the page break before the current paragraph + if (!hasPageBreak) + { + WParagraph newPara = new WParagraph(document); + newPara.AppendBreak(BreakType.PageBreak); + // Insert the new paragraph with page break at the correct position + paragraph.OwnerTextBody.ChildEntities.Insert(index, newPara); + } + } + } + document.Save(Path.GetFullPath("../../../Output/Output.docx")); + document.Close(); + } + } +}