Question about ImageSavingArgs.setImageFileName in IImageSavingCallback- HtmlSaveOptions.setImagesFolder

Hi Aspose team

We are working on HTML conversion of Word files.

We want to processing the images and replace the images’ address with our server’s URL while saving with IImageSavingCallback

And here are some problem when we are dealing with the images’ address.

For example, we want to make the src attribute like this: http://www.example.com/aaa.jpg

At first we use ImageSavingArgs.setImageFileName in IImageSavingCallback.

However a exception comes out, which said, path can not be included.

So we decide to divide the address into two parts : “http://www.example.com/” for HtmlSaveOptions.setImagesFolder and “aaa.jpg” for ImageSavingArgs.setImageFileName

“aaa.jpg” can be accepted with ImageSavingArgs.setImageFileName.

However with HtmlSaveOptions.setImagesFolder, it will be formatted to "http:\\www.example.com" which is not the right format of an URL

Is there any way to remove input String’s restriction and formatting?

Or any way to achieve this scenario?

Hi Craig,

You can fix this issue after using the following code:

Document doc = new Document(MyDir + @"input.docx");
// iterate through all shape nodes
foreach (Shape s in doc.GetChildNodes(NodeType.Shape, true))
{
    // check if it is an image
    if (s.HasImage)
    {
        // upload the image to server and retrieve new url
        string imgUrl = UploadImageToServer(s);
        string imgName = Path.GetFileName(imgUrl);
        // temporarily store name of the image in a suitable Shape's property
        s.AlternativeText = imgName;
    }
}
HtmlSaveOptions so = new HtmlSaveOptions(SaveFormat.Html);
// set an option to specify server's BaseUri
so.ImagesFolderAlias = "http://www.yourservername.com/images/";
// create and pass the object which implements the image handler methods.
so.ImageSavingCallback = new HandleImageSaving();
doc.Save(MyDir + @"out.html", so);
public class HandleImageSaving : IImageSavingCallback
{
    void IImageSavingCallback.ImageSaving(ImageSavingArgs e)
    {
        if (e.CurrentShape.NodeType == NodeType.Shape && ((Shape)e.CurrentShape).HasImage)
        {
            e.ImageFileName = e.CurrentShape.AlternativeText;
            ((Shape)e.CurrentShape).AlternativeText = string.Empty;
            e.ImageStream = new MemoryStream();
            e.KeepImageStreamOpen = false;
        }
    }
}

Hope, this helps.

Best regards,