Skip to content

Commit a7c0915

Browse files
Merge pull request #189 from SyncfusionExamples/ES-890364-RemoveTOC
ES-890364 - Sample to remove Table of Contents from Word Document
2 parents 88c2c12 + 9821cbd commit a7c0915

File tree

4 files changed

+174
-0
lines changed

4 files changed

+174
-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.10.34928.147
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Remove-table-of-contents", "Remove-table-of-contents\Remove-table-of-contents.csproj", "{23EE73F9-43ED-4B05-9700-18324C1B6EE1}"
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+
{23EE73F9-43ED-4B05-9700-18324C1B6EE1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{23EE73F9-43ED-4B05-9700-18324C1B6EE1}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{23EE73F9-43ED-4B05-9700-18324C1B6EE1}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{23EE73F9-43ED-4B05-9700-18324C1B6EE1}.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 = {AAA5C0F3-BC36-4ABB-B87A-70F41B0015F3}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// See https://aka.ms/new-console-template for more information
2+
using Syncfusion.DocIO.DLS;
3+
using Syncfusion.DocIO;
4+
5+
using (WordDocument document = new WordDocument())
6+
{
7+
//Opens the Word template document.
8+
Stream docStream = File.OpenRead(Path.GetFullPath(@"../../../Data/TOC.docx"));
9+
document.Open(docStream, FormatType.Docx);
10+
docStream.Dispose();
11+
//Removes the TOC field.
12+
TableOfContent toc = document.Sections[0].Body.Paragraphs[2].Items[0] as TableOfContent;
13+
RemoveTableOfContents(toc);
14+
//Saves the file in the given path
15+
docStream = File.Create(Path.GetFullPath(@"../../../Sample.docx"));
16+
document.Save(docStream, FormatType.Docx);
17+
docStream.Dispose();
18+
}
19+
20+
#region Helper methods
21+
/// <summary>
22+
/// Removes the table of contents from Word document.
23+
/// </summary>
24+
/// <param name="toc"></param>
25+
static void RemoveTableOfContents(TableOfContent toc)
26+
{
27+
//Finds the last TOC item.
28+
Entity lastItem = FindLastTOCItem(toc);
29+
30+
//TOC field end mark wasn't exist.
31+
if (lastItem == null)
32+
return;
33+
34+
//Inserts the bookmark start before the TOC instance.
35+
BookmarkStart bkmkStart = new BookmarkStart(toc.Document, "tableOfContent");
36+
toc.OwnerParagraph.Items.Insert(toc.OwnerParagraph.Items.IndexOf(toc), bkmkStart);
37+
38+
//Inserts the bookmark end to next of TOC last item.
39+
BookmarkEnd bkmkEnd = new BookmarkEnd(toc.Document, "tableOfContent");
40+
WParagraph paragraph = lastItem.Owner as WParagraph;
41+
paragraph.Items.Insert(paragraph.Items.IndexOf(lastItem) + 1, bkmkEnd);
42+
43+
//Delete all the items from bookmark start to end (TOC items) using Bookmark Navigator.
44+
DeleteBookmarkContents(bkmkEnd.Name, toc.Document);
45+
}
46+
/// <summary>
47+
/// Delete the bookmark items.
48+
/// </summary>
49+
/// <param name="bkmkName"></param>
50+
/// <param name="document"></param>
51+
static void DeleteBookmarkContents(string bkmkName, WordDocument document)
52+
{
53+
//Creates the bookmark navigator instance to access the bookmark
54+
BookmarksNavigator navigator = new BookmarksNavigator(document);
55+
//Moves the virtual cursor to the location before the end of the bookmark "tableOfContent".
56+
navigator.MoveToBookmark(bkmkName);
57+
//Deletes the bookmark content.
58+
navigator.DeleteBookmarkContent(false);
59+
//Gets the bookmark instance by using FindByName method of BookmarkCollection with bookmark name.
60+
Bookmark bookmark = document.Bookmarks.FindByName(bkmkName);
61+
//Removes the bookmark named "tableOfContent" from Word document.
62+
document.Bookmarks.Remove(bookmark);
63+
}
64+
/// <summary>
65+
/// Finds the last TOC item.
66+
/// </summary>
67+
/// <param name="toc"></param>
68+
/// <returns></returns>
69+
static Entity FindLastTOCItem(TableOfContent toc)
70+
{
71+
int tocIndex = toc.OwnerParagraph.Items.IndexOf(toc);
72+
//TOC may contains nested fields and each fields has its owner field end mark
73+
//so to identify the TOC Field end mark (WFieldMark instance) used the stack.
74+
Stack<Entity> fieldStack = new Stack<Entity>();
75+
fieldStack.Push(toc);
76+
77+
//Finds whether TOC end item is exist in same paragraph.
78+
for (int i = tocIndex + 1; i < toc.OwnerParagraph.Items.Count; i++)
79+
{
80+
Entity item = toc.OwnerParagraph.Items[i];
81+
82+
if (item is WField)
83+
fieldStack.Push(item);
84+
else if (item is WFieldMark && (item as WFieldMark).Type == FieldMarkType.FieldEnd)
85+
{
86+
if (fieldStack.Count == 1)
87+
{
88+
fieldStack.Clear();
89+
return item;
90+
}
91+
else
92+
fieldStack.Pop();
93+
}
94+
}
95+
return FindLastItemInTextBody(toc, fieldStack);
96+
}
97+
/// <summary>
98+
/// Finds the last TOC item from consequence text body items.
99+
/// </summary>
100+
/// <param name="toc"></param>
101+
/// <param name="fieldStack"></param>
102+
/// <returns></returns>
103+
static Entity FindLastItemInTextBody(TableOfContent toc, Stack<Entity> fieldStack)
104+
{
105+
WTextBody tBody = toc.OwnerParagraph.OwnerTextBody;
106+
107+
//Finds whether TOC end item is exist in text body items.
108+
for (int i = tBody.ChildEntities.IndexOf(toc.OwnerParagraph) + 1; i < tBody.ChildEntities.Count; i++)
109+
{
110+
WParagraph paragraph = null;
111+
if (tBody.ChildEntities[i] is WParagraph)
112+
paragraph = tBody.ChildEntities[i] as WParagraph;
113+
else
114+
continue;
115+
116+
foreach (Entity item in paragraph.Items)
117+
{
118+
if (item is WField)
119+
fieldStack.Push(item);
120+
else if (item is WFieldMark && (item as WFieldMark).Type == FieldMarkType.FieldEnd)
121+
{
122+
if (fieldStack.Count == 1)
123+
{
124+
fieldStack.Clear();
125+
return item;
126+
}
127+
else
128+
fieldStack.Pop();
129+
}
130+
}
131+
}
132+
return null;
133+
}
134+
#endregion
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>net8.0</TargetFramework>
6+
<RootNamespace>Remove_table_of_contents</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="25.2.7" />
13+
</ItemGroup>
14+
15+
</Project>

0 commit comments

Comments
 (0)