Skip to content

Commit

Permalink
ES-875349-How-to-convert-Webpage-to-Word-document
Browse files Browse the repository at this point in the history
  • Loading branch information
DharanyaSakthivel-SF4210 committed Jan 13, 2025
1 parent 1f7b661 commit d834e13
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
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
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>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

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;
}
}

0 comments on commit d834e13

Please sign in to comment.