MailMerge docx to HTML with absolute path img src

I am working on an application which has similar requirements as posted on
In my Word template, I have a few bookmarks. For each bookmark, I will need to insert an image at runtime based on my business logic.
I am using InsertHtml method to add the image tag at the bookmarks. In my image tag, I set the atl=“imgId”. However, the alt is set to empty in the html output.

DocumentBuilder docBuilder = new DocumentBuilder(AsposeDocument);
docBuilder.MoveToBookmark("HeaderImg"); 
//docBuilder.InsertImage(imgUrl);
docBuilder.InsertHtml(string.Format("<img id=\"headerImg\" src='{0}' alt='ImgId'/>", imgUrl));

Aspose Word generated the image tag as a <img src="http://company/Library/header.jpg" width="650" height="250" alt="" />
I can use ImageSavingCallback as suggested. However, how do I set absolute path img src for multiple images in the same document? The ImageSavingArgs does not have any property which can be used to identify the image ids. In my other test, I added a static image in the Word template and set the AltText. The CurrentShape.AlternativeText is still empty.
I am using the Aspose Word 13.3, evaluate version.
Thanks for your help!

Hi Tiffany,

Thanks for your inquiry. You can achieve you requirements by using https://reference.aspose.com/words/net/aspose.words.saving/htmlsaveoptions/imagesfolderalias/ property. The property specifies the name of the folder used to construct image URIs written into an HTML document. Default is an empty string. Please read the following detail of this property for your kind reference.

When you save a Document in HTML format, Aspose.Words needs to save all images embedded in the document as standalone files. ImagesFolder allows you to specify where the images will be saved and ImagesFolderAlias allows to specify how the image URIs will be constructed.

If ImagesFolderAlias is not an empty string, then the image URI written to HTML will be ImagesFolderAlias + <image file name>.

If ImagesFolderAlias is an empty string, then the image URI written to HTML will be ImagesFolder + <image file name>.

If ImagesFolderAlias is set to ‘.’ (dot), then the image file name will be written to HTML without path regardless of other options.

I would suggest you please upgrade to the latest version (v13.4.0) from here:
https://downloads.aspose.com/words/net

If the problem still remains, please attach your input Word document here for testing. I will investigate the issue on my side and provide you more information.

Tahir,
Thanks for your quick response. I need to be able to create a html document from a Word template and then email out the html document. All the images in the html document need to have the URI set the company image folder on the web server. I am able to set ImagesFolderAlias to the image folder. However, Aspose renamed the image to a Aspose.WordsXXXXXXX.jpeg which is different then the orignal file name.

HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html);
options.ImagesFolderAlias = "http://company/images";

Aspose generated the img tag as

<a name="HeaderImg"><img src="http://company/images/Aspose.Words.af87de60-4185-4c8a-9ad3-2a1df0cff259.001.jpeg" width="650" height="250" alt="" />

and the correct src of the image should be

<a name="HeaderImg"><img src="http://company/images/originalImageFileName.jpeg" width="650" height="250" alt="" />

The html file which I need to email out has multiple images as shown below.

<a name="HeaderImg"><img src="http://company/images/img1.jpeg" width="650" height="250" alt="" />
<a name="HeaderImg"><img src="http://company/images/img2.jpeg" width="650" height="250" alt="" />
<a name="HeaderImg"><img src="http://company/images/img3.jpeg" width="650" height="250" alt="" />

I could use ImageSavingCallback to rename the image file as shown below.

options.ImageSavingCallback = new HtmlImageExportCallback("originalImageFileName.jpg");

However, renaming the image in the ImageSavingCallback event does not work if the document has mutiple images which have different URIs. How do I set the URIs of multiple images in a document? Why does Aspose rename the orginal image file name? How can I differentiate the images from the ImageSavingArgs?
Thanks!

Hi Tiffany,
Thanks for your inquiry. The IImageSavingCallback can be fired while saving either a shape or a group shape. That’s why the property has ShapeBase type. You can check whether it’s a group shape comparing ShapeType with Group or by casting it to one of derived classes: Shape or GroupShape.
Aspose.Words uses the document file name and a unique number to generate unique file name for each image found in the document. You can use the CurrentShape property to generate a “better” file name by examining shape properties such as Title (Shape only), SourceFullName (Shape only) and Name. Of course you can build file names using any other properties or criteria but note that subsidiary file names must be unique within the export operation.
Some images in the document can be unavailable. To check image availability use the IsImageAvailable property.
Regarding Shape.AlternativeText, I have managed to reproduce the same issue at my side. I have logged this issue as WORDSNET-8258 in our issue tracking system. I have linked this forum thread to the same issue and you will be notified via this forum thread once this issue is resolved. We apologize for your inconvenience.
Please use the following code snippet to set the Shape.AlternativeText property as a workaround. Hope this helps you.

Document doc = new Document(MyDir + "ImageSavingCallback.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertHtml(string.Format("<p><img id=\"headerImg\"src='{0}' alt='ImgId' title='aspose-logo.jpg'/>", "http://www.aspose.com/Images/aspose-logo.jpg"));
Node currentNode = builder.CurrentNode;
while (currentNode.NodeType != NodeType.Shape)
{
    currentNode = currentNode.PreviousPreOrder(doc);
}

((Shape)currentNode).AlternativeText = "aspose-logo.jpg";

HtmlSaveOptions opt = new HtmlSaveOptions(SaveFormat.Html);
opt.ImagesFolderAlias = "http://www.aspose.com/";
opt.ImageSavingCallback = new
ImageSavingCallback();
doc.Save(MyDir + "out.html", opt);
public class ImageSavingCallback : IImageSavingCallback
{
    public void ImageSaving(ImageSavingArgs args)
    {
        if(args.CurrentShape.Name != "")
            args.ImageFileName = args.CurrentShape.Name;
        else
            if (args.CurrentShape.IsImage)
            {
                Shape shape = (Shape)args.CurrentShape;
                args.ImageFileName = shape.AlternativeText;
            }
    }
}

The code snippet works great! Thanks for your help!

Tiffany

Hi Tiffany,


Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

The issues you have found earlier (filed as WORDSNET-8258) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.