Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 17
VisualStudioVersion = 17.11.35327.3
MinimumVisualStudioVersion = 10.0.40219.1
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {ADDC6FEE-8718-4B00-B091-6C4DC0CBF62A}
EndGlobalSection
EndGlobal
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<p>
<br/>
</p>
<ol level="1">
<li>
<p>
<span>Mountain 200</span>
</p>
</li>
<li>
<p>
<span>
<span>Mountain 300</span>
<br/>
</span>
</p>
</li>
</ol>
<p>
<br/>
</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;

namespace Replace_text_with_HTML_list
{
internal class Program
{
// List to store the names of different list styles used in the document.
static List<string> listStyleNames = new List<string>();
static void Main(string[] args)
{
// Load the input Word document from file stream
using (FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read))
{
// Open the Word document
using (WordDocument document = new WordDocument(docStream, FormatType.Docx))
{
// Load the input Word document from file stream
using (FileStream htmlStream = new FileStream(Path.GetFullPath(@"Data/sample.html"), FileMode.Open, FileAccess.Read))
{
// Open the Word document
using (WordDocument replaceDoc = new WordDocument(htmlStream, FormatType.Html))
{
//Replace the first word with HTML file content
ReplaceText(document, "Tag1", replaceDoc, true);
//Replace the second word with HTML file content
ReplaceText(document, "Tag2", replaceDoc, false);

// Save the modified document to a new file
using (FileStream docStream1 = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.Write))
{
document.Save(docStream1, FormatType.Docx);
}
}
}
}
}
}
private static void ReplaceText(WordDocument document, string findText, WordDocument replaceDoc, bool isFirstReplace)
{
TextSelection selection = document.Find(findText, true, true);
if (selection != null)
{
//Get the textrange
WTextRange textRange = selection.GetAsOneRange();
//Get the owner paragraph
WParagraph ownerPara = textRange.OwnerParagraph;

//For first time replacement alone.
if (isFirstReplace)
{
//Get the index of the textrange
int index = ownerPara.ChildEntities.IndexOf(textRange);
//Add the bookmark start before the textrange
BookmarkStart bookmarkStart = new BookmarkStart(document, "Bkmk");
ownerPara.ChildEntities.Insert(index, bookmarkStart);
//Increment the index
index++;
//Add the bookmark end after the textrange
BookmarkEnd bookmarkEnd = new BookmarkEnd(document, "Bkmk");
ownerPara.ChildEntities.Insert(index + 1, bookmarkEnd);
//Replace the text with HTML content
document.Replace(findText, replaceDoc, true, true);
//Navigate to the bookmark content
BookmarksNavigator navigator = new BookmarksNavigator(document);
navigator.MoveToBookmark("Bkmk");
//Get the bookmark content
TextBodyPart bodyPart = navigator.GetBookmarkContent();
//Get the list of list styles
GetListStyleName(bodyPart.BodyItems);
//Remove the bookmark
Bookmark bookmark = document.Bookmarks.FindByName("Bkmk");
document.Bookmarks.Remove(bookmark);
}
else
{
//Get the next sibiling of the owner paragraph
IEntity nextSibiling = ownerPara.NextSibling;
//Get the owner paragraph index as start index
int startIndex = ownerPara.OwnerTextBody.ChildEntities.IndexOf(ownerPara);
//Replace the text with HTML content
document.Replace(findText, replaceDoc, true, true);
//Get the end index
//If the next sibiling is present then it is the end index, else the child entities count
int endIndex = nextSibiling != null ? ownerPara.OwnerTextBody.ChildEntities.IndexOf(nextSibiling)
: ownerPara.OwnerTextBody.ChildEntities.Count;
//Restart the numbering
RestartNumbering(startIndex, endIndex, document.Sections[0].Body.ChildEntities);
}
}
}
//Get the list style names from the collection
private static void GetListStyleName(EntityCollection collection)
{
//Iterate through the collection
foreach (Entity entity in collection)
{
switch (entity.EntityType)
{
//Entity is paragraph
case EntityType.Paragraph:
WParagraph wParagraph = (WParagraph)entity;
//Check whether the paragrah has list format with numbered type which is not in the collection list.
if (wParagraph.ListFormat.CurrentListLevel != null
&& wParagraph.ListFormat.ListType == ListType.Numbered
&& !listStyleNames.Contains(wParagraph.ListFormat.CurrentListStyle.Name))
//Add the list style name to the collection list
listStyleNames.Add(wParagraph.ListFormat.CurrentListStyle.Name);
break;
//Entity is Table
case EntityType.Table:
WTable table = (WTable)entity;
//Iterate thorugh rows
foreach (WTableRow row in table.Rows)
{
//Iterate through cells
foreach (WTableCell cell in row.Cells)
{
//Get the list style name
GetListStyleName(cell.ChildEntities);
}
}
break;
}
}
}
//Restart the numbering for replaced items
private static void RestartNumbering(int startIndex, int endIndex, EntityCollection collection)
{
//Local value
string listName = string.Empty;
for (int i = startIndex; i < endIndex; i++)
{
Entity entity = collection[i];
switch (entity.EntityType)
{
//Entity is Paragraph
case EntityType.Paragraph:
WParagraph wParagraph = (WParagraph)entity;
//Check whether the paragraph have current list level and the same list name in the collection list.
if (wParagraph.ListFormat.CurrentListLevel != null
&& listStyleNames.Contains(wParagraph.ListFormat.CurrentListStyle.Name))
{
//If the local name is not equal to current list name, then restart the numbering
if (listName != wParagraph.ListFormat.CurrentListStyle.Name)
{
//Set the current list name as local name
listName = wParagraph.ListFormat.CurrentListStyle.Name;
//Enable restart numbering
wParagraph.ListFormat.RestartNumbering = true;
}
//If the local name and current list name are equal then continue list numbering
else
wParagraph.ListFormat.ContinueListNumbering();
}
break;
//Entity is table
case EntityType.Table:
WTable table = (WTable)entity;
//Iterate through rows
foreach (WTableRow row in table.Rows)
{
//Iterate thorugh cells
foreach (WTableCell cell in row.Cells)
{
//Restart numbering for child entities in the cell.
RestartNumbering(0, cell.ChildEntities.Count, cell.ChildEntities);
}
}
break;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Replace_text_with_HTML_list</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="Data\sample.html">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\Template.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Loading