Skip to content

Commit ab650e2

Browse files
committed
Added Find text and make it editable samples
1 parent 87307a4 commit ab650e2

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-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.14.36518.9 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Find-Text-and-Make-it-Editable", "Find-Text-and-Make-it-Editable\Find-Text-and-Make-it-Editable.csproj", "{BAA6F342-2A27-4644-8D7D-1AC2DDDEA5B3}"
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+
{BAA6F342-2A27-4644-8D7D-1AC2DDDEA5B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{BAA6F342-2A27-4644-8D7D-1AC2DDDEA5B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{BAA6F342-2A27-4644-8D7D-1AC2DDDEA5B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{BAA6F342-2A27-4644-8D7D-1AC2DDDEA5B3}.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 = {CDAFC3B6-0F05-4FBC-8B77-214AF0CA9392}
24+
EndGlobalSection
25+
EndGlobal
Binary file not shown.
Lines changed: 24 additions & 0 deletions
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>Find_Text_and_Make_it_Editable</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>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using Syncfusion.DocIO.DLS;
2+
3+
namespace Find_Text_and_Make_it_Editable
4+
{
5+
public class Program
6+
{
7+
public static void Main(string[] args)
8+
{
9+
//Loads template document
10+
WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx"));
11+
// Find all instances of the target text (text, case-insensitive, whole word match)
12+
TextSelection[] textSelections = document.FindAll("Adventure Works Cycles", false, true);
13+
14+
// Append editable ranges around each matched text.
15+
foreach (var selection in textSelections)
16+
{
17+
AppendEditableRange(selection);
18+
}
19+
// save and close the document
20+
document.Save(Path.GetFullPath(Path.GetFullPath(@"../../../Output/Result.docx")));
21+
document.Close();
22+
}
23+
24+
public static void AppendEditableRange(TextSelection selection)
25+
{
26+
// Get the text range from the selection.
27+
WTextRange[] textRanges = selection.GetRanges();
28+
if (textRanges.Length > 0)
29+
{
30+
// Get the first and last text ranges in the selection.
31+
WTextRange startTextRange = textRanges[0];
32+
WTextRange endTextRange = textRanges[textRanges.Length - 1];
33+
// Get the paragraph that owns the start text range.
34+
WParagraph paragraph = startTextRange.OwnerParagraph;
35+
// Create a new EditableRangeStart and assign it to everyone by default.
36+
EditableRangeStart editableRangeStart = new EditableRangeStart(paragraph.Document);
37+
editableRangeStart.EditorGroup = EditorType.Everyone;
38+
// Find the index of the start text range within the paragraph's child entities.
39+
int startTextRangeIndex = paragraph.ChildEntities.IndexOf(startTextRange);
40+
// Insert the editable range start before the start text range.
41+
paragraph.ChildEntities.Insert(startTextRangeIndex, editableRangeStart);
42+
// Create a new EditableRangeEnd linked to the start.
43+
EditableRangeEnd editableRangeEnd = new EditableRangeEnd(paragraph.Document, editableRangeStart);
44+
// Find the index of the end text range and insert the editable range end after it.
45+
int endTextRangeIndex = paragraph.ChildEntities.IndexOf(endTextRange);
46+
paragraph.ChildEntities.Insert(endTextRangeIndex + 1, editableRangeEnd);
47+
}
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)