Image URL References Removed when using Word2016 Compatibility Mode

Hello,

I am trying to convert some HTML into a Word document and load it into Word Online. I found some code on another thread to skip the download of images:

public class SkipImageDownloadHandler : IResourceLoadingCallback
{
public ResourceLoadingAction ResourceLoading(ResourceLoadingArgs args)
{
String url = args.OriginalUri;
if (args.ResourceType == ResourceType.Image)
{
args.Uri = url;
return ResourceLoadingAction.Skip;
}

    return ResourceLoadingAction.Default;
}

}

Next up I am wanting to convert it into a word document (with compatibility options set to optimise for Word 2016 so it renders in Word Online). See the test code below:

[TestMethod]
public void Word2016_Image_Links_Test()
{
    var html = "<html><head></head><body><img src=\"https://cms.admin.containerize.com/templates/aspose/App_Themes/V3/images/aspose-logo.png\" /></body></html>";

    using (var inputStream = new MemoryStream())
    using (var inputWriter = new StreamWriter(inputStream))
    {
        inputWriter.Write(html);
        inputWriter.Flush();
        inputStream.Position = 0;

        var options = new Aspose.Words.LoadOptions
        {
            LoadFormat = Aspose.Words.LoadFormat.Html,
            ResourceLoadingCallback = new SkipImageDownloadHandler()
        };

        var asposeDoc = new Aspose.Words.Document(inputStream, options);

        // Note: if the line below is commented out then the images display fine
        asposeDoc.CompatibilityOptions.OptimizeFor(Aspose.Words.Settings.MsWordVersion.Word2016);

        asposeDoc.Save("c:\\temp\\converter-output.docx", Aspose.Words.SaveFormat.Docx);
    }
}

Previously when I ran this code I got an exception (which was fixed in WORDSNET-17646). Now when I run the code a document is created but the image does not display. When I run the output document through the Open XML SDK 2.5 Productivity Tool to check the contents, there is no reference to the image URL (the “aspose-logo.png” file mentioned above).

If I comment out the compatibility options line above then the images display fine and when I check them in the Open XML SDK Productivity Tool then the references to the image files are in the document as expected. I am wanting to create a document with 2016 compatibility mode and have it contain images as URLS.

Any advice would be appreciated.

@keithcs

Thanks for your inquiry. Please use the following code to get desired results. Hope,this helps.

var html = "<html><head></head><body><img src=\"https://cms.admin.containerize.com/templates/aspose/App_Themes/V3/images/aspose-logo.png\" /></body></html>";

using(var inputStream = new MemoryStream())
using(var inputWriter = new StreamWriter(inputStream)) {
  inputWriter.Write(html);
  inputWriter.Flush();
  inputStream.Position = 0;

  var options = new Aspose.Words.LoadOptions {
   LoadFormat = Aspose.Words.LoadFormat.Html,
    ResourceLoadingCallback = new SkipImageDownloadHandler()
  };

  var asposeDoc = new Aspose.Words.Document(inputStream, options);

  // Note: if the line below is commented out then the images display fine
  asposeDoc.CompatibilityOptions.OptimizeFor(Aspose.Words.Settings.MsWordVersion.Word2016);
  OoxmlSaveOptions saveOptions = new OoxmlSaveOptions {
   Compliance = OoxmlCompliance.Iso29500_2008_Strict,
    SaveFormat = SaveFormat.Docx
  };
  asposeDoc.Save("c:\\temp\\converter-output_19.1.docx", saveOptions);

public class SkipImageDownloadHandler: IResourceLoadingCallback {
 public ResourceLoadingAction ResourceLoading(ResourceLoadingArgs args) {
  String url = args.OriginalUri;
  if (args.ResourceType == ResourceType.Image) {
   args.Uri = url;
   return ResourceLoadingAction.Skip;
  }

  return ResourceLoadingAction.Default;
 }

}

Please check attached document for your kind reference.converter-output_19.1.zip (5.6 KB)