Skip to content

Commit be43594

Browse files
Merge pull request #444 from SyncfusionExamples/ES-957666-Find-and-extract_content_between_markers
ES-957666- Add the sample Find-and-extract-content-between-markers
2 parents a92b8de + 9e54c0f commit be43594

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-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}") = "Find-and-extract-content-between-markers", "Find-and-extract-content-between-markers\Find-and-extract-content-between-markers.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,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Find_and_extract_content_between_markers</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+
</ItemGroup>
20+
21+
</Project>
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using Syncfusion.DocIO.DLS;
2+
3+
// Load the Word document using Syncfusion.DocIO
4+
using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx")))
5+
{
6+
// Define an array of texts to find within the document
7+
string[] textsToFind = new string[2] { "GIANT START", "GIANT END" };
8+
9+
// Iterate through each text in the array and call FindStartEndAndIterate to process them
10+
foreach (string textToFind in textsToFind)
11+
{
12+
FindStartEndAndIterate(document, textToFind);
13+
}
14+
}
15+
16+
/// <summary>
17+
/// Finds the start and end paragraphs based on the search text, then iterates through the paragraphs in the specified range.
18+
/// </summary>
19+
/// <param name="document">The Word document to search through.</param>
20+
/// <param name="textToSearch">The text to search for that marks the start of the range.</param>
21+
void FindStartEndAndIterate(WordDocument document, string textToSearch)
22+
{
23+
WParagraph startPara = null;
24+
WParagraph endPara = null;
25+
WSection startSection = null;
26+
WSection endSection = null;
27+
28+
string endText = "GIANT"; // Text to mark the end of the range
29+
30+
// Step 1: Find the start and end paragraphs by iterating through each section of the document
31+
foreach (WSection section in document.Sections)
32+
{
33+
foreach (Entity entity in section.Body.ChildEntities)
34+
{
35+
// Check if the entity is a paragraph
36+
if (entity is WParagraph paragraph)
37+
{
38+
string paraText = paragraph.Text;
39+
40+
// If the paragraph starts with the 'textToSearch', set it as the start paragraph
41+
if (paraText.StartsWith(textToSearch))
42+
{
43+
startPara = paragraph;
44+
startSection = section;
45+
}
46+
// If the paragraph contains the 'endText', set it as the end paragraph
47+
else if (paraText.Contains(endText))
48+
{
49+
endPara = paragraph;
50+
endSection = section;
51+
break; // Stop once the end paragraph is found
52+
}
53+
}
54+
}
55+
56+
// If both start and end paragraphs have been found, break out of the loop
57+
if (startPara != null && endPara != null)
58+
break;
59+
}
60+
61+
// If no start or end paragraphs were found, exit the method
62+
if (startPara == null || endPara == null)
63+
return;
64+
65+
// Get the index of the start paragraph within its section's body and the section itself
66+
int startBodyIndex = startSection.Body.ChildEntities.IndexOf(startPara);
67+
int startSectionIndex = document.Sections.IndexOf(startSection);
68+
69+
// Get the index of the end paragraph within its section's body and the section itself
70+
int endBodyIndex = endSection.Body.ChildEntities.IndexOf(endPara);
71+
int endSectionIndex = document.Sections.IndexOf(endSection);
72+
73+
// Step 2: Loop through the sections from the start section to the end section
74+
for (int sectionIndex = startSectionIndex; sectionIndex <= endSectionIndex; sectionIndex++)
75+
{
76+
// Get the current section from the document
77+
WSection currentSection = document.Sections[sectionIndex];
78+
79+
// Determine the starting index of body entities in this section
80+
// If it's the first section, start from startBodyIndex; otherwise, start from the beginning
81+
int start = (sectionIndex == startSectionIndex) ? startBodyIndex : 0;
82+
83+
// Determine the ending index of body entities in this section
84+
// If it's the last section, end at endBodyIndex; otherwise, end at the last entity
85+
int end = (sectionIndex == endSectionIndex) ? endBodyIndex : currentSection.Body.ChildEntities.Count - 1;
86+
87+
// Step 3: Loop through the paragraphs from start to end within the current section
88+
for (int paraIndex = start; paraIndex <= end; paraIndex++)
89+
{
90+
// Get the current entity in the section's body
91+
Entity currEntity = currentSection.Body.ChildEntities[paraIndex];
92+
93+
// Check if the current entity is a paragraph
94+
if (currEntity is WParagraph currentPara)
95+
{
96+
// Print the paragraph text to the console
97+
Console.WriteLine(currentPara.Text);
98+
}
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)