Skip to content

Commit 6875448

Browse files
committed
Added sample
1 parent 4d6f15f commit 6875448

File tree

5 files changed

+101
-0
lines changed

5 files changed

+101
-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.14.36518.9 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
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}"
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+
{739A7FFC-EC2E-4483-97D1-AB32A6EA775F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{739A7FFC-EC2E-4483-97D1-AB32A6EA775F}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{739A7FFC-EC2E-4483-97D1-AB32A6EA775F}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{739A7FFC-EC2E-4483-97D1-AB32A6EA775F}.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 = {B7C6B523-03D5-4B12-9AEE-636BD9C3E7E4}
24+
EndGlobalSection
25+
EndGlobal
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>net8.0</TargetFramework>
6+
<RootNamespace>Insert_PageBreak_Before_Heading_Paragraphs</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\Input.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Output\.gitkeep">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
</ItemGroup>
23+
24+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using Syncfusion.DocIO.DLS;
2+
3+
namespace Insert_PageBreak_Before_Heading_Paragraphs
4+
{
5+
class Program
6+
{
7+
public static void Main(string[] args)
8+
{
9+
// Load the existing Word document
10+
WordDocument document = new WordDocument(Path.GetFullPath("Data/Input.docx"));
11+
// Find all paragraphs with the style "Heading 1"
12+
List<Entity> entities = document.FindAllItemsByProperty(EntityType.Paragraph, "StyleName", "Heading 1");
13+
14+
if (entities == null)
15+
Console.WriteLine("No paragraphs with the style 'Heading 1' found.");
16+
foreach (Entity entity in entities)
17+
{
18+
// Check if the entity is a paragraph
19+
WParagraph paragraph = entity as WParagraph;
20+
// Get the index of the current paragraph in its parent text body
21+
int index = paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph);
22+
// Continue only if there is a previous entity
23+
if (index > 0)
24+
{
25+
// Get the previous entity and cast it to a paragraph
26+
WParagraph previousParagraph = paragraph.OwnerTextBody.ChildEntities[index - 1] as WParagraph;
27+
bool hasPageBreak = false;
28+
// Check if the previous paragraph ends with a page break
29+
if (previousParagraph != null && previousParagraph.ChildEntities.Count > 0)
30+
{
31+
// Get the last item in the previous paragraph
32+
ParagraphItem lastItem = previousParagraph.ChildEntities[previousParagraph.ChildEntities.Count - 1] as ParagraphItem;
33+
// Enable the boolean if the last item is a page break
34+
if (lastItem != null && lastItem is Break && (lastItem as Break).BreakType == BreakType.PageBreak)
35+
hasPageBreak = true;
36+
}
37+
// If no page break is found, insert the page break before the current paragraph
38+
if (!hasPageBreak)
39+
{
40+
WParagraph newPara = new WParagraph(document);
41+
newPara.AppendBreak(BreakType.PageBreak);
42+
// Insert the new paragraph with page break at the correct position
43+
paragraph.OwnerTextBody.ChildEntities.Insert(index, newPara);
44+
}
45+
}
46+
}
47+
document.Save(Path.GetFullPath("../../../Output/Output.docx"));
48+
document.Close();
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)