Skip to content

Commit eafde5c

Browse files
Merge pull request #308 from SindhuRSF4549/main
Added Delete-content-between-two-bookmarks sample
2 parents 49bf280 + 966cf3d commit eafde5c

File tree

7 files changed

+232
-0
lines changed

7 files changed

+232
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
5+
</startup>
6+
</configuration>
Binary file not shown.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{0DBD5B73-BB6B-419B-AF29-F5F30F4DF524}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>Delete_content_between_two_bookmarks</RootNamespace>
10+
<AssemblyName>Delete-content-between-two-bookmarks</AssemblyName>
11+
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
<Deterministic>true</Deterministic>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="Syncfusion.Compression.Base, Version=27.1462.53.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
37+
<HintPath>packages\Syncfusion.Compression.Base.27.1.53\lib\net462\Syncfusion.Compression.Base.dll</HintPath>
38+
</Reference>
39+
<Reference Include="Syncfusion.DocIO.Base, Version=27.1462.53.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
40+
<HintPath>packages\Syncfusion.DocIO.WinForms.27.1.53\lib\net462\Syncfusion.DocIO.Base.dll</HintPath>
41+
</Reference>
42+
<Reference Include="Syncfusion.Licensing, Version=27.1462.53.0, Culture=neutral, PublicKeyToken=632609b4d040f6b4, processorArchitecture=MSIL">
43+
<HintPath>packages\Syncfusion.Licensing.27.1.53\lib\net462\Syncfusion.Licensing.dll</HintPath>
44+
</Reference>
45+
<Reference Include="Syncfusion.OfficeChart.Base, Version=27.1462.53.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
46+
<HintPath>packages\Syncfusion.OfficeChart.Base.27.1.53\lib\net462\Syncfusion.OfficeChart.Base.dll</HintPath>
47+
</Reference>
48+
<Reference Include="System" />
49+
<Reference Include="System.Core" />
50+
<Reference Include="System.Xml.Linq" />
51+
<Reference Include="System.Data.DataSetExtensions" />
52+
<Reference Include="Microsoft.CSharp" />
53+
<Reference Include="System.Data" />
54+
<Reference Include="System.Net.Http" />
55+
<Reference Include="System.Xml" />
56+
</ItemGroup>
57+
<ItemGroup>
58+
<Compile Include="Program.cs" />
59+
<Compile Include="Properties\AssemblyInfo.cs" />
60+
</ItemGroup>
61+
<ItemGroup>
62+
<None Include="App.config" />
63+
<None Include="packages.config" />
64+
</ItemGroup>
65+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
66+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35527.113 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Delete-content-between-two-bookmarks", "Delete-content-between-two-bookmarks.csproj", "{0DBD5B73-BB6B-419B-AF29-F5F30F4DF524}"
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+
{0DBD5B73-BB6B-419B-AF29-F5F30F4DF524}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{0DBD5B73-BB6B-419B-AF29-F5F30F4DF524}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{0DBD5B73-BB6B-419B-AF29-F5F30F4DF524}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{0DBD5B73-BB6B-419B-AF29-F5F30F4DF524}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using Syncfusion.DocIO.DLS;
2+
using Syncfusion.DocIO;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using System.IO;
9+
10+
namespace Delete_content_between_two_bookmarks
11+
{
12+
internal class Program
13+
{
14+
static void Main(string[] args)
15+
{
16+
using (WordDocument document = new WordDocument(@"../../Data/Template.docx", FormatType.Docx))
17+
{
18+
//Delete the text between two different existing bookmarks.
19+
DeleteBookmarkContent(document, "BM1", "BM2");
20+
//Save and close the Word document.
21+
document.Save(@"../../Data/Result.docx", FormatType.Docx);
22+
}
23+
}
24+
/// <summary>
25+
/// Deletes the content of a bookmark located between two other bookmarks in the Word document.
26+
/// A temporary bookmark is created between the two specified bookmarks to identify the content to delete.
27+
/// </summary>
28+
/// <param name="document">The <see cref="WordDocument"/> from which the bookmark content will be deleted.</param>
29+
/// <param name="bookmark1">The name of the first bookmark used to define the starting point of the content to delete.</param>
30+
/// <param name="bookmark2">The name of the second bookmark used to define the ending point of the content to delete.</param>
31+
public static void DeleteBookmarkContent(WordDocument document, String bookmark1, String bookmark2)
32+
{
33+
//Temp Bookmark.
34+
String tempBookmarkName = "tempBookmark";
35+
36+
#region Insert bookmark start after bookmark1.
37+
//Get the bookmark instance by using FindByName method of BookmarkCollection with bookmark name.
38+
Bookmark firstBookmark = document.Bookmarks.FindByName(bookmark1);
39+
//Access the bookmark end’s owner paragraph by using bookmark.
40+
WParagraph firstBookmarkOwnerPara = firstBookmark.BookmarkEnd.OwnerParagraph;
41+
//Get the index of bookmark end of bookmark1.
42+
int index = firstBookmarkOwnerPara.Items.IndexOf(firstBookmark.BookmarkEnd);
43+
//Create and add new bookmark start after bookmark1.
44+
BookmarkStart newBookmarkStart = new BookmarkStart(document, tempBookmarkName);
45+
firstBookmarkOwnerPara.ChildEntities.Insert(index + 1, newBookmarkStart);
46+
#endregion
47+
48+
#region Insert bookmark end before bookmark2.
49+
//Get the bookmark instance by using FindByName method of BookmarkCollection with bookmark name.
50+
Bookmark secondBookmark = document.Bookmarks.FindByName(bookmark2);
51+
//Access the bookmark start’s owner paragraph by using bookmark.
52+
WParagraph secondBookmarkOwnerPara = secondBookmark.BookmarkStart.OwnerParagraph;
53+
//Get the index of bookmark start of bookmark2.
54+
index = secondBookmarkOwnerPara.Items.IndexOf(secondBookmark.BookmarkStart);
55+
//Create and add new bookmark end before bookmark2.
56+
BookmarkEnd newBookmarkEnd = new BookmarkEnd(document, tempBookmarkName);
57+
secondBookmarkOwnerPara.ChildEntities.Insert(index, newBookmarkEnd);
58+
#endregion
59+
60+
#region Select bookmark content and delete.
61+
//Create the bookmark navigator instance to access the newly created bookmark.
62+
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
63+
//Move the virtual cursor to the location of the temp bookmark.
64+
bookmarkNavigator.MoveToBookmark(tempBookmarkName);
65+
//Save bookmark content as Word document.
66+
ExportBookmarkContentToDocument(bookmarkNavigator);
67+
//Replace the bookmark content.
68+
bookmarkNavigator.DeleteBookmarkContent(false);
69+
#endregion
70+
71+
#region Remove that temporary bookmark.
72+
//Get the bookmark instance by using FindByName method of BookmarkCollection with bookmark name.
73+
Bookmark bookmark = document.Bookmarks.FindByName(tempBookmarkName);
74+
//Remove the temp bookmark named from Word document.
75+
document.Bookmarks.Remove(bookmark);
76+
#endregion
77+
}
78+
/// <summary>
79+
/// Extracts the content of the current bookmark and saves it as a separate Word document (.docx).
80+
/// </summary>
81+
/// <param name="bookmarkNavigator">The <see cref="BookmarksNavigator"/> instance used to navigate and access the bookmark content.</param>
82+
private static void ExportBookmarkContentToDocument(BookmarksNavigator bookmarkNavigator)
83+
{
84+
//Get the bookmark content as WordDocumentPart.
85+
WordDocumentPart documentPart = bookmarkNavigator.GetContent();
86+
//Save the WordDocumentPart as separate Word document
87+
using (WordDocument newDocument = documentPart.GetAsWordDocument())
88+
{
89+
string tempBookmarkName = bookmarkNavigator.CurrentBookmark.Name;
90+
//Save the Word document to file stream.
91+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath($@"../../Data/{tempBookmarkName}.docx"), FileMode.Create, FileAccess.Write))
92+
{
93+
newDocument.Save(outputFileStream, FormatType.Docx);
94+
}
95+
}
96+
}
97+
}
98+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Delete-content-between-two-bookmarks")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Delete-content-between-two-bookmarks")]
13+
[assembly: AssemblyCopyright("Copyright © 2024")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("0dbd5b73-bb6b-419b-af29-f5f30f4df524")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
[assembly: AssemblyVersion("1.0.0.0")]
33+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="Syncfusion.Compression.Base" version="27.1.53" targetFramework="net462" />
4+
<package id="Syncfusion.DocIO.WinForms" version="27.1.53" targetFramework="net462" />
5+
<package id="Syncfusion.Licensing" version="27.1.53" targetFramework="net462" />
6+
<package id="Syncfusion.OfficeChart.Base" version="27.1.53" targetFramework="net462" />
7+
</packages>

0 commit comments

Comments
 (0)