Problem saving html with linked images

Hi,

We own aspose words and I want to use it to clean up pasted in HTML. So I am loading HTML and saving to HTML. I cannot figure out how to make include the linked images. It keeps wanting to save the images to folders and just want it to reference the images from the public url.

HTML looks like this: (ignore brackets, just needed to get through your encoding)

I am using this code:

Aspose.Words.License license = new License();
license.SetLicense("Zavanta.OutputEngine.Creation.Aspose.Words.lic");

var stream = new MemoryStream(Encoding.UTF8.GetBytes(html));
var doc = new Aspose.Words.Document(stream, new HtmlLoadOptions(LoadFormat.Html, string.Empty, string.Empty));
using (var ms = new MemoryStream())
{
    HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html);
    options.ExportImagesAsBase64 = false;
    options.PrettyFormat = true;
    options.ExportOriginalUrlForLinkedImages = true;
    doc.Save(ms, options);
    return Encoding.UTF8.GetString(ms.GetBuffer());
}

I get this error:
“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.”

What am I doing wrong?

@shmeep,

Thanks for your inquiry. We tested the scenario and have managed to reproduce the same problem on our end. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-16590. We will further look into the details of this problem and will keep you updated on the status of correction. We apologize for your inconvenience.

@shmeep,

Regarding WORDSNET-16590, our product team has completed the work on your issue and has come to a conclusion that this issue is actually not a bug. So, we will close this issue as 'Not a Bug.

HtmlSaveOptions.ExportOriginalUrlForLinkedImages does save image references to HTML without writing image data to disk (or to a user-provided stream). However, this option works only for linked images. The following code shows how to make images linked when loading an HTML document:

public void Test()
{
    const string html = @"
        <html>
            <img src='https://placeimg.com/100/100/any'>
            <img src='https://placeimg.com/200/200/any'>
        </html>";
 
    HtmlLoadOptions loadOptions = new HtmlLoadOptions();
    loadOptions.ResourceLoadingCallback = new NullImageLoadingCallback();
 
    Document doc;
    using (MemoryStream inStream = new MemoryStream(Encoding.UTF8.GetBytes(html)))
    {
        doc = new Document(inStream, loadOptions);
    }
    doc.Save("images.docx");
    
    HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html);
    options.ExportImagesAsBase64 = false;
    options.PrettyFormat = true;
    options.ExportOriginalUrlForLinkedImages = true;
    options.ExportRoundtripInformation = false;
 
    using (MemoryStream outStream = new MemoryStream())
    {
        doc.Save(outStream, options);
        File.WriteAllBytes("images.html", outStream.ToArray());
    }
}
 
private class NullImageLoadingCallback : IResourceLoadingCallback
{
    public ResourceLoadingAction ResourceLoading(ResourceLoadingArgs args)
    {
        return (args.ResourceType == ResourceType.Image)
            ? ResourceLoadingAction.Skip // Skip loading image data. This will make images linked.
            : ResourceLoadingAction.Default;
    }
}

Output files produced by this code are attached in: SampleOutDocs.zip (5.2 KB)