Skip to content

Commit b8f1898

Browse files
Update Program.cs
1 parent 590cf96 commit b8f1898

File tree

1 file changed

+141
-15
lines changed
  • Find-and-Replace/Replace-List-Restart-Numbering-HTML-with-text/.NET/Replace-list-restart-numbering-HTML-with-text

1 file changed

+141
-15
lines changed
Lines changed: 141 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
using Syncfusion.DocIO;
1+
using Syncfusion.DocIO;
22
using Syncfusion.DocIO.DLS;
33

44
namespace Replace_list_restart_numbering_HTML_with_text
55
{
66
internal class Program
77
{
8+
// List to store the names of different list styles used in the document.
9+
static List<string> listStyleNames = new List<string>();
810
static void Main(string[] args)
911
{
1012
// Load the input Word document from file stream
@@ -20,20 +22,9 @@ static void Main(string[] args)
2022
using (WordDocument replaceDoc = new WordDocument(htmlStream, FormatType.Html))
2123
{
2224
//Replace the first word with HTML file content
23-
document.Replace("Tag1", replaceDoc, true, true);
24-
//Get the paragraph count
25-
int oldcount = document.LastSection.Paragraphs.Count;
26-
//Replace the second word with the HTML file content
27-
document.Replace("Tag2", replaceDoc, true, true);
28-
//Enable restart numbering for the HTML file first paragraph
29-
document.LastSection.Paragraphs[oldcount].ListFormat.RestartNumbering = true;
30-
//Iterate through the remaining paragraphs in the document
31-
for (int i = oldcount + 1; i < document.LastSection.Paragraphs.Count; i++)
32-
{
33-
// If existing list style presents, then continue list numbering
34-
if (document.LastSection.Paragraphs[i].ListFormat.CurrentListStyle != null)
35-
document.LastSection.Paragraphs[i].ListFormat.ContinueListNumbering();
36-
}
25+
ReplaceText(document, "Tag1", replaceDoc, true);
26+
//Replace the second word with HTML file content
27+
ReplaceText(document, "Tag2", replaceDoc, false);
3728

3829
// Save the modified document to a new file
3930
using (FileStream docStream1 = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.Write))
@@ -45,5 +36,140 @@ static void Main(string[] args)
4536
}
4637
}
4738
}
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+
}
48174
}
49175
}

0 commit comments

Comments
 (0)