InsertHtml with images in it

Hi,

I’m using InsertHtml with the DocumentBuilder object. The inserted HTML some times contains images. There are some images which the user uploads which is there in the HTML. The problem is that the image path in the HTML is something like this

<img src="http://123.123.123.123/images/abc.gif" />

the 123.123.123.123 is the external IP of the machine which has the images but when Aspose accesses the image, I want it to use internal IP of the machine as the external IP is not accessible. So basically when an image is located on 123.123.123.123 I want to change the path to 192.168.0.1. The code which is using Aspose already has a lot of regular expressions applied and out of 100 HTML inserted only 1 or 2 would contain images so I don’t want to apply a regular expression on all the 100 HTMLs that I’m inserting. Is there a way to intercept the image requests made by Aspose?

Thanks in advance…

Hi
Thanks for your request. Unfortunately, there is no other way than parsing your HTML to achieve what you need. You can use the same approach as suggested here:
https://forum.aspose.com/t/58948
Best regards,

Hi Alexey,

Thanks for you reply. I’m afraid I cannot use another regular expression in my code as I’m already using too many of them for footnotes, paragraphs and left indentation

Can I iterate over all the images after they are inserted and check if they belong to 123.123.123.123 ip address and if they do, I can replace them with an image from the internal ip address (or something like that)?

Hi there,

Thanks for your inquiry.

I’m afraid that idea isn’t possible the as the Shape object does not store the address where it is inserted from.

However you maybe able to achieve this by using the IResourceLoadingCallback. This interface is exactly what you need as it’s called when an image is found during HTML import and allows you to get and set the URI to download the image from or even skip loading the image at all.

However there is one snag, I’m afraid at the moment this interface is only called for images found during document load and not when inserting images through InsertHtml.

In the mean time we can use some work around code. Please see the code below, hopefully this achieves what you are looking for. You should replace your call to DocumentBuilder.InsertHtml for any instances where you want to monitor the address of any images that are inserted.

public static void InsertHtmlWithImageHandler(DocumentBuilder builder, string html)
{
    // Set the handler which is called each time an image is encountered in the HTML.
    LoadOptions options = new LoadOptions();
    options.LoadFormat = LoadFormat.Html;
    options.ResourceLoadingCallback = new ImageLoadingHandler();
    // Currently we have to load the HTML into a new document in order for this handler to be called.
    Document tempDoc = new Document(new MemoryStream(Encoding.UTF8.GetBytes(html)), options);
    // Import all the content from the specified HTML into the document at the current position of the builder.
    NodeImporter importer = new NodeImporter(tempDoc, builder.Document, ImportFormatMode.UseDestinationStyles);
    Node currentNode = builder.CurrentParagraph;
    foreach(Node node in tempDoc.FirstSection.Body.ChildNodes)
    {
        Node importNode = importer.ImportNode(node, true);
        currentNode.ParentNode.InsertAfter(importNode, currentNode);
        currentNode = importNode;
    }
}
public class ImageLoadingHandler: IResourceLoadingCallback
    {
        public ResourceLoadingAction ResourceLoading(ResourceLoadingArgs args)
        {
            if (args.ResourceType == ResourceType.Image)
            {
                Uri uri = new Uri(args.Uri);
                // If the URI to the image is an external address then change it to an internal address.
                if (uri.Host == "123.123.123.123")
                {
                    UriBuilder builder = new UriBuilder(uri);
                    builder.Host = "192.168.0.1";
                    args.Uri = builder.Uri.AbsoluteUri;
                }
                return ResourceLoadingAction.Default;
            }

        }

We will consider adding this type of callback for other image operations such as InsertHtml or InsertImage in the future. I have linked to your request to the appropriate issue, you will be notified of any developments.

Thanks,

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

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