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.14.37111.16 d17.14
MinimumVisualStudioVersion = 10.0.40219.1
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BFD82BD1-243F-86F6-FB0C-E6FB6CF11614}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BFD82BD1-243F-86F6-FB0C-E6FB6CF11614}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BFD82BD1-243F-86F6-FB0C-E6FB6CF11614}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BFD82BD1-243F-86F6-FB0C-E6FB6CF11614}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6E0ACDC8-D80C-4DBF-BD66-0BB0FDE4BF92}
EndGlobalSection
EndGlobal
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@


Large diffs are not rendered by default.

Large diffs are not rendered by default.

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

namespace Save_HTML_From_Word_Document
{
class Program
{
static void Main(string[] args)
{
//Open the input Word document as a file stream
using (FileStream inputStream = new FileStream(Path.GetFullPath(@"../../../Data/Template.docx"), FileMode.Open, FileAccess.Read))
{
// Load the Word document
using (WordDocument document = new WordDocument(inputStream, FormatType.Docx))
{
int i = 0;
// Iterate through each section in the document
foreach (WSection section in document.Sections)
{
// Handle first page header / footer if enabled
if (section.PageSetup.DifferentFirstPage)
{
GenerateHTML(section.HeadersFooters.FirstPageHeader, "FirstPageHeader_" + i + ".html");
GenerateHTML(section.HeadersFooters.FirstPageFooter, "FirstPageFooter_" + i + ".html");
}
// Handle even page header / footer if enabled
else if (section.PageSetup.DifferentOddAndEvenPages)
{
GenerateHTML(section.HeadersFooters.EvenHeader, "EvenHeader_" + i + ".html");
GenerateHTML(section.HeadersFooters.EvenFooter, "EvenFooter_" + i + ".html");

}
//This is the default header and footer
GenerateHTML(section.HeadersFooters.OddHeader, "OddHeader_" + i + ".html");
GenerateHTML(section.HeadersFooters.OddFooter, "OddFooter_" + i + ".html");

//After generating headers and footers, clear it
section.HeadersFooters.FirstPageHeader.ChildEntities.Clear();
section.HeadersFooters.FirstPageFooter.ChildEntities.Clear();
section.HeadersFooters.EvenHeader.ChildEntities.Clear();
section.HeadersFooters.EvenFooter.ChildEntities.Clear();
section.HeadersFooters.OddHeader.ChildEntities.Clear();
section.HeadersFooters.OddFooter.ChildEntities.Clear();

i++;
}
// Save the remaining document body content as HTM
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"../../../Output/TextBody.html"), FileMode.Create))
{
document.Save(outputStream, FormatType.Html);
}
}
}
}
/// </summary>
// Generates an HTML file from the given text body(header or footer).
/// </summary>
/// <param name="textBody">The text body (header/footer) to convert.</param>
/// <param name="outputFile">The output HTML file name.</param>

private static void GenerateHTML(WTextBody textBody, string outputFile)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add xml comments

{
string outputPath = Path.GetFullPath(@"../../../Output/");
// Check if the text body contains any content
if (textBody.ChildEntities.Count > 0)
{
// Create a new Word document to hold extracted content
WordDocument document = new WordDocument();
document.AddSection();
// Clone and add each entity from the source text body
foreach (Entity entity in textBody.ChildEntities)
document.LastSection.Body.ChildEntities.Add(entity.Clone());

//Save the extracted content as an HTML file
document.Save(outputPath + outputFile, FormatType.Html);
}
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Save_HTML_From_Word_Document</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
</ItemGroup>
<ItemGroup>
<None Update="Data\Template.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
Loading