Skip to content

Commit 188f247

Browse files
ES-957509-Export-content-between-bookmarks-to-HTML
1 parent 410b6a3 commit 188f247

File tree

5 files changed

+120
-0
lines changed

5 files changed

+120
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35527.113 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Export-content-between-bookmarks-to-HTML", "Export-content-between-bookmarks-to-HTML\Export-content-between-bookmarks-to-HTML.csproj", "{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}"
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+
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
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>Export_content_between_bookmarks_to_HTML</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\Template.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+

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
5+
6+
// Define the name of the first existing bookmark from where extraction starts.
7+
string bookmark1 = "Mysteries";
8+
// Define the name of the second existing bookmark where extraction ends.
9+
string bookmark2 = "Facts";
10+
// Define the name for a temporary bookmark that spans content between bookmark1 and bookmark2.
11+
string tempBookmarkName = "tempBookmark";
12+
13+
// Load the Word document.
14+
using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx")))
15+
{
16+
// Find the first bookmark by name.
17+
Bookmark firstBookmark = document.Bookmarks.FindByName(bookmark1);
18+
int index = 0;
19+
20+
// Get the paragraph that contains the end of the first bookmark.
21+
WParagraph firstBookmarkOwnerPara = firstBookmark.BookmarkEnd.OwnerParagraph;
22+
// Find the position of the bookmark end in the paragraph.
23+
index = firstBookmarkOwnerPara.Items.IndexOf(firstBookmark.BookmarkEnd);
24+
25+
// Create a temporary bookmark start and insert it right after the first bookmark end.
26+
BookmarkStart newBookmarkStart = new BookmarkStart(document, tempBookmarkName);
27+
firstBookmarkOwnerPara.ChildEntities.Insert(index + 1, newBookmarkStart);
28+
29+
// Find the second bookmark by name.
30+
Bookmark secondBookmark = document.Bookmarks.FindByName(bookmark2);
31+
// Get the paragraph that contains the start of the second bookmark.
32+
WParagraph secondBookmarkOwnerPara = secondBookmark.BookmarkStart.OwnerParagraph;
33+
// Find the position of the bookmark start in the paragraph.
34+
index = secondBookmarkOwnerPara.Items.IndexOf(secondBookmark.BookmarkStart);
35+
36+
// Create a temporary bookmark end and insert it just before the second bookmark start.
37+
BookmarkEnd newBookmarkEnd = new BookmarkEnd(document, tempBookmarkName);
38+
secondBookmarkOwnerPara.ChildEntities.Insert(index, newBookmarkEnd);
39+
40+
// Navigate to the temporary bookmark created between the two bookmarks.
41+
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
42+
bookmarkNavigator.MoveToBookmark(tempBookmarkName);
43+
44+
// Extract the content between the bookmarks as TextBodyPart (optional if needed later).
45+
TextBodyPart part = bookmarkNavigator.GetBookmarkContent();
46+
47+
// Get the bookmark content as a new Word document part.
48+
WordDocumentPart content = bookmarkNavigator.GetContent();
49+
50+
// Load the extracted content into a temporary Word document for modification or export.
51+
using (WordDocument tempDocument = content.GetAsWordDocument())
52+
{
53+
// Remove all headers and footers from the extracted content.
54+
RemoveHeaderAndFooter(tempDocument);
55+
56+
// Save the extracted content as an HTML file.
57+
tempDocument.Save(Path.GetFullPath(@"Output/Result.html"), FormatType.Html);
58+
}
59+
}
60+
61+
// Removes all headers and footers from the given Word document.
62+
void RemoveHeaderAndFooter(WordDocument document)
63+
{
64+
foreach (WSection section in document.Sections)
65+
{
66+
foreach (HeaderFooter entity in section.HeadersFooters)
67+
{
68+
if (entity != null)
69+
entity.ChildEntities.Clear();
70+
}
71+
}
72+
}
73+

0 commit comments

Comments
 (0)