forked from SyncfusionExamples/DocIO-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added sample to clone and add a paragraph with bookmarks in the same …
…Word document
- Loading branch information
1 parent
f4b8cb0
commit 484afea
Showing
5 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
...ks/Preserve-bookmarks-from-cloned-content/.NET/Preserve-bookmarks-from-cloned-content.sln
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 16 | ||
VisualStudioVersion = 16.0.31911.196 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Preserve-bookmarks-from-cloned-content", "Preserve-bookmarks-from-cloned-content\Preserve-bookmarks-from-cloned-content.csproj", "{C17B90BC-F559-456B-B189-90B53FF6CDD4}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {EF357FC6-E9E5-4E3C-B932-43F727BE1DE4} | ||
EndGlobalSection | ||
EndGlobal |
Binary file added
BIN
+13.6 KB
...bookmarks-from-cloned-content/.NET/Preserve-bookmarks-from-cloned-content/Data/Input.docx
Binary file not shown.
1 change: 1 addition & 0 deletions
1
...bookmarks-from-cloned-content/.NET/Preserve-bookmarks-from-cloned-content/Output/.gitkeep
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
22 changes: 22 additions & 0 deletions
22
....NET/Preserve-bookmarks-from-cloned-content/Preserve-bookmarks-from-cloned-content.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<RootNamespace>Preserve_bookmarks_from_cloned_content</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="Data\Input.docx"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
<None Update="Output\.gitkeep"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
61 changes: 61 additions & 0 deletions
61
...erve-bookmarks-from-cloned-content/.NET/Preserve-bookmarks-from-cloned-content/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using Syncfusion.DocIO; | ||
using Syncfusion.DocIO.DLS; | ||
using System.IO; | ||
|
||
namespace Preserve_bookmarks_from_cloned_content | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Input.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) | ||
{ | ||
//Open an existing Word document. | ||
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Automatic)) | ||
{ | ||
//Create the bookmark navigator instance to access the bookmark. | ||
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document); | ||
//Moves the virtual cursor to the location before the end of the bookmark "MainContent". | ||
bookmarkNavigator.MoveToBookmark("MainContent"); | ||
//Get the content of the "MainContent" bookmark as WordDocumentPart. | ||
WordDocumentPart wordDocumentPart = bookmarkNavigator.GetContent(); | ||
|
||
//Find the "MainContent" bookmark in the document by its name. | ||
Bookmark bookmark = document.Bookmarks.FindByName("MainContent"); | ||
//Identify the parent text body of the bookmark. | ||
WTextBody textbody = bookmark.BookmarkStart.OwnerParagraph.Owner as WTextBody; | ||
//Determine the index of the paragraph containing the bookmark. | ||
int index = textbody.ChildEntities.IndexOf(bookmark.BookmarkStart.OwnerParagraph); | ||
|
||
//Remove the "MainContent" bookmark from the document. | ||
document.Bookmarks.Remove(bookmark); | ||
//Remove inner bookmarks (SubContent1, SubContent2, SubContent3) from the document. | ||
bookmark = document.Bookmarks.FindByName("SubContent1"); | ||
document.Bookmarks.Remove(bookmark); | ||
bookmark = document.Bookmarks.FindByName("SubContent2"); | ||
document.Bookmarks.Remove(bookmark); | ||
bookmark = document.Bookmarks.FindByName("SubContent3"); | ||
document.Bookmarks.Remove(bookmark); | ||
|
||
//Insert the cloned content of the "MainContent" bookmark after the original bookmark paragraph. | ||
if (wordDocumentPart.Sections[0].ChildEntities[0] is WTextBody) | ||
{ | ||
WTextBody clonedTextBody = wordDocumentPart.Sections[0].ChildEntities[0] as WTextBody; | ||
for (int i = 0, j = index + 1; i < clonedTextBody.ChildEntities.Count; i++, j++) | ||
{ | ||
textbody.ChildEntities.Insert(j, clonedTextBody.ChildEntities[i]); | ||
} | ||
} | ||
//Close the WordDocumentPart instance. | ||
wordDocumentPart.Close(); | ||
//Create file stream. | ||
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.docx"), FileMode.Create, FileAccess.ReadWrite)) | ||
{ | ||
//Save the Word document to file stream. | ||
document.Save(outputFileStream, FormatType.Docx); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |