Word mail merge - is it possible to Insert Image with url as src

Hi,
I am using mail merge and document builder to insert text and images into a template. When inserting images and saviing to html, I see that aspose saves the image files locally (which makes sense for the most part). But I need to generate a file with images that are urls (i.e https://mydomain/myimgae.jpg) so that the images will appear when the file is transfeered across a network.
I have tried inserting the html img tag manually in the IFieldMergingCallback with the following code:

string imgtag = "<img src='" + imagefile + "' />";
builder.InsertHtml(imgtag);

But when the document is saved to html, the img tags get rewritten with file paths on my local system. So the images are blank when the document gets transferred across the network. (The file of course works fine on my local machine.)
Is there anyway to insert a tag like the one below and have it remain intact when saving to html?

<img src="https://mydomain.com/myimage.jpg" />

Thanks.

Hi
Debra,

Thanks for your inquiry. Please follow up the thread below where you can get help to insert image via url.
https://forum.aspose.com/t/error-on-insertimage/99079

In case of any ambiguity, please let me know.

Thanks for the quick reply but that topic does not help me. That topic refers to a web application. Our product is an intranet application.

I save the docx file as an html file. It must work locally and this file can also be sent across the network. So it can’t refer to file locations on a local system. I simply need the img tag to retain the src=http://mydomain/myinage.jpg, instead of converting it to a local file.

I know I can use HTMLSaveOptions and set the image folder alias to the http path and use ImageSavingCallback to set the filename but the problem with that is that my file may have more than one image to be loaded from a url. But it seems you can set only one image folder alias?

Hi there,

Thanks for your inquiry.

What your looking for is to insert an image as link only. This will allow you to specify the external address that image should be downloaded from. Please see the code below for an example.

DocumentBuilder builder = new DocumentBuilder(doc);
Shape image = new Shape(doc, ShapeType.Image);
image.ImageData.SourceFullName = "http://www.aspose.com/images/aspose-logo.gif";
builder.InsertNode(image);

When you save the document to HTML you will find that the full URL is referenced in the img tag. What’s also better about this technique is that during insertion the image is never downloaded and stored in the document at all, only the link is written to HTML during save.

Thanks,

Thanks for your quick reply. I appreciate your help. That works great in the body of the document but doesn’t seem to work in the header - nothing gets inserted. Is there any way around that?

Hi there,

Thanks for this additional information.

I’m afraid I couldn’t reproduce any issue with the header footer on my side. Could you please attach the document and code you are using here for testing? I will take a look into this for you.

Thanks,

Thanks Adam,

The code used to generate the document is large,cmplex and spread out into many different modules. If you need a code sample I will try to put togeather something simple that mimics the basics of what we are doing. But I have figured out that the issue is that when the document is saved to html, it is using the header on the 2nd page of my document template. This header does not have the image merge field so that is why the image is not being displayed. If I add the image to the 2nd page header, the html saves fine with the img url. But the problem is we don’t want the 2nd page to have the image (we use the same template to save to pdf and html and we don’t want the image on the 2nd page when we save the document to pdf)

II have tried using both ExportHeadersFootersMode.FirstSectionHeaderLastSectionFooter and PerSection but either way the header on the 2nd page is the one that is used when I save to html.

I’ve attached my template. Can I get the document to save in html and use the header on the first page?

Thanks,

I just searched the forums and see that Aspose does not support saving the First Page Header to html? It uses the Primary Header always?

Okay I can work around this when saving to html by replacing the contents of the PrimaryHEader with the contents of the FirstPageHeader. Is there an easy way to do this? I’m thinking I should loop thorugh the HeadersFooters in the doc and if it’s the PrimaryHeader, remove it. Then add a copy of the FirstPageHeader nodes into the HeaderFootersCollection at the index of the PrimaryHeader? Will that work? Is that the best way?

Hi there,

Thanks for this additional information.

Your explanation makes sense so I don’t think you need to create a sample application. You are also correct that only primary header is exported to HTML and not first page header.

The way you suggested copying the nodes from first page header to primary header sounds like the best method to achieve what you are looking for. If you need any help with making this work then please feel free to ask.

Thanks,

Hi,

I am using Aspose Word .NET 13.3 with mail merge.

I want to insert image as link when a export to html and I want to provide my own url.

The image url are contain within a mergefile value.

By using your technique, it still wants me to provide an HtmlSaveOptions.ImagesFolder and it is translating the url to something like Aspose.Words.0d869fcb-b83e-4d2c-999c-bd568b26cc99.002.png

Do you have any idea ?

Thanks a lot.

Audran

Hi Audran,

Thanks for your inquiry. I am working over your query and will get back to you soon.

Best regards,

Hi Audran,

Thank you for being patient. You can still achieve the same using the ShapeBase.HRef property; for example, please try run the following code snippet. This code inserts a click able (Hyperlinked) image in Word document and exports it to HTML format:

DocumentBuilder builder = new DocumentBuilder();
Shape shape = builder.InsertImage(@"C:\Temp\Aspose.Words.png");
shape.HRef = "http://www.aspose.com/images/aspose-logo.gif";
builder.Document.Save(@"C:\Temp\out.html");

I hope, this helps.

Best regards,