Articles in this section
Category / Section

How to save images into a folder and use that path in Word to HTML?

4 mins read

Syncfusion Essential DocIO is a .NET Core Word library used to create, read, and edit Word documents programmatically without Microsoft Word or Interop dependencies. Using this library, you can save images into a folder and use that path in Word to HTML conversion in C#.

Steps to save images into a folder and use that path in Word to HTML conversion

  1. Create a new C# .NET Core console application project. Create .NET Core console application in Visual Studio in ASP.NET Core Word
  2. Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your .NET Core applications from NuGet.org. Add DocIO.Net.Core NuGet packages of ASP.NET Core Word
  3. Include the following namespace in the Program.cs file.

C#

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
  1. Use the following code example to save images into a folder and use that path in Word to HTML conversion.

C#

//Load an existing Word document. 
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"../../../Input.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
    //Load the file stream into a Word document.
    using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
    {
        //Create a file stream.
        using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../WordToHtml.html"), FileMode.Create, FileAccess.ReadWrite))
        {
            //Hook the event to customize the image. 
            document.SaveOptions.ImageNodeVisited += SaveImage;
            //Save the html file to the file stream.
            document.Save(outputFileStream, FormatType.Html);
        }
    }
}
  1. Use the following helper method to save images into a folder and use that path in Word to HTML conversion by using ImageNodeVisited event.

C#

static int imageCount = 0;
static void SaveImage(object sender, ImageNodeVisitedEventArgs args)
{
    //Customize the image path and save the image in an external folder.
    string imagepath = @"D:\Temp\Image_" + imageCount + ".png";
    //Save the image stream as a file.
    using (FileStream fileStreamOutput = File.Create(imagepath))
    args.ImageStream.CopyTo(fileStreamOutput);
    //Set the image URI to be used in the output HTML.
    args.Uri = imagepath;
    imageCount++;
}

A complete working sample to save images into a folder and use that path in Word to HTML conversion in C# can be downloaded from GitHub.

Take a moment to peruse the documentation where you can find basic Word document processing options along with the features like mail mergemerge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.

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

See Also

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

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied