Skip to content

Commit 81b41e5

Browse files
Added sample for maintain list format of the source document to destination document
1 parent 7e7812e commit 81b41e5

File tree

6 files changed

+147
-0
lines changed

6 files changed

+147
-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}") = "Maintain-source-list-format-after-replace", "Maintain-source-list-format-after-replace\Maintain-source-list-format-after-replace.csproj", "{162D7557-79E5-4AAA-97B9-4BE8C3F12CA6}"
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+
{162D7557-79E5-4AAA-97B9-4BE8C3F12CA6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{162D7557-79E5-4AAA-97B9-4BE8C3F12CA6}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{162D7557-79E5-4AAA-97B9-4BE8C3F12CA6}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{162D7557-79E5-4AAA-97B9-4BE8C3F12CA6}.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 = {C5C11616-290D-4824-A387-AF3D408F533E}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Maintain_source_list_format_after_replace</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\DestinationDocument.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Data\SourceDocument.docx">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
<None Update="Output\.gitkeep">
23+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
24+
</None>
25+
</ItemGroup>
26+
27+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
namespace Replace_content_with_bookmark
5+
{
6+
internal class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
using (FileStream destinationStream = new FileStream(Path.GetFullPath("Data/DestinationDocument.docx"), FileMode.Open, FileAccess.Read))
11+
{
12+
//Open the destination Word document.
13+
using (WordDocument destinationDocument = new WordDocument(destinationStream, FormatType.Docx))
14+
{
15+
using (FileStream sourceStream = new FileStream(Path.GetFullPath(@"Data/SourceDocument.docx"), FileMode.Open, FileAccess.Read))
16+
{
17+
//Open the source Word document.
18+
using (WordDocument sourceDocument = new WordDocument(sourceStream, FormatType.Docx))
19+
{
20+
//Replace text "Text one" in the destination document with content from the bookmark "bkmk1" in the source document.
21+
DocxReplaceTextWithDocPart(destinationDocument, sourceDocument, "Text one", "bkmk1");
22+
//Replace text "Text two" in the destination document with content from the bookmark "bkmk2" in the source document.
23+
DocxReplaceTextWithDocPart(destinationDocument, sourceDocument, "Text two", "bkmk2");
24+
//Save the modified destination document to the output stream.
25+
using (FileStream output = new FileStream(Path.GetFullPath("Output/Output.docx"), FileMode.Create, FileAccess.Write))
26+
{
27+
destinationDocument.Save(output, FormatType.Docx);
28+
}
29+
}
30+
}
31+
}
32+
}
33+
}
34+
35+
/// <summary>
36+
/// Replaces specific text in a Word document with bookmarked content from another document, maintaining formatting.
37+
/// </summary>
38+
private static void DocxReplaceTextWithDocPart(WordDocument destinationDocument, WordDocument sourceDocument, string tokenToFind, string textBookmark)
39+
{
40+
string bookmarkRef = textBookmark + "_bm";
41+
42+
// Find the text in the destination document where the bookmark start needs to be inserted.
43+
TextSelection start = destinationDocument.Find(tokenToFind, true, true);
44+
if (start != null)
45+
{
46+
// Get the selected text range and its parent paragraph.
47+
WTextRange startText = start.GetAsOneRange();
48+
WParagraph startParagraph = startText.OwnerParagraph;
49+
// Get the index of the selected text range in the paragraph.
50+
int index = startParagraph.Items.IndexOf(startText);
51+
// Remove the selected text at the identified index.
52+
startParagraph.Items.Remove(startText);
53+
// Create a BookmarkStart with a unique reference and insert it at the same index.
54+
BookmarkStart bookmarkStart = new BookmarkStart(destinationDocument, bookmarkRef);
55+
startParagraph.Items.Insert(index, bookmarkStart);
56+
// Append a BookmarkEnd with the same reference to mark the bookmark’s end.
57+
startParagraph.AppendBookmarkEnd(bookmarkRef);
58+
59+
// Check if the specified bookmark exists in the source document.
60+
if (sourceDocument.Bookmarks.FindByName(textBookmark) != null)
61+
{
62+
// Move the navigator to the bookmark in the source document.
63+
BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(sourceDocument);
64+
bookmarksNavigator.MoveToBookmark(textBookmark);
65+
// Extract the content within the bookmark.
66+
WordDocumentPart wordDocumentPart = bookmarksNavigator.GetContent();
67+
68+
// Move the navigator to the newly created bookmark in the destination document.
69+
bookmarksNavigator = new BookmarksNavigator(destinationDocument);
70+
bookmarksNavigator.MoveToBookmark(bookmarkRef);
71+
// Get the paragraph containing the bookmark start in the destination document.
72+
WParagraph destinationPara = bookmarksNavigator.CurrentBookmark.BookmarkStart.OwnerParagraph;
73+
// Store the list style, first-line indent, and left indent of the paragraph.
74+
string listStyleName = destinationPara.ListFormat.CustomStyleName;
75+
float firstLineIndent = destinationPara.ParagraphFormat.FirstLineIndent;
76+
float leftIndent = destinationPara.ParagraphFormat.LeftIndent;
77+
// Replace the bookmark content with the extracted content from the source document.
78+
bookmarksNavigator.ReplaceContent(wordDocumentPart);
79+
// Reapply the original list style and indent settings to the paragraph.
80+
destinationPara.ListFormat.ApplyStyle(listStyleName);
81+
destinationPara.ParagraphFormat.FirstLineIndent = firstLineIndent;
82+
destinationPara.ParagraphFormat.LeftIndent = leftIndent;
83+
}
84+
else
85+
{
86+
// If the bookmark is not found, replace the content with an empty string.
87+
BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(destinationDocument);
88+
bookmarksNavigator.MoveToBookmark(bookmarkRef);
89+
bookmarksNavigator.ReplaceBookmarkContent(string.Empty, true);
90+
}
91+
}
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)