Skip to content

Commit 2ebd312

Browse files
Merge pull request #471 from SyncfusionExamples/ES-959948-Make-specific-paragraphs-editable-in-a-protected-Word-document
Add samples for make editable in a protected Word document
2 parents 7046f21 + ab650e2 commit 2ebd312

File tree

10 files changed

+195
-0
lines changed

10 files changed

+195
-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}") = "Add-Editable-range-to-specific-paragraph", "Add-Editable-range-to-specific-paragraph\Add-Editable-range-to-specific-paragraph.csproj", "{FA1949AA-B35C-4EB6-A763-7B9FAA8207FE}"
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+
{FA1949AA-B35C-4EB6-A763-7B9FAA8207FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{FA1949AA-B35C-4EB6-A763-7B9FAA8207FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{FA1949AA-B35C-4EB6-A763-7B9FAA8207FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{FA1949AA-B35C-4EB6-A763-7B9FAA8207FE}.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 = {48134F0E-F228-4631-90C5-02A0A3A5DD6B}
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>Add_Editable_range_to_specific_paragraph</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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Syncfusion.DocIO.DLS;
2+
using Syncfusion.DocIO;
3+
4+
namespace Add_Editable_range_to_specific_paragraph
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
// Load the Word document
11+
using (WordDocument document = new WordDocument(Path.GetFullPath("Data/Template.docx")))
12+
{
13+
// Access the first section of the document
14+
WSection section = document.Sections[0];
15+
// Insert an editable range at index 1 in the 2nd paragraph
16+
AddEditableRange(document, section.Paragraphs[1], 1);
17+
// Insert an editable range at index 2 in the 4th paragraph
18+
AddEditableRange(document, section.Paragraphs[3], 1);
19+
// Insert an editable range at index 4 in the 12th paragraph
20+
AddEditableRange(document, section.Paragraphs[11], 4);
21+
// Save the modified document to the new file
22+
document.Save(Path.GetFullPath(@"../../../Output/Result.docx"));
23+
}
24+
}
25+
/// <summary>
26+
/// Inserts an editable range at a specific index within a paragraph.
27+
/// </summary>
28+
/// <param name="document">The Word document instance</param>
29+
/// <param name="paragraph">The paragraph where the editable range will be inserted.</param>
30+
/// <param name="index">The index within the paragraph's child entities to insert the editable range.</param>
31+
private static void AddEditableRange(WordDocument document, WParagraph paragraph, int index)
32+
{
33+
// Create the start of the editable range
34+
EditableRangeStart editableRangeStart = new EditableRangeStart(document);
35+
// Insert the editable range start at the specified index in the paragraph
36+
paragraph.ChildEntities.Insert(index, editableRangeStart);
37+
// Set the editor group to allow everyone to edit this range
38+
editableRangeStart.EditorGroup = EditorType.Everyone;
39+
// Create the end of the editable range
40+
EditableRangeEnd editableRangeEnd = new EditableRangeEnd(document);
41+
// Insert the editable range end after the editable content
42+
paragraph.ChildEntities.Insert(index + 2, editableRangeEnd);
43+
}
44+
}
45+
}
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)