Skip to content
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

Add sample for convert web page to word document #271

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,60 @@
using System.Net;
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;

//Register Syncfusion license
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("Mgo+DSMBMAY9C3t2UlhhQlNHfV5DQmBWfFN0QXNYfVRwdF9GYEwgOX1dQl9nSXZTc0VlWndfcXNSQWc=");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove license key in samples


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