Facing error while saving the document as HTML

In a document, let’s say if we have nodes of type FieldSeparator, FieldEnd or FieldStart, the HTML generation fails with the below message

Image file cannot be written to disk. When saving the document to a stream either ImagesFolder should be specified or custom streams should be provided via ImageSavingCallback. Please see documentation for details.

But that FieldCode only has plain text. It does not have an image. Anyone can help me with this?

Below is the code

 using(MemoryStream stream = new MemoryStream()) {
     try {
         newDocument.Save(stream, SaveFormat.Html);
         stream.Position = 0;
         using(StreamReader reader = new StreamReader(stream)) {
             return reader.ReadToEnd();
         }
     }
     catch(System.Exception ex) {
         var exception = ex;
         return string.Empty;
     }
 }

Library: Aspose.Words, Version: 23.8.0.0

@imsuhas

The error you are encountering while saving the document as HTML is related to the handling of images during the conversion process. The message indicates that when saving to a stream, you need to specify a folder for images or provide a custom image saving callback.

To resolve this issue, you can take one of the following approaches:

  1. Specify an Images Folder: You can set the ImagesFolder property in the HtmlSaveOptions to define a directory where images will be saved. Here’s an example of how to do this:

    using (MemoryStream stream = new MemoryStream())
    {
        try
        {
            HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html)
            {
                ImagesFolder = "C:\\Temp\\Images" // Specify your images folder path here
            };
            newDocument.Save(stream, options);
            stream.Position = 0;
            using (StreamReader reader = new StreamReader(stream))
            {
                return reader.ReadToEnd();
            }
        }
        catch (System.Exception ex)
        {
            var exception = ex;
            return string.Empty;
        }
    }
    
  2. Use an Image Saving Callback: If you want to handle image saving in a custom way, you can implement the IImageSavingCallback interface. This allows you to control how images are processed during the save operation. Here’s a basic example:

    public class CustomImageSavingCallback : IImageSavingCallback
    {
        public void ImageSaving(ImageSavingArgs args)
        {
            // Save the image to a stream or handle it as needed
            args.SetImageStream(new MemoryStream()); // Example: save to a memory stream
            args.KeepImageStreamOpen = false; // Close the stream after saving
        }
    }
    
    using (MemoryStream stream = new MemoryStream())
    {
        try
        {
            HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html)
            {
                ImageSavingCallback = new CustomImageSavingCallback()
            };
            newDocument.Save(stream, options);
            stream.Position = 0;
            using (StreamReader reader = new StreamReader(stream))
            {
                return reader.ReadToEnd();
            }
        }
        catch (System.Exception ex)
        {
            var exception = ex;
            return string.Empty;
        }
    }
    
  3. Export Images as Base64: If you prefer to embed images directly into the HTML as Base64 strings, you can set the ExportImagesAsBase64 property to true:

    HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html)
    {
        ExportImagesAsBase64 = true
    };
    

By implementing one of these solutions, you should be able to resolve the error and successfully save your document as HTML. If you continue to experience issues, please ensure that the document does not contain any unsupported elements that could be causing the conversion to fail.

Feel free to reach out if you have any further questions or need additional assistance!

But that FieldCode does not have an image. Why should I do this? It’s just a plain text

@imsuhas The problem is not caused by the field. Most likely your document contains content that is saved as an image to HTML and Aspose.Words needs a place to save this image. You can try saving the document to a file and see what image Aspose.Words will create next to the output HTML file.