Skip to content

Commit 314b7db

Browse files
ES-992085-Changes added
1 parent 1094fa0 commit 314b7db

5 files changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Solution>
2+
<Project Path="Remove_editablerange/Remove_editablerange.csproj" />
3+
</Solution>
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
namespace Remove_editablerange
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
11+
{
12+
//Creates a new Word document.
13+
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Automatic))
14+
{
15+
// Loop through all bookmarks in the document
16+
for (int i = 0; i < document.Bookmarks.Count; i++)
17+
{
18+
Bookmark bookmark = document.Bookmarks[i];
19+
// Check and remove editable ranges within the bookmark
20+
RemoveEditableRange(document, bookmark.Name);
21+
}
22+
//Create a file stream.
23+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
24+
{
25+
//Save the Word document to the file stream.
26+
document.Save(outputFileStream, FormatType.Docx);
27+
}
28+
}
29+
}
30+
}
31+
32+
/// <summary>
33+
/// Removes any editable ranges found within the bookmark
34+
/// </summary>
35+
/// <param name="document"></param>
36+
/// <param name="bookmarkName"></param>
37+
private static void RemoveEditableRange(WordDocument document, string bookmarkName)
38+
{
39+
// Create a Bookmark Navigator
40+
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
41+
// Move to the bookmark
42+
bookmarkNavigator.MoveToBookmark(bookmarkName);
43+
// Get the content inside the bookmark
44+
WordDocumentPart bookmarkContent = bookmarkNavigator.GetContent();
45+
// Loop through all sections in the bookmark content
46+
for (int s = 0; s < bookmarkContent.Sections.Count; s++)
47+
{
48+
WSection section = bookmarkContent.Sections[s];
49+
// Iterate through all entities in the section body (paragraphs, tables, etc.).
50+
for (int i = 0; i < section.Body.ChildEntities.Count; i++)
51+
{
52+
IEntity entity = section.Body.ChildEntities[i];
53+
54+
if (entity is WParagraph)
55+
{
56+
WParagraph paragraph = entity as WParagraph;
57+
// Loop through all child entities in the paragraph
58+
for (int j = 0; j < paragraph.ChildEntities.Count; j++)
59+
{
60+
Entity item = paragraph.ChildEntities[j];
61+
// Check if the item is the start of an editable range
62+
if (item is EditableRangeStart)
63+
{
64+
// Find the editable range by ID and remove it
65+
EditableRange editableRange = document.EditableRanges.FindById((item as EditableRangeStart).Id);
66+
if (editableRange != null)
67+
document.EditableRanges.Remove(editableRange);
68+
}
69+
}
70+
}
71+
}
72+
}
73+
}
74+
}
75+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<None Update="Data\Template.docx">
16+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
17+
</None>
18+
<None Update="Output\.gitkeep">
19+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
20+
</None>
21+
</ItemGroup>
22+
23+
</Project>

0 commit comments

Comments
 (0)