How to get image from URL while opening HTML in ASP.NET Core?
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; } }
Hook the ImageNodeVisited event before opening the input HTML document and do not dispose the image stream in the event handler. otherwise, image will not be preserved. Internally, DocIO will dispose the image stream.
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.