Skip to content

Commit 00528e8

Browse files
committed
Added the sample
1 parent 637f2eb commit 00528e8

File tree

5 files changed

+163
-0
lines changed

5 files changed

+163
-0
lines changed
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}") = "Delete-Bracketed-Content-Up-To-Specific-Word", "Delete-Bracketed-Content-Up-To-Specific-Word\Delete-Bracketed-Content-Up-To-Specific-Word.csproj", "{793CDE3B-3B7F-4439-90F0-07837F5BB570}"
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+
{793CDE3B-3B7F-4439-90F0-07837F5BB570}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{793CDE3B-3B7F-4439-90F0-07837F5BB570}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{793CDE3B-3B7F-4439-90F0-07837F5BB570}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{793CDE3B-3B7F-4439-90F0-07837F5BB570}.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 = {50FFD4ED-8E56-4074-8464-3524094309F7}
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>Delete_Bracketed_Content_Up_To_Specific_Word</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+

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
using Syncfusion.DocIO.DLS;
2+
3+
namespace Delete_bracketed_content_up_to_specific_word
4+
{
5+
class Program
6+
{
7+
public static void Main(string[] args)
8+
{
9+
// Load the existing word document
10+
using(WordDocument document = new WordDocument(@"Data\Input.docx"))
11+
{
12+
// Find the phrase using Find API (Specific Word, case-insensitive, whole-word match)
13+
TextSelection selection = document.Find("Importadores", true, true);
14+
// Proceed only if the phrase is found
15+
if (selection != null)
16+
{
17+
// Get the paragraph that contains the selected word
18+
WParagraph paragraph = selection.GetAsOneRange().OwnerParagraph;
19+
// Get a owner section
20+
WSection ownerSection = (WSection)paragraph.OwnerTextBody.Owner;
21+
// Find the position (index) of the paragraph in the section
22+
int phraseParaIndex = ownerSection.Body.ChildEntities.IndexOf(paragraph);
23+
// Find the position of the word inside the paragraph
24+
int phraseEntityIndex = paragraph.ChildEntities.IndexOf(selection.GetAsOneRange());
25+
// Set how many paragraphs before the word we want to check
26+
int maxPreviousParagraphs = 6;
27+
// Call a method to remove content inside brackets before the word
28+
RemoveBlock(document, paragraph, phraseParaIndex, phraseEntityIndex, ownerSection, maxPreviousParagraphs);
29+
// Save the updated document to a file
30+
document.Save(@"../../../Output/Output.docx");
31+
}
32+
}
33+
}
34+
/// <summary>
35+
/// Removes content enclosed in brackets from previous paragraphs up to a specific word in the document.
36+
/// </summary>
37+
/// <param name="document">The Word document to modify.</param>
38+
/// <param name="paragraph">The paragraph that contains the specific word or phrase.</param>
39+
/// <param name="phraseParaIndex">The index of the paragraph containing the specific word.</param>
40+
/// <param name="phraseEntityIndex">The index of the word within the paragraph.</param>
41+
/// <param name="ownerSection">The section that contains the paragraph.</param>
42+
/// <param name="maxPreviousParagraphs">The number of previous paragraphs to check for bracketed content.</param>
43+
private static void RemoveBlock(WordDocument document, WParagraph paragraph, int phraseParaIndex, int phraseEntityIndex, WSection ownerSection, int maxPreviousParagraphs)
44+
{
45+
// Initialize state
46+
int bracketCount = 0;
47+
bool isBracketFound = false;
48+
// Create a unique name for the temporary bookmark
49+
string bookmarkName = "Remove" + Guid.NewGuid().ToString();
50+
// Store the paragraph where the opening bracket '[' is found
51+
WParagraph openingBracketParagraph = null;
52+
// Store the character position of the opening bracket '['
53+
int openingBracketCharIndex = 0;
54+
// Loop backwards through previous paragraphs
55+
for (int i = phraseParaIndex; i >= 0 && i > phraseParaIndex - maxPreviousParagraphs; i--)
56+
{
57+
WParagraph currentParagraph = ownerSection.Paragraphs[i];
58+
// If it's the phrase paragraph, start from the word's position; otherwise, start from the end
59+
int start = i == phraseParaIndex ? phraseEntityIndex : currentParagraph.ChildEntities.Count - 1;
60+
// Loop through entities in reverse
61+
for (int j = start; j >= 0; j--)
62+
{
63+
// Check if the entity is a text range
64+
if (currentParagraph.ChildEntities[j] is WTextRange textRange)
65+
{
66+
string text = textRange.Text;
67+
// Loop through characters in reverse
68+
for (int k = text.Length - 1; k >= 0; k--)
69+
{
70+
char ch = text[k];
71+
// If we find a closing bracket
72+
if (ch == ']')
73+
{
74+
isBracketFound = true;
75+
bracketCount++;
76+
}
77+
// If we find an opening bracket
78+
else if (ch == '[')
79+
{
80+
// If there's no matching closing bracket, stop processing
81+
if (bracketCount == 0)
82+
return;
83+
84+
// Reduce bracket count
85+
bracketCount--;
86+
// Save the position of the opening bracket
87+
openingBracketCharIndex = k;
88+
// Store the paragraph that contains this opening bracket,
89+
openingBracketParagraph = currentParagraph;
90+
}
91+
}
92+
}
93+
}
94+
}
95+
// When a bracket is found and bracket count is 0, it means a complete and valid bracket pair is detected.
96+
if (isBracketFound && bracketCount == 0)
97+
{
98+
// Create and insert a bookmark start at the opening bracket position
99+
BookmarkStart bookmarkStart = new BookmarkStart(document, bookmarkName);
100+
openingBracketParagraph .ChildEntities.Insert(openingBracketCharIndex, bookmarkStart);
101+
// Create and insert a bookmark end after the specific word
102+
BookmarkEnd bookmarkEnd = new BookmarkEnd(document, bookmarkName);
103+
paragraph.ChildEntities.Insert(phraseEntityIndex + 1, bookmarkEnd);
104+
// Use Bookmarknavigator to delete content between the bookmarks
105+
BookmarksNavigator navigator = new BookmarksNavigator(document);
106+
navigator.MoveToBookmark(bookmarkName);
107+
navigator.DeleteBookmarkContent(true);
108+
// Remove the temporary bookmark from the document
109+
document.Bookmarks.Remove(navigator.CurrentBookmark);
110+
}
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)