-
Notifications
You must be signed in to change notification settings - Fork 56
263523-How to save Header, Body and Footer as separate HTML from Word #545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
...TML-From-Word-Document/.NET/Save-HTML-From-Word-Document/Save-HTML-From-Word-Document.sln
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 added
BIN
+156 KB
...ocument/.NET/Save-HTML-From-Word-Document/Save-HTML-From-Word-Document/Data/Template.docx
Binary file not shown.
1 change: 1 addition & 0 deletions
1
...d-Document/.NET/Save-HTML-From-Word-Document/Save-HTML-From-Word-Document/Output/.gitkeep
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| |
7 changes: 7 additions & 0 deletions
7
...nt/.NET/Save-HTML-From-Word-Document/Save-HTML-From-Word-Document/Output/OddHeader_0.html
Large diffs are not rendered by default.
Oops, something went wrong.
48 changes: 48 additions & 0 deletions
48
...ument/.NET/Save-HTML-From-Word-Document/Save-HTML-From-Word-Document/Output/TextBody.html
Large diffs are not rendered by default.
Oops, something went wrong.
80 changes: 80 additions & 0 deletions
80
...m-Word-Document/.NET/Save-HTML-From-Word-Document/Save-HTML-From-Word-Document/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| { | ||
| 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); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
21 changes: 21 additions & 0 deletions
21
...-HTML-From-Word-Document/Save-HTML-From-Word-Document/Save-HTML-From-Word-Document.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add xml comments