Skip to content

Commit 660b2fa

Browse files
Merge pull request #371 from SyncfusionExamples/ES-876611-Replace-list-restart-numbering-HTML-with-text
ES-876611- Add the sample Replace-text-with-HTML-list
2 parents 78b57dc + aaf69d1 commit 660b2fa

File tree

6 files changed

+249
-0
lines changed

6 files changed

+249
-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.11.35327.3
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Replace-text-with-HTML-list", "Replace-text-with-HTML-list\Replace-text-with-HTML-list.csproj", "{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}"
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+
{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}.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 = {ADDC6FEE-8718-4B00-B091-6C4DC0CBF62A}
24+
EndGlobalSection
25+
EndGlobal
Binary file not shown.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<p>
2+
<br/>
3+
</p>
4+
<ol level="1">
5+
<li>
6+
<p>
7+
<span>Mountain 200</span>
8+
</p>
9+
</li>
10+
<li>
11+
<p>
12+
<span>
13+
<span>Mountain 300</span>
14+
<br/>
15+
</span>
16+
</p>
17+
</li>
18+
</ol>
19+
<p>
20+
<br/>
21+
</p>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
namespace Replace_text_with_HTML_list
5+
{
6+
internal class Program
7+
{
8+
// List to store the names of different list styles used in the document.
9+
static List<string> listStyleNames = new List<string>();
10+
static void Main(string[] args)
11+
{
12+
// Load the input Word document from file stream
13+
using (FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read))
14+
{
15+
// Open the Word document
16+
using (WordDocument document = new WordDocument(docStream, FormatType.Docx))
17+
{
18+
// Load the input Word document from file stream
19+
using (FileStream htmlStream = new FileStream(Path.GetFullPath(@"Data/sample.html"), FileMode.Open, FileAccess.Read))
20+
{
21+
// Open the Word document
22+
using (WordDocument replaceDoc = new WordDocument(htmlStream, FormatType.Html))
23+
{
24+
//Replace the first word with HTML file content
25+
ReplaceText(document, "Tag1", replaceDoc, true);
26+
//Replace the second word with HTML file content
27+
ReplaceText(document, "Tag2", replaceDoc, false);
28+
29+
// Save the modified document to a new file
30+
using (FileStream docStream1 = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.Write))
31+
{
32+
document.Save(docStream1, FormatType.Docx);
33+
}
34+
}
35+
}
36+
}
37+
}
38+
}
39+
private static void ReplaceText(WordDocument document, string findText, WordDocument replaceDoc, bool isFirstReplace)
40+
{
41+
TextSelection selection = document.Find(findText, true, true);
42+
if (selection != null)
43+
{
44+
//Get the textrange
45+
WTextRange textRange = selection.GetAsOneRange();
46+
//Get the owner paragraph
47+
WParagraph ownerPara = textRange.OwnerParagraph;
48+
49+
//For first time replacement alone.
50+
if (isFirstReplace)
51+
{
52+
//Get the index of the textrange
53+
int index = ownerPara.ChildEntities.IndexOf(textRange);
54+
//Add the bookmark start before the textrange
55+
BookmarkStart bookmarkStart = new BookmarkStart(document, "Bkmk");
56+
ownerPara.ChildEntities.Insert(index, bookmarkStart);
57+
//Increment the index
58+
index++;
59+
//Add the bookmark end after the textrange
60+
BookmarkEnd bookmarkEnd = new BookmarkEnd(document, "Bkmk");
61+
ownerPara.ChildEntities.Insert(index + 1, bookmarkEnd);
62+
//Replace the text with HTML content
63+
document.Replace(findText, replaceDoc, true, true);
64+
//Navigate to the bookmark content
65+
BookmarksNavigator navigator = new BookmarksNavigator(document);
66+
navigator.MoveToBookmark("Bkmk");
67+
//Get the bookmark content
68+
TextBodyPart bodyPart = navigator.GetBookmarkContent();
69+
//Get the list of list styles
70+
GetListStyleName(bodyPart.BodyItems);
71+
//Remove the bookmark
72+
Bookmark bookmark = document.Bookmarks.FindByName("Bkmk");
73+
document.Bookmarks.Remove(bookmark);
74+
}
75+
else
76+
{
77+
//Get the next sibiling of the owner paragraph
78+
IEntity nextSibiling = ownerPara.NextSibling;
79+
//Get the owner paragraph index as start index
80+
int startIndex = ownerPara.OwnerTextBody.ChildEntities.IndexOf(ownerPara);
81+
//Replace the text with HTML content
82+
document.Replace(findText, replaceDoc, true, true);
83+
//Get the end index
84+
//If the next sibiling is present then it is the end index, else the child entities count
85+
int endIndex = nextSibiling != null ? ownerPara.OwnerTextBody.ChildEntities.IndexOf(nextSibiling)
86+
: ownerPara.OwnerTextBody.ChildEntities.Count;
87+
//Restart the numbering
88+
RestartNumbering(startIndex, endIndex, document.Sections[0].Body.ChildEntities);
89+
}
90+
}
91+
}
92+
//Get the list style names from the collection
93+
private static void GetListStyleName(EntityCollection collection)
94+
{
95+
//Iterate through the collection
96+
foreach (Entity entity in collection)
97+
{
98+
switch (entity.EntityType)
99+
{
100+
//Entity is paragraph
101+
case EntityType.Paragraph:
102+
WParagraph wParagraph = (WParagraph)entity;
103+
//Check whether the paragrah has list format with numbered type which is not in the collection list.
104+
if (wParagraph.ListFormat.CurrentListLevel != null
105+
&& wParagraph.ListFormat.ListType == ListType.Numbered
106+
&& !listStyleNames.Contains(wParagraph.ListFormat.CurrentListStyle.Name))
107+
//Add the list style name to the collection list
108+
listStyleNames.Add(wParagraph.ListFormat.CurrentListStyle.Name);
109+
break;
110+
//Entity is Table
111+
case EntityType.Table:
112+
WTable table = (WTable)entity;
113+
//Iterate thorugh rows
114+
foreach (WTableRow row in table.Rows)
115+
{
116+
//Iterate through cells
117+
foreach (WTableCell cell in row.Cells)
118+
{
119+
//Get the list style name
120+
GetListStyleName(cell.ChildEntities);
121+
}
122+
}
123+
break;
124+
}
125+
}
126+
}
127+
//Restart the numbering for replaced items
128+
private static void RestartNumbering(int startIndex, int endIndex, EntityCollection collection)
129+
{
130+
//Local value
131+
string listName = string.Empty;
132+
for (int i = startIndex; i < endIndex; i++)
133+
{
134+
Entity entity = collection[i];
135+
switch (entity.EntityType)
136+
{
137+
//Entity is Paragraph
138+
case EntityType.Paragraph:
139+
WParagraph wParagraph = (WParagraph)entity;
140+
//Check whether the paragraph have current list level and the same list name in the collection list.
141+
if (wParagraph.ListFormat.CurrentListLevel != null
142+
&& listStyleNames.Contains(wParagraph.ListFormat.CurrentListStyle.Name))
143+
{
144+
//If the local name is not equal to current list name, then restart the numbering
145+
if (listName != wParagraph.ListFormat.CurrentListStyle.Name)
146+
{
147+
//Set the current list name as local name
148+
listName = wParagraph.ListFormat.CurrentListStyle.Name;
149+
//Enable restart numbering
150+
wParagraph.ListFormat.RestartNumbering = true;
151+
}
152+
//If the local name and current list name are equal then continue list numbering
153+
else
154+
wParagraph.ListFormat.ContinueListNumbering();
155+
}
156+
break;
157+
//Entity is table
158+
case EntityType.Table:
159+
WTable table = (WTable)entity;
160+
//Iterate through rows
161+
foreach (WTableRow row in table.Rows)
162+
{
163+
//Iterate thorugh cells
164+
foreach (WTableCell cell in row.Cells)
165+
{
166+
//Restart numbering for child entities in the cell.
167+
RestartNumbering(0, cell.ChildEntities.Count, cell.ChildEntities);
168+
}
169+
}
170+
break;
171+
}
172+
}
173+
}
174+
}
175+
}
Lines changed: 27 additions & 0 deletions
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>Replace_text_with_HTML_list</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\sample.html">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Data\Template.docx">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
<None Update="Output\.gitkeep">
23+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
24+
</None>
25+
</ItemGroup>
26+
27+
</Project>

0 commit comments

Comments
 (0)