Skip to content
Open
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}") = "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
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>Insert_PageBreak_Before_Heading_Paragraphs</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

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

<ItemGroup>
<None Update="Data\Input.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 @@

Original file line number Diff line number Diff line change
@@ -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<Entity> 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();
}
}
}
Loading