ResourceLoadingAction Enumeration

Specifies the mode of resource loading.

Namespace:  Aspose.Words.Loading
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.3
Syntax
public enum ResourceLoadingAction
Members
  Member nameValueDescription
Default0 Aspose.Words will load this resource as usual.
Skip1 Aspose.Words will skip loading of this resource. Only link without data will be stored for an image. Css style sheet will be ignored.
UserProvided2 Aspose.Words will use byte array provided by user in SetData(Byte) as resource data.
Examples
Shows how to process inserted resources differently.
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(ImageDir + "Transparent background logo.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