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
the 22.214.171.124 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 126.96.36.199 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…
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:
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 188.8.131.52 ip address and if they do, I can replace them with an image from the internal ip address (or something like that)?
10.0pt;font-family:“Courier New”;color:blue;mso-no-proof:yes”>public<span style=“font-size:10.0pt;font-family:“Courier New”;mso-no-proof:yes”> static void
builder, string html)<o:p></o:p>
// 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 = 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 == "184.108.40.206")
UriBuilder builder = new UriBuilder(uri);
builder.Host = "192.168.0.1";
args.Uri = builder.Uri.AbsoluteUri;