Skip to content

Commit 7dc95f0

Browse files
Merge pull request #265 from Suriya-Balamurugan/ES-903456-Replace-text-with-HTML-table
Added sample to replace the text with an HTML table in a Word document
2 parents 3946507 + 72cf0ae commit 7dc95f0

File tree

6 files changed

+109
-0
lines changed

6 files changed

+109
-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.31911.196
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Replace-text-with-HTML-table", "Replace-text-with-HTML-table\Replace-text-with-HTML-table.csproj", "{D3AF529E-DB54-4294-A876-DD42E1E472D0}"
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+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.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 = {58137FF9-5AE1-4514-9929-3A8A7DA1DFEB}
24+
EndGlobalSection
25+
EndGlobal
Binary file not shown.

Find-and-Replace/Replace-text-with-HTML-table/.NET/Replace-text-with-HTML-table/Data/Table.html

Lines changed: 18 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using System.IO;
4+
5+
namespace Replace_text_with_HTML_table
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
using (FileStream inputFileStream = new FileStream(Path.GetFullPath(@"Data/Input.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
12+
{
13+
//Open an existing Word document.
14+
using (WordDocument document = new WordDocument(inputFileStream, FormatType.Docx))
15+
{
16+
using (FileStream htmlFileStream = new FileStream(Path.GetFullPath(@"Data/Table.html"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
17+
{
18+
//Open an HTML document.
19+
using (WordDocument htmlDocument = new WordDocument(htmlFileStream, FormatType.Html))
20+
{
21+
//Get the first table from the HTML document.
22+
WTable table = htmlDocument.Sections[0].Tables[0].Clone() as WTable;
23+
TextBodyPart bodyPart = new TextBodyPart(document);
24+
//Add the table to the body part.
25+
bodyPart.BodyItems.Add(table);
26+
//Replace the placeholder text "<<Product Table>>" with the HTML table in the Word document.
27+
document.Replace("<<Product Table>>", bodyPart, true, true, false);
28+
//Create a file stream to save the modified Word document.
29+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
30+
{
31+
//Save the modified Word document to the file stream.
32+
document.Save(outputFileStream, FormatType.Docx);
33+
}
34+
}
35+
}
36+
}
37+
}
38+
}
39+
}
40+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Replace_text_with_HTML_table</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Update="Data\Input.docx">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
17+
<None Update="Data\Table.html">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</None>
20+
<None Update="Output\.gitkeep">
21+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
22+
</None>
23+
</ItemGroup>
24+
25+
</Project>

0 commit comments

Comments
 (0)