Skip to content

Commit 3946507

Browse files
Merge pull request #258 from VijayadharshiniMathiyalagan/ES-883902-How-to-maintain-list-format-of-the-source-document-to-destination-document
Added sample for Maintain list format of the source document to destination document
2 parents ac7e8c4 + ff7a83b commit 3946507

File tree

15 files changed

+194
-48
lines changed

15 files changed

+194
-48
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>

Word-document/Merge–multiple-Word-files-in-same-page/.NET/Merge–multiple-Word-files-in-same-page/Output/.gitkeep renamed to Bookmarks/Maintain-source-list-format-after-replace/.NET/Maintain-source-list-format-after-replace/Output/.gitkeep

File renamed without changes.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
ReplaceTextAndMaintainListFormat(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+
ReplaceTextAndMaintainListFormat(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 ReplaceTextAndMaintainListFormat(WordDocument destinationDocument, WordDocument sourceDocument, string tokenToFind, string textBookmark)
39+
{
40+
string bookmarkRef = textBookmark + "_bm";
41+
// Find the text in the destination document where the bookmark start needs to be inserted.
42+
TextSelection start = destinationDocument.Find(tokenToFind, true, true);
43+
if (start != null)
44+
{
45+
// Get the selected text range and its parent paragraph.
46+
WTextRange startText = start.GetAsOneRange();
47+
WParagraph startParagraph = startText.OwnerParagraph;
48+
// Get the index of the selected text range in the paragraph.
49+
int index = startParagraph.Items.IndexOf(startText);
50+
// Remove the selected text at the identified index.
51+
startParagraph.Items.Remove(startText);
52+
// Create a BookmarkStart with a unique reference and insert it at the same index.
53+
BookmarkStart bookmarkStart = new BookmarkStart(destinationDocument, bookmarkRef);
54+
startParagraph.Items.Insert(index, bookmarkStart);
55+
// Append a BookmarkEnd with the same reference to mark the bookmark’s end.
56+
startParagraph.AppendBookmarkEnd(bookmarkRef);
57+
58+
// Check if the specified bookmark exists in the source document.
59+
if (sourceDocument.Bookmarks.FindByName(textBookmark) != null)
60+
{
61+
// Move the navigator to the bookmark in the source document.
62+
BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(sourceDocument);
63+
bookmarksNavigator.MoveToBookmark(textBookmark);
64+
// Extract the content within the bookmark.
65+
WordDocumentPart wordDocumentPart = bookmarksNavigator.GetContent();
66+
67+
// Move the navigator to the newly created bookmark in the destination document.
68+
bookmarksNavigator = new BookmarksNavigator(destinationDocument);
69+
bookmarksNavigator.MoveToBookmark(bookmarkRef);
70+
// Get the paragraph containing the bookmark start in the destination document.
71+
WParagraph destinationPara = bookmarksNavigator.CurrentBookmark.BookmarkStart.OwnerParagraph;
72+
// Store the list style, first-line indent, and left indent of the paragraph.
73+
string listStyleName = destinationPara.ListFormat.CustomStyleName;
74+
float firstLineIndent = destinationPara.ParagraphFormat.FirstLineIndent;
75+
float leftIndent = destinationPara.ParagraphFormat.LeftIndent;
76+
// Replace the bookmark content with the extracted content from the source document.
77+
bookmarksNavigator.ReplaceContent(wordDocumentPart);
78+
// Reapply the original list style and indent settings to the paragraph.
79+
destinationPara.ListFormat.ApplyStyle(listStyleName);
80+
destinationPara.ParagraphFormat.FirstLineIndent = firstLineIndent;
81+
destinationPara.ParagraphFormat.LeftIndent = leftIndent;
82+
}
83+
else
84+
{
85+
// If the bookmark is not found, replace the content with an empty string.
86+
BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(destinationDocument);
87+
bookmarksNavigator.MoveToBookmark(bookmarkRef);
88+
bookmarksNavigator.ReplaceBookmarkContent(string.Empty, true);
89+
}
90+
}
91+
}
92+
}
93+
}

Word-document/Merge–multiple-Word-files-in-same-page/.NET/Merge–multiple-Word-files-in-same-page.sln renamed to Word-document/Merge-multiple-Word-files-in-same-page/.NET/Merge-multiple-Word-files-in-same-page.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
44
VisualStudioVersion = 17.8.34322.80
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mergemultiple-Word-files-in-same-page", "Mergemultiple-Word-files-in-same-page\Mergemultiple-Word-files-in-same-page.csproj", "{C790F761-62BA-49E1-8FF6-E15165CB08C1}"
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Merge-multiple-Word-files-in-same-page", "Merge-multiple-Word-files-in-same-page\Merge-multiple-Word-files-in-same-page.csproj", "{C790F761-62BA-49E1-8FF6-E15165CB08C1}"
77
EndProject
88
Global
99
GlobalSection(SolutionConfigurationPlatforms) = preSolution

Word-document/Merge–multiple-Word-files-in-same-page/.NET/Merge–multiple-Word-files-in-same-page/Data/Addressblock.docx renamed to Word-document/Merge-multiple-Word-files-in-same-page/.NET/Merge-multiple-Word-files-in-same-page/Data/Addressblock.docx

File renamed without changes.

Word-document/Merge–multiple-Word-files-in-same-page/.NET/Merge–multiple-Word-files-in-same-page/Data/Greetings.docx renamed to Word-document/Merge-multiple-Word-files-in-same-page/.NET/Merge-multiple-Word-files-in-same-page/Data/Greetings.docx

File renamed without changes.

Word-document/Merge–multiple-Word-files-in-same-page/.NET/Merge–multiple-Word-files-in-same-page/Data/Salutation.docx renamed to Word-document/Merge-multiple-Word-files-in-same-page/.NET/Merge-multiple-Word-files-in-same-page/Data/Salutation.docx

File renamed without changes.

0 commit comments

Comments
 (0)