Skip to content

Commit 1ffe70f

Browse files
Merge pull request #260 from VijayadharshiniMathiyalagan/ES-916206-How-to-delete-a-content-from-a-Word-document
Added sample to delete specific content from the Word document
2 parents 614ee00 + 5987897 commit 1ffe70f

File tree

10 files changed

+167
-0
lines changed

10 files changed

+167
-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.12.35309.182
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Delete-all-content", "Delete-all-content\Delete-all-content.csproj", "{87F2FD84-C407-45E3-B1C4-7269C4744E19}"
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+
{87F2FD84-C407-45E3-B1C4-7269C4744E19}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{87F2FD84-C407-45E3-B1C4-7269C4744E19}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{87F2FD84-C407-45E3-B1C4-7269C4744E19}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{87F2FD84-C407-45E3-B1C4-7269C4744E19}.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 = {2703251E-EB81-4CA9-A64D-7FDC78CDE6E8}
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>Delete_all_content</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.NET" 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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Syncfusion.DocIO.DLS;
2+
using Syncfusion.DocIO;
3+
4+
using (FileStream inputFileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read))
5+
{
6+
// Opens the template Word docuemnt.
7+
using (WordDocument document = new WordDocument(inputFileStream, FormatType.Docx))
8+
{
9+
//Delete all the content in the Word document
10+
DeleteAllContentInWordDocument(document);
11+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
12+
{
13+
// Saves the modified Word document to the output file stream.
14+
document.Save(outputFileStream, FormatType.Docx);
15+
}
16+
}
17+
}
18+
19+
/// <summary>
20+
/// Deletes all content from every section of the Word document.
21+
/// </summary>
22+
void DeleteAllContentInWordDocument(WordDocument document)
23+
{
24+
// Iterate through all sections in the Word document.
25+
foreach (WSection section in document.Sections)
26+
{
27+
// Access the body of the current section.
28+
WTextBody body = section.Body;
29+
// Remove all child entities from the body.
30+
body.ChildEntities.Clear();
31+
}
32+
}
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.12.35309.182
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Delete-specific-content", "Delete-specific-content\Delete-specific-content.csproj", "{E52F6469-4EF7-4D91-8AD6-9A970E3CF704}"
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+
{E52F6469-4EF7-4D91-8AD6-9A970E3CF704}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{E52F6469-4EF7-4D91-8AD6-9A970E3CF704}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{E52F6469-4EF7-4D91-8AD6-9A970E3CF704}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{E52F6469-4EF7-4D91-8AD6-9A970E3CF704}.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 = {D4C8D559-B3B9-42B9-B796-88F660D9E001}
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>Delete_specific_content</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.NET" 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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Syncfusion.DocIO.DLS;
2+
using Syncfusion.DocIO;
3+
4+
using (FileStream inputFileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read))
5+
{
6+
// Opens the template Word docuemnt.
7+
using (WordDocument document = new WordDocument(inputFileStream, FormatType.Docx))
8+
{
9+
// Deletes content from the 2nd to the 6th index in the text body of the Word document.
10+
DeleteSpecificContent(2, 6, document);
11+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
12+
{
13+
// Saves the modified Word document to the output file stream.
14+
document.Save(outputFileStream, FormatType.Docx);
15+
}
16+
}
17+
}
18+
19+
/// <summary>
20+
/// Deletes content from the specified start index to the end index within the text body of the Word document.
21+
/// </summary>
22+
void DeleteSpecificContent(int startIndex, int endIndex, WordDocument document)
23+
{
24+
// Retrieves the text body of the last section in the Word document.
25+
WTextBody body = document.LastSection.Body;
26+
// Ensures the start and end indices are within valid bounds.
27+
if (startIndex >= 0 && endIndex < body.ChildEntities.Count && startIndex <= endIndex)
28+
{
29+
// Removes items from endIndex to startIndex in reverse order to prevent index shifting issues.
30+
for (int index = endIndex; index >= startIndex; index--)
31+
{
32+
body.ChildEntities.RemoveAt(index);
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)