Skip to content

Commit c513437

Browse files
Merge pull request #545 from SyncfusionExamples/263523-HTML
263523-How to save Header, Body and Footer as separate HTML from Word
2 parents 11e64fe + c6e55d8 commit c513437

7 files changed

Lines changed: 182 additions & 0 deletions

File tree

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.14.37111.16 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Save-HTML-From-Word-Document", "Save-HTML-From-Word-Document\Save-HTML-From-Word-Document.csproj", "{BFD82BD1-243F-86F6-FB0C-E6FB6CF11614}"
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+
{BFD82BD1-243F-86F6-FB0C-E6FB6CF11614}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{BFD82BD1-243F-86F6-FB0C-E6FB6CF11614}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{BFD82BD1-243F-86F6-FB0C-E6FB6CF11614}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{BFD82BD1-243F-86F6-FB0C-E6FB6CF11614}.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 = {6E0ACDC8-D80C-4DBF-BD66-0BB0FDE4BF92}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+


HTML-conversions/Save-HTML-From-Word-Document/.NET/Save-HTML-From-Word-Document/Save-HTML-From-Word-Document/Output/OddHeader_0.html

Lines changed: 7 additions & 0 deletions
Large diffs are not rendered by default.

HTML-conversions/Save-HTML-From-Word-Document/.NET/Save-HTML-From-Word-Document/Save-HTML-From-Word-Document/Output/TextBody.html

Lines changed: 48 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
namespace Save_HTML_From_Word_Document
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
//Open the input Word document as a file stream
11+
using (FileStream inputStream = new FileStream(Path.GetFullPath(@"../../../Data/Template.docx"), FileMode.Open, FileAccess.Read))
12+
{
13+
// Load the Word document
14+
using (WordDocument document = new WordDocument(inputStream, FormatType.Docx))
15+
{
16+
int i = 0;
17+
// Iterate through each section in the document
18+
foreach (WSection section in document.Sections)
19+
{
20+
// Handle first page header / footer if enabled
21+
if (section.PageSetup.DifferentFirstPage)
22+
{
23+
GenerateHTML(section.HeadersFooters.FirstPageHeader, "FirstPageHeader_" + i + ".html");
24+
GenerateHTML(section.HeadersFooters.FirstPageFooter, "FirstPageFooter_" + i + ".html");
25+
}
26+
// Handle even page header / footer if enabled
27+
else if (section.PageSetup.DifferentOddAndEvenPages)
28+
{
29+
GenerateHTML(section.HeadersFooters.EvenHeader, "EvenHeader_" + i + ".html");
30+
GenerateHTML(section.HeadersFooters.EvenFooter, "EvenFooter_" + i + ".html");
31+
32+
}
33+
//This is the default header and footer
34+
GenerateHTML(section.HeadersFooters.OddHeader, "OddHeader_" + i + ".html");
35+
GenerateHTML(section.HeadersFooters.OddFooter, "OddFooter_" + i + ".html");
36+
37+
//After generating headers and footers, clear it
38+
section.HeadersFooters.FirstPageHeader.ChildEntities.Clear();
39+
section.HeadersFooters.FirstPageFooter.ChildEntities.Clear();
40+
section.HeadersFooters.EvenHeader.ChildEntities.Clear();
41+
section.HeadersFooters.EvenFooter.ChildEntities.Clear();
42+
section.HeadersFooters.OddHeader.ChildEntities.Clear();
43+
section.HeadersFooters.OddFooter.ChildEntities.Clear();
44+
45+
i++;
46+
}
47+
// Save the remaining document body content as HTM
48+
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"../../../Output/TextBody.html"), FileMode.Create))
49+
{
50+
document.Save(outputStream, FormatType.Html);
51+
}
52+
}
53+
}
54+
}
55+
/// </summary>
56+
// Generates an HTML file from the given text body(header or footer).
57+
/// </summary>
58+
/// <param name="textBody">The text body (header/footer) to convert.</param>
59+
/// <param name="outputFile">The output HTML file name.</param>
60+
61+
private static void GenerateHTML(WTextBody textBody, string outputFile)
62+
{
63+
string outputPath = Path.GetFullPath(@"../../../Output/");
64+
// Check if the text body contains any content
65+
if (textBody.ChildEntities.Count > 0)
66+
{
67+
// Create a new Word document to hold extracted content
68+
WordDocument document = new WordDocument();
69+
document.AddSection();
70+
// Clone and add each entity from the source text body
71+
foreach (Entity entity in textBody.ChildEntities)
72+
document.LastSection.Body.ChildEntities.Add(entity.Clone());
73+
74+
//Save the extracted content as an HTML file
75+
document.Save(outputPath + outputFile, FormatType.Html);
76+
}
77+
}
78+
}
79+
}
80+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Save_HTML_From_Word_Document</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
<ItemGroup>
11+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
12+
</ItemGroup>
13+
<ItemGroup>
14+
<None Update="Data\Template.docx">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
17+
<None Update="Output\.gitkeep">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</None>
20+
</ItemGroup>
21+
</Project>

0 commit comments

Comments
 (0)