We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to get image from URL while opening HTML in ASP.NET Core

Platform: ASP.NET Core |
Control: DocIO |
Published Date: February 18, 2021 |
Last Revised Date: February 19, 2021

You can convert HTML to Word document and vice versa using Syncfusion .NET Core Word library (Essential DocIO) without Microsoft Word or interop dependencies.

When converting HTML to Word document using .NET Core Word library, the images referred as URL in the input HTML file (“<img src=”https://”>”) are not imported in the Word document. Essential DocIO doesn’t support to download image from Website URL in ASP.NET Core, Xamarin, and Blazor platforms. You can import these images using ImageNodeVisited event in DocIO.

Get the image from URL in the input HTML:

To import the images referred as URL in the input HTML, we suggest you download the image using ImageNodeVisited event in DocIO.

The following code example shows how to hook ImageNodeVisited event while converting HTML to Word document.

C#

//Open the file as Stream
FileStream docStream = new FileStream("Input.html", FileMode.Open, FileAccess.Read);
//Creates a new instance of WordDocument
WordDocument document = new WordDocument();
 
//Hooks the ImageNodeVisited event to download the image from a Website URL
document.HTMLImportSettings.ImageNodeVisited += DownloadImage;
 
//Opens the input HTML document
document.Open(docStream, FormatType.Html);
 
//Unhooks the ImageNodeVisited event after loading HTML
document.HTMLImportSettings.ImageNodeVisited -= DownloadImage;
 
FileStream outputStream = new FileStream("HtmlToWord.docx", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
//Saves the Word document
document.Save(outputStream, FormatType.Docx);
//Closes the Word document
document.Close();
//Disposes the output stream
outputStream.Flush();
outputStream.Dispose();

 

The following code example shows event handler to download the image from website URL.

C#

/// <summary>
/// Event handler to download the image from website.
/// </summary>
private static void DownloadImage(object sender, ImageNodeVisitedEventArgs args)
{
    //Check whether image src is mentioned as website URL.
    if (args.Uri.StartsWith("https://"))
    {
       WebClient client = new WebClient();
       //Download the image as stream.
       byte[] image = client.DownloadData(args.Uri);
       Stream stream = new MemoryStream(image);
       //Set the retrieved image from the input HTML.
       args.ImageStream = stream;
    }
}

 

Note:

Hooks the ImageNodeVisited event before opening the input HTML document.

Take a moment to peruse the documentation, where you can find more information about HTML to Word conversion and vice versa.

Explore more about the rich set of Syncfusion Word Framework features.

2X faster development

The ultimate ASP.NET Core UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile