Help with INCLUDEPICTURE not working on certain urls

I’m pulling my hair out trying to figure out why certain url’s, like this following, do not work:

builder.InsertField("INCLUDEPICTURE \"https://upload.wikimedia.org/wikipedia/commons/7/70/Example.png\" \\x \\y  \\* MERGEFORMAT");

If you copy & paste that url into your browser, it’s just a small sample png file from wikipedia.

However, when attempting to use builder.InsertField(“INCLUDEPICTURE…”), the merged file displays “Error! Filename not specified.”

Can someone explain why the image is not inserted into the merged document?

Hi @dhartley,

Please try to use inserImage like in this example. Another option is Field.Hiperlink

I have to use INCLUDEPICTURE for a number of reasons.

The thing that’s driving me nuts is that I’ve had this code in place for years and never had an issue. But today I had to make a modification to unrelated code which forced me to rebuild the component, and then suddenly it doesn’t work.

What’s even weirder is that some urls continue to work without issue. I even tested by taking the same image and copying it to separate servers and had success from one server but not the other. So it’s like the url itself or something is causing the problem…only it used to always work. So I’m trying to understand where it’s failing.

@dhartley Could you please attach the output document produced on your side? I have checked the scenario using the following code and the picture is displayed properly in the output documents:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.InsertField("INCLUDEPICTURE \"https://upload.wikimedia.org/wikipedia/commons/7/70/Example.png\" \\x \\y  \\* MERGEFORMAT");

doc.Save(@"C:\Temp\out.docx");
doc.Save(@"C:\Temp\out.pdf");

Here are the output documents: out.docx (9.9 KB) out.pdf (4.6 KB)

I’m not sure what’s going on but after taking a break and coming back and running the same test, now the image is showing!

So I pushed my changes to another environment, tested, and got the same “Error! Filename not specified” error. But after waiting an hour or so, I tested again, and it works there too!

This makes absolutely no sense to me, I didn’t change anything, just waited a bit, and the problem resolved itself??? I’m at a total loss…

I wonder if it has something to do with a network or firewall issue…only…it encountered this error for multiple images using different urls, and also when running from different environments, including my local environment…and some images always worked…

I’m stumped! But also greatly relieved that it works now. Thank you all for taking a look and trying to help. If anyone can shed some light on what’s going on, I’m all ears.

@dhartley It looks like this is causes by network access issues. I tried to run the same code on machine without internet connection and see the same error in the output docment

Error! Filename not specified

But once internet connection is available and I open the document and update fields in the document the image is displayed properly. You can implement IResourceLoadingCallback to control how external resource are loaded. For example the following code prints urls requested by Aspose.Words:

Document doc = new Document();
doc.ResourceLoadingCallback = new ResourceLoadingCallback();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.InsertField("INCLUDEPICTURE \"https://upload.wikimedia.org/wikipedia/commons/7/70/Example.png\" \\x \\y  \\* MERGEFORMAT");

doc.Save(@"C:\Temp\out.docx");
doc.Save(@"C:\Temp\out.pdf");
private class ResourceLoadingCallback : IResourceLoadingCallback
{
    public ResourceLoadingAction ResourceLoading(ResourceLoadingArgs args)
    {
        Console.WriteLine(args.OriginalUri);
        return ResourceLoadingAction.Default;
    }
}