Skip to content

Commit c77fd56

Browse files
Merge pull request #310 from SyncfusionExamples/ES-876618-Replace-text-in-list-paragraph
Replace-text-in-list-paragraph
2 parents 54f6996 + 2563286 commit c77fd56

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-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 16
4+
VisualStudioVersion = 16.0.30804.86
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Replace-text-in-list-paragraph-with-HTML", "Replace-text-in-list-paragraph-with-HTML/Replace-text-in-list-paragraph-with-HTML.csproj", "{DDC158F3-4052-48B7-AF5F-4AB3D6CD4B22}"
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+
{DDC158F3-4052-48B7-AF5F-4AB3D6CD4B22}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{DDC158F3-4052-48B7-AF5F-4AB3D6CD4B22}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{DDC158F3-4052-48B7-AF5F-4AB3D6CD4B22}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{DDC158F3-4052-48B7-AF5F-4AB3D6CD4B22}.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 = {2F907F5C-D20B-4C16-8863-3281F8B7A58B}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html>
2+
<body>
3+
<div style="font-family:calibri;color:#3C4D6F;">
4+
<p>Cycling Helmet</p>
5+
<p>Enhance durability and performance in rough terrain</p>
6+
<p>Beefier look with thicker tires</p>
7+
</div>
8+
</body>
9+
</html>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using System.IO;
4+
5+
namespace Replace_text_in_list_paragraph_with_HTML
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
using (FileStream inputFileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite))
12+
{
13+
//Open the template Word document.
14+
using (WordDocument document = new WordDocument(inputFileStream, FormatType.Automatic))
15+
{
16+
string htmlFilePath = @"Data/File.html";
17+
//Check if the HTML content is valid.
18+
if (document.LastSection.Body.IsValidXHTML(htmlFilePath, XHTMLValidationType.None))
19+
{
20+
//Define the variable containing the text to search within the paragraph.
21+
string variable = "Youth mountain bike";
22+
//Find the first occurrence of a particular text in the document
23+
TextSelection textSelection = document.Find(variable, true, true);
24+
//Get the found text as single text range
25+
WTextRange textRange = textSelection.GetAsOneRange();
26+
// Get the paragraph containing the found text range
27+
WParagraph paragraph = textRange.OwnerParagraph;
28+
//Get the next sibling element of the current paragraph.
29+
TextBodyItem nextSibling = paragraph.NextSibling as TextBodyItem;
30+
//Get the index of the current paragraph within its parent text body.
31+
int sourceIndex = paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph);
32+
//Clear all child entities within the paragraph.
33+
paragraph.ChildEntities.Clear();
34+
//Get the list style name from the paragraph.
35+
string listStyleName = paragraph.ListFormat.CurrentListStyle.Name;
36+
//Get the current list level number.
37+
int listLevelNum = paragraph.ListFormat.ListLevelNumber;
38+
//Append HTML content from the specified file to the paragraph.
39+
paragraph.AppendHTML(File.ReadAllText(Path.GetFullPath(htmlFilePath)));
40+
//Determine the index of the next sibling if it exists.
41+
int nextSiblingIndex = nextSibling != null ? nextSibling.OwnerTextBody.ChildEntities.IndexOf(nextSibling) : -1;
42+
//Apply the same list style to newly added paragraphs from the HTML content.
43+
for (int k = sourceIndex; k < paragraph.OwnerTextBody.Count; k++)
44+
{
45+
//Stop applying the style if the next sibling is reached.
46+
if (nextSiblingIndex != -1 && k == nextSiblingIndex)
47+
{
48+
break;
49+
}
50+
Entity entity = paragraph.OwnerTextBody.ChildEntities[k];
51+
//Apply the list style only if the entity is a paragraph.
52+
if (entity is WParagraph)
53+
{
54+
(entity as WParagraph).ListFormat.ApplyStyle(listStyleName);
55+
(entity as WParagraph).ListFormat.ListLevelNumber = listLevelNum;
56+
}
57+
else
58+
{
59+
break;
60+
}
61+
}
62+
}
63+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Result.docx"), FileMode.Create, FileAccess.ReadWrite))
64+
{
65+
//Save the modified Word document to the output file stream.
66+
document.Save(outputFileStream, FormatType.Docx);
67+
}
68+
}
69+
}
70+
}
71+
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Replace_text_in_list_paragraph_with_HTML</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Update="Data\File.html">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
17+
<None Update="Data\Template.docx">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</None>
20+
</ItemGroup>
21+
22+
</Project>

0 commit comments

Comments
 (0)