Skip to content

Commit 68da84f

Browse files
Merge pull request #495 from SyncfusionExamples/992135-Add-insert-page-break-for-specific-heading-paragraph-sample
992135 - Add DocIO Sample for Insert Page Breaks Before Heading Paragraphs in Word Document
2 parents 6ce0083 + 3ecfb37 commit 68da84f

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
if (entities == null)
14+
{
15+
Console.WriteLine("No paragraphs with the style 'Heading 1' found.");
16+
}
17+
else
18+
{
19+
foreach (Entity entity in entities)
20+
{
21+
WParagraph paragraph = entity as WParagraph;
22+
// Get the index of the current paragraph in its parent text body
23+
int index = paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph);
24+
// Continue only if there is a previous entity
25+
if (index > 0)
26+
{
27+
// Get the previous entity and cast it to a paragraph
28+
WParagraph previousParagraph = paragraph.OwnerTextBody.ChildEntities[index - 1] as WParagraph;
29+
bool hasPageBreak = false;
30+
// Check if the previous paragraph ends with a page break
31+
if (previousParagraph != null && previousParagraph.ChildEntities.Count > 0)
32+
{
33+
// Get the last item in the previous paragraph
34+
ParagraphItem lastItem = previousParagraph.ChildEntities[previousParagraph.ChildEntities.Count - 1] as ParagraphItem;
35+
// Enable the boolean if the last item is a page break
36+
if (lastItem is Break && (lastItem as Break).BreakType == BreakType.PageBreak)
37+
hasPageBreak = true;
38+
}
39+
// If no page break is found, insert the page break before the current paragraph
40+
if (!hasPageBreak)
41+
{
42+
WParagraph newPara = new WParagraph(document);
43+
newPara.AppendBreak(BreakType.PageBreak);
44+
// Insert the new paragraph with page break at the correct position
45+
paragraph.OwnerTextBody.ChildEntities.Insert(index, newPara);
46+
}
47+
}
48+
}
49+
}
50+
// Save the Word document.
51+
document.Save(Path.GetFullPath("../../../Output/Output.docx"));
52+
// Close the Word document.
53+
document.Close();
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)