DocumentBaseResourceLoadingCallback Property
Allows to control how external resources are loaded.

Namespace: Aspose.Words
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 19.9
Syntax
public IResourceLoadingCallback ResourceLoadingCallback { get; set; }

Property Value

Type: IResourceLoadingCallback
Examples
Shows how to process inserted resources differently.
[C#]

public void ResourceLoadingCallback()
{
    Document doc = new Document();

    // Images belong to NodeType.Shape
    // There are none in a blank document
    Assert.AreEqual(0, doc.GetChildNodes(NodeType.Shape, true).Count);

    // Enable our custom image loading
    doc.ResourceLoadingCallback = new ImageNameHandler();

    DocumentBuilder builder = new DocumentBuilder(doc);

    // We usually insert images as a uri or byte array, but there are many other possibilities with ResourceLoadingCallback
    // In this case we are referencing images with simple names and keep the image fetching logic somewhere else
    builder.InsertImage("Google Logo");
    builder.InsertImage("Aspose Logo");
    builder.InsertImage("My Watermark");

    Assert.AreEqual(3, doc.GetChildNodes(NodeType.Shape, true).Count);

    doc.Save(ArtifactsDir + "DocumentBase.ResourceLoadingCallback.docx");            
}

private class ImageNameHandler : IResourceLoadingCallback
{
    public ResourceLoadingAction ResourceLoading(ResourceLoadingArgs args)
    {
        if (args.ResourceType == ResourceType.Image)
        {
            // builder.InsertImage expects a uri so inputs like "Google Logo" would normally trigger a FileNotFoundException
            // We can still process those inputs and find an image any way we like, as long as an image byte array is passed to args.SetData()
            if (args.OriginalUri == "Google Logo")
            {
                using (WebClient webClient = new WebClient())
                {
                    byte[] imageBytes =
                        webClient.DownloadData("http://www.google.com/images/logos/ps_logo2.png");
                    args.SetData(imageBytes);
                    // We need this return statement any time a resource is loaded in a custom manner
                    return ResourceLoadingAction.UserProvided;
                }
            }

            if (args.OriginalUri == "Aspose Logo")
            {
                using (WebClient webClient = new WebClient())
                {
                    byte[] imageBytes = webClient.DownloadData(AsposeLogoUrl);
                    args.SetData(imageBytes);
                    return ResourceLoadingAction.UserProvided;
                }
            }

            // We can find and add an image any way we like, as long as args.SetData() is called with some image byte array as a parameter
            if (args.OriginalUri == "My Watermark")
            {
                System.Drawing.Image watermark = System.Drawing.Image.FromFile(MyDir + "Images/Watermark.png");

                System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
                byte[] imageBytes = (byte[]) converter.ConvertTo(watermark, typeof(byte[]));
                args.SetData(imageBytes);

                return ResourceLoadingAction.UserProvided;
            }
        }

        // All other resources such as documents, CSS stylesheets and images passed as uris are handled as they were normally
        return ResourceLoadingAction.Default;
    }
}
See Also