Skip to content

Commit 43e42f4

Browse files
Merge pull request #184 from snehasf3509/main
Sample to remove empty pages from a Word document
2 parents 1baca13 + 445a8f8 commit 43e42f4

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-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.8.34322.80
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Remove-empty-pages-from-Word-document", "Remove-empty-pages-from-Word-document\Remove-empty-pages-from-Word-document.csproj", "{2AEB2610-0251-430B-8961-EBF87F44C53B}"
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+
{2AEB2610-0251-430B-8961-EBF87F44C53B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{2AEB2610-0251-430B-8961-EBF87F44C53B}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{2AEB2610-0251-430B-8961-EBF87F44C53B}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{2AEB2610-0251-430B-8961-EBF87F44C53B}.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 = {5DA52884-E894-4B31-970D-779621A968AF}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
5+
//Opens an existing Word document
6+
using (FileStream inputStream = new FileStream("../../../Data/Sample.docx", FileMode.Open, FileAccess.Read))
7+
{
8+
using (WordDocument document = new WordDocument(inputStream, FormatType.Docx))
9+
{
10+
WTextBody textBody = null;
11+
//Iterates sections in Word document.
12+
for (int i = document.Sections.Count - 1; i >= 0; i--)
13+
{
14+
//Accesses the Body of section where all the contents in document are apart
15+
textBody = document.Sections[i].Body;
16+
//Removes the last empty page in the Word document
17+
RemoveEmptyItems(textBody);
18+
//Removes the empty sections in the document
19+
if (textBody.ChildEntities.Count == 0)
20+
{
21+
int SectionIndex = document.ChildEntities.IndexOf(document.Sections[i]);
22+
document.ChildEntities.RemoveAt(SectionIndex);
23+
}
24+
}
25+
using (FileStream outputStream = new FileStream("../../../Data/Output.docx", FileMode.Create, FileAccess.Write))
26+
{
27+
//Saves and closes the Word document
28+
document.Save(outputStream, FormatType.Docx);
29+
}
30+
}
31+
}
32+
/// <summary>
33+
/// Remove the empty paragraph in the Word document.
34+
/// </summary>
35+
void RemoveEmptyItems(WTextBody textBody)
36+
{
37+
//A flag to determine any renderable item found in the Word document.
38+
bool IsRenderableItem = false;
39+
//A flag to determine a page break is found as the previous item
40+
bool HasPrevPageBreak = false;
41+
//Iterates into textbody items.
42+
for (int itemIndex = textBody.ChildEntities.Count - 1; itemIndex >= 0; itemIndex--)
43+
{
44+
//Checks item is empty paragraph and removes it.
45+
if (textBody.ChildEntities[itemIndex] is WParagraph)
46+
{
47+
WParagraph paragraph = textBody.ChildEntities[itemIndex] as WParagraph;
48+
//Iterates into paragraph
49+
for (int pIndex = paragraph.Items.Count - 1; pIndex >= 0; pIndex--)
50+
{
51+
ParagraphItem paragraphItem = paragraph.Items[pIndex];
52+
53+
//Removes page breaks
54+
if ((paragraphItem is Break && (paragraphItem as Break).BreakType == BreakType.PageBreak))
55+
{
56+
if (HasPrevPageBreak)
57+
paragraph.Items.RemoveAt(pIndex);
58+
else
59+
HasPrevPageBreak = true;
60+
}
61+
//Check paragraph contains any renderable items.
62+
else
63+
{
64+
HasPrevPageBreak = false;
65+
if (!(paragraphItem is BookmarkStart || paragraphItem is BookmarkEnd))
66+
{
67+
//Found renderable item and break the iteration.
68+
IsRenderableItem = true;
69+
break;
70+
}
71+
}
72+
}
73+
//Remove empty paragraph and the paragraph with bookmarks only
74+
if (paragraph.Items.Count == 0 || !IsRenderableItem)
75+
textBody.ChildEntities.RemoveAt(itemIndex);
76+
}
77+
}
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<RootNamespace>Remove_empty_pages_from_Word_document</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+
</Project>

0 commit comments

Comments
 (0)