Skip to content

Commit c416d1d

Browse files
Merge pull request #169 from AkashArul26/main
Sample for Replace-text-in-Word-with-HTML in .NET
2 parents fdd952b + 97600ba commit c416d1d

File tree

6 files changed

+118
-0
lines changed

6 files changed

+118
-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.8.34330.188
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Replace-text-in-Word-with-HTML", "Replace-text-in-Word-with-HTML\Replace-text-in-Word-with-HTML.csproj", "{8F5D6E09-E381-4D05-8687-13101DE78580}"
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+
{8F5D6E09-E381-4D05-8687-13101DE78580}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{8F5D6E09-E381-4D05-8687-13101DE78580}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{8F5D6E09-E381-4D05-8687-13101DE78580}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{8F5D6E09-E381-4D05-8687-13101DE78580}.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 = {BEB6F4CA-AB89-4F0C-BF6D-6470C5CE3750}
24+
EndGlobalSection
25+
EndGlobal

Find-and-Replace/Replace-text-in-Word-with-HTML/.NET/Replace-text-in-Word-with-HTML/Data/File.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<html> <body> <div style="font-family:calibri;color:#203864;"> <ul> <li>Mountain-200</li> <li>Mountain-300</li> <li>Road-150</li> </ul> </div> </body></html>

Find-and-Replace/Replace-text-in-Word-with-HTML/.NET/Replace-text-in-Word-with-HTML/Data/Keyword.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<html> <body> <div> <p><b><u>Text 1</u></b></p> </div> </body></html>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using System.IO;
4+
using System.Text.RegularExpressions;
5+
6+
namespace Replace_text_in_Word_with_HTML
7+
{
8+
class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"../../../Data/Sample.docx"), FileMode.Open, FileAccess.ReadWrite))
13+
{
14+
//Open an existing Word document.
15+
using (WordDocument document = new WordDocument(fileStream, FormatType.Automatic))
16+
{
17+
//Replace the text by finding the word using ReGex.
18+
ReplaceTextUsingRegex(document);
19+
//Replace the text by finding the word using string.
20+
ReplaceTextUsingString(document);
21+
//Create file stream.
22+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Result.docx"), FileMode.Create, FileAccess.ReadWrite))
23+
{
24+
//Save the Word document to file stream.
25+
document.Save(outputFileStream, FormatType.Docx);
26+
}
27+
}
28+
}
29+
}
30+
#region Helper method
31+
private static void ReplaceTextUsingString(WordDocument document)
32+
{
33+
//Create the temporary word document for HTML.
34+
using (WordDocument replaceDoc = new WordDocument())
35+
{
36+
//Add section for HTML document.
37+
IWSection htmlsection = replaceDoc.AddSection();
38+
//Read HTML string from the file.
39+
string htmlString = File.ReadAllText(Path.GetFullPath(@"../../../Data/Keyword.html"));
40+
//Validate the HTML string.
41+
bool isValidHtml = htmlsection.Body.IsValidXHTML(htmlString, XHTMLValidationType.None);
42+
//When the HTML string passes validation, it is inserted to the document.
43+
if (isValidHtml)
44+
{
45+
//Append HTML string in the temporary word document.
46+
htmlsection.Body.InsertXHTML(htmlString);
47+
}
48+
//Replace the placeholder text with content of HTML document.
49+
document.Replace("<Keyword>", replaceDoc, true, true);
50+
}
51+
}
52+
53+
private static void ReplaceTextUsingRegex(WordDocument document)
54+
{
55+
//Create the temporary word document for HTML.
56+
using (WordDocument replaceDoc = new WordDocument())
57+
{
58+
//Add section for HTML document.
59+
IWSection htmlsection = replaceDoc.AddSection();
60+
//Read HTML string from the file.
61+
string htmlString = File.ReadAllText(Path.GetFullPath(@"../../../Data/File.html"));
62+
//Validate the HTML string.
63+
bool isValidHtml = htmlsection.Body.IsValidXHTML(htmlString, XHTMLValidationType.None);
64+
//When the HTML string passes validation, it is inserted to the document.
65+
if (isValidHtml)
66+
{
67+
//Append HTML string in the temporary word document.
68+
htmlsection.Body.InsertXHTML(htmlString);
69+
}
70+
//Replace the placeholder text with content of HTML document.
71+
document.Replace(new Regex("«([a-zA-Z0-9 ]*:*[a-zA-Z0-9 ]+)»"), replaceDoc, true);
72+
}
73+
}
74+
#endregion
75+
}
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<RootNamespace>Replace_text_in_Word_with_HTML</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+
</Project>

0 commit comments

Comments
 (0)