diff --git a/HTML-conversions/Convert-Webpage-to-Word-document/Convert-Webpage-to-Word-document.sln b/HTML-conversions/Convert-Webpage-to-Word-document/Convert-Webpage-to-Word-document.sln new file mode 100644 index 000000000..b022e2ffb --- /dev/null +++ b/HTML-conversions/Convert-Webpage-to-Word-document/Convert-Webpage-to-Word-document.sln @@ -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 diff --git a/HTML-conversions/Convert-Webpage-to-Word-document/Convert-Webpage-to-Word-document/Convert-Webpage-to-Word-document.csproj b/HTML-conversions/Convert-Webpage-to-Word-document/Convert-Webpage-to-Word-document/Convert-Webpage-to-Word-document.csproj new file mode 100644 index 000000000..e6519eb0d --- /dev/null +++ b/HTML-conversions/Convert-Webpage-to-Word-document/Convert-Webpage-to-Word-document/Convert-Webpage-to-Word-document.csproj @@ -0,0 +1,21 @@ + + + + Exe + net8.0 + Convert_Webpage_to_Word_document + enable + enable + + + + + + + + + Always + + + + diff --git a/HTML-conversions/Convert-Webpage-to-Word-document/Convert-Webpage-to-Word-document/Output/.gitkeep b/HTML-conversions/Convert-Webpage-to-Word-document/Convert-Webpage-to-Word-document/Output/.gitkeep new file mode 100644 index 000000000..5f282702b --- /dev/null +++ b/HTML-conversions/Convert-Webpage-to-Word-document/Convert-Webpage-to-Word-document/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/HTML-conversions/Convert-Webpage-to-Word-document/Convert-Webpage-to-Word-document/Program.cs b/HTML-conversions/Convert-Webpage-to-Word-document/Convert-Webpage-to-Word-document/Program.cs new file mode 100644 index 000000000..d3976fdd4 --- /dev/null +++ b/HTML-conversions/Convert-Webpage-to-Word-document/Convert-Webpage-to-Word-document/Program.cs @@ -0,0 +1,60 @@ +using System.Net; +using Syncfusion.DocIO; +using Syncfusion.DocIO.DLS; + +//Register Syncfusion license +Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("Mgo+DSMBMAY9C3t2UlhhQlNHfV5DQmBWfFN0QXNYfVRwdF9GYEwgOX1dQl9nSXZTc0VlWndfcXNSQWc="); + +// 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. + } +} + +/// +/// Fetches the HTML content from a given URL by sending a GET request and reading the server's response stream. +/// +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; + } +} +