-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ES-875349-How-to-convert-Webpage-to-Word-document
- Loading branch information
1 parent
1f7b661
commit d834e13
Showing
4 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
HTML-conversions/Convert-Webpage-to-Word-document/Convert-Webpage-to-Word-document.sln
This file contains 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.12.35309.182 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Convert-Webpage-to-Word-document", "Convert-Webpage-to-Word-document\Convert-Webpage-to-Word-document.csproj", "{2C603F46-8AE1-4CB2-A51E-2E7E17F0D6E0}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{2C603F46-8AE1-4CB2-A51E-2E7E17F0D6E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{2C603F46-8AE1-4CB2-A51E-2E7E17F0D6E0}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{2C603F46-8AE1-4CB2-A51E-2E7E17F0D6E0}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{2C603F46-8AE1-4CB2-A51E-2E7E17F0D6E0}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {CC794805-DD23-4D06-8219-488DFE30DAB9} | ||
EndGlobalSection | ||
EndGlobal |
21 changes: 21 additions & 0 deletions
21
...to-Word-document/Convert-Webpage-to-Word-document/Convert-Webpage-to-Word-document.csproj
This file contains 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>Convert_Webpage_to_Word_document</RootNamespace> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="Output\.gitkeep"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
1 change: 1 addition & 0 deletions
1
...ersions/Convert-Webpage-to-Word-document/Convert-Webpage-to-Word-document/Output/.gitkeep
This file contains 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 @@ | ||
|
57 changes: 57 additions & 0 deletions
57
...-conversions/Convert-Webpage-to-Word-document/Convert-Webpage-to-Word-document/Program.cs
This file contains 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,57 @@ | ||
using System.Net; | ||
using Syncfusion.DocIO; | ||
using Syncfusion.DocIO.DLS; | ||
|
||
// Request URLs for header, footer, and main body content. | ||
Console.WriteLine("Please enter the URL for the header content:"); | ||
string headerHtmlUrl = Console.ReadLine(); | ||
Console.WriteLine("Please enter the URL for the footer content:"); | ||
string footerHtmlUrl = Console.ReadLine(); | ||
Console.WriteLine("Please enter the URL for the main body content:"); | ||
string bodyHtmlUrl = Console.ReadLine(); | ||
// Retrieve HTML content from the specified URLs. | ||
string headerContent = GetHtmlContent(headerHtmlUrl); | ||
string footerContent = GetHtmlContent(footerHtmlUrl); | ||
string mainContent = GetHtmlContent(bodyHtmlUrl); | ||
// Create a new Word document instance. | ||
using (WordDocument document = new WordDocument()) | ||
{ | ||
// Add a new section to the document. | ||
WSection section = document.AddSection() as WSection; | ||
// Append the main content HTML to the paragraph. | ||
WParagraph paragraph = section.AddParagraph() as WParagraph; | ||
paragraph.AppendHTML(mainContent); | ||
// Append the header content HTML to the header paragraph. | ||
paragraph = section.HeadersFooters.OddHeader.AddParagraph() as WParagraph; | ||
paragraph.AppendHTML(headerContent); | ||
// Append the footer content HTML to the footer paragraph. | ||
paragraph = section.HeadersFooters.OddFooter.AddParagraph() as WParagraph; | ||
paragraph.AppendHTML(footerContent); | ||
// Save the modified document. | ||
using (FileStream outputStream = new FileStream("Output/Output.docx", FileMode.Create, FileAccess.Write)) | ||
{ | ||
document.Save(outputStream, FormatType.Docx); // Save the document in DOCX format. | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Fetches the HTML content from a given URL by sending a GET request and reading the server's response stream. | ||
/// </summary> | ||
string GetHtmlContent(string url) | ||
{ | ||
// Create a web request to the specified URL. | ||
WebRequest myRequest = WebRequest.Create(url); | ||
// Set the request method to GET to fetch data from the URL. | ||
myRequest.Method = "GET"; | ||
// Get the response from the web server. | ||
WebResponse myResponse = myRequest.GetResponse(); | ||
// Read the response stream and return the HTML content as a string. | ||
using (StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8)) | ||
{ | ||
// Read all content from the response stream. | ||
string result = sr.ReadToEnd(); | ||
// Return the HTML content as a string. | ||
return result; | ||
} | ||
} | ||
|