Skip to content

Commit 6443fae

Browse files
Merge pull request #321 from SyncfusionExamples/ES-875349-How-to-convert-Webpage-to-Word-document
Add sample for convert web page to word document
2 parents 4f71670 + d834e13 commit 6443fae

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35309.182
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
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}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{2C603F46-8AE1-4CB2-A51E-2E7E17F0D6E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{2C603F46-8AE1-4CB2-A51E-2E7E17F0D6E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{2C603F46-8AE1-4CB2-A51E-2E7E17F0D6E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{2C603F46-8AE1-4CB2-A51E-2E7E17F0D6E0}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {CC794805-DD23-4D06-8219-488DFE30DAB9}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Convert_Webpage_to_Word_document</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Output\.gitkeep">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
</ItemGroup>
20+
21+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System.Net;
2+
using Syncfusion.DocIO;
3+
using Syncfusion.DocIO.DLS;
4+
5+
// Request URLs for header, footer, and main body content.
6+
Console.WriteLine("Please enter the URL for the header content:");
7+
string headerHtmlUrl = Console.ReadLine();
8+
Console.WriteLine("Please enter the URL for the footer content:");
9+
string footerHtmlUrl = Console.ReadLine();
10+
Console.WriteLine("Please enter the URL for the main body content:");
11+
string bodyHtmlUrl = Console.ReadLine();
12+
// Retrieve HTML content from the specified URLs.
13+
string headerContent = GetHtmlContent(headerHtmlUrl);
14+
string footerContent = GetHtmlContent(footerHtmlUrl);
15+
string mainContent = GetHtmlContent(bodyHtmlUrl);
16+
// Create a new Word document instance.
17+
using (WordDocument document = new WordDocument())
18+
{
19+
// Add a new section to the document.
20+
WSection section = document.AddSection() as WSection;
21+
// Append the main content HTML to the paragraph.
22+
WParagraph paragraph = section.AddParagraph() as WParagraph;
23+
paragraph.AppendHTML(mainContent);
24+
// Append the header content HTML to the header paragraph.
25+
paragraph = section.HeadersFooters.OddHeader.AddParagraph() as WParagraph;
26+
paragraph.AppendHTML(headerContent);
27+
// Append the footer content HTML to the footer paragraph.
28+
paragraph = section.HeadersFooters.OddFooter.AddParagraph() as WParagraph;
29+
paragraph.AppendHTML(footerContent);
30+
// Save the modified document.
31+
using (FileStream outputStream = new FileStream("Output/Output.docx", FileMode.Create, FileAccess.Write))
32+
{
33+
document.Save(outputStream, FormatType.Docx); // Save the document in DOCX format.
34+
}
35+
}
36+
37+
/// <summary>
38+
/// Fetches the HTML content from a given URL by sending a GET request and reading the server's response stream.
39+
/// </summary>
40+
string GetHtmlContent(string url)
41+
{
42+
// Create a web request to the specified URL.
43+
WebRequest myRequest = WebRequest.Create(url);
44+
// Set the request method to GET to fetch data from the URL.
45+
myRequest.Method = "GET";
46+
// Get the response from the web server.
47+
WebResponse myResponse = myRequest.GetResponse();
48+
// Read the response stream and return the HTML content as a string.
49+
using (StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8))
50+
{
51+
// Read all content from the response stream.
52+
string result = sr.ReadToEnd();
53+
// Return the HTML content as a string.
54+
return result;
55+
}
56+
}
57+

0 commit comments

Comments
 (0)