How to save images into a folder and use that path in Word to HTML?
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
- Create a new C# .NET Core console application project.
- Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your .NET Core applications from NuGet.org.
- Include the following namespace in the Program.cs file.
C#
using Syncfusion.DocIO; using Syncfusion.DocIO.DLS;
- 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); } } }
- 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 merge, merge 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