Class ResourceLoadingArgs

Class ResourceLoadingArgs

Namespace: Aspose.Words.Loading
Assembly: Aspose.Words.dll (25.12.0)

Provides data for the Aspose.Words.Loading.IResourceLoadingCallback.ResourceLoading(Aspose.Words.Loading.ResourceLoadingArgs) method.

public class ResourceLoadingArgs

Inheritance

object ResourceLoadingArgs

Inherited Members

object.GetType() , object.MemberwiseClone() , object.ToString() , object.Equals(object?) , object.Equals(object?, object?) , object.ReferenceEquals(object?, object?) , object.GetHashCode()

Examples

Shows how to customize the process of loading external resources into a document.

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

                                                                                                DocumentBuilder builder = new DocumentBuilder(doc);

                                                                                                // Images usually are inserted using a URI, or a byte array.
                                                                                                // Every instance of a resource load will call our callback's ResourceLoading method.
                                                                                                builder.InsertImage("Google logo");
                                                                                                builder.InsertImage("Aspose logo");
                                                                                                builder.InsertImage("Watermark");

                                                                                                Assert.That(doc.GetChildNodes(NodeType.Shape, true).Count, Is.EqualTo(3));

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

                                                                                            /// <summary>
                                                                                            /// Allows us to load images into a document using predefined shorthands, as opposed to URIs.
                                                                                            /// This will separate image loading logic from the rest of the document construction.
                                                                                            /// </summary>
                                                                                            private class ImageNameHandler : IResourceLoadingCallback
                                                                                            {
                                                                                                public ResourceLoadingAction ResourceLoading(ResourceLoadingArgs args)
                                                                                                {
                                                                                                    // If this callback encounters one of the image shorthands while loading an image,
                                                                                                    // it will apply unique logic for each defined shorthand instead of treating it as a URI.
                                                                                                    if (args.ResourceType == ResourceType.Image)
                                                                                                        switch (args.OriginalUri)
                                                                                                        {
                                                                                                            case "Google logo":
                                                                                                                using (HttpClient client = new HttpClient())
                                                                                                                {
                                                                                                                    byte[] imageData = client.GetByteArrayAsync("http://www.google.com/images/logos/ps_logo2.png").GetAwaiter().GetResult();
                                                                                                                    args.SetData(imageData);
                                                                                                                }

                                                                                                                return ResourceLoadingAction.UserProvided;

                                                                                                            case "Aspose logo":
                                                                                                                args.SetData(File.ReadAllBytes(ImageDir + "Logo.jpg"));

                                                                                                                return ResourceLoadingAction.UserProvided;

                                                                                                            case "Watermark":
                                                                                                                args.SetData(File.ReadAllBytes(ImageDir + "Transparent background logo.png"));

                                                                                                                return ResourceLoadingAction.UserProvided;
                                                                                                        }

                                                                                                    return ResourceLoadingAction.Default;
                                                                                                }
                                                                                            }

Properties

OriginalUri

Original URI of the resource as specified in imported document.

public string OriginalUri { get; }

Property Value

string

Examples

Shows how to customize the process of loading external resources into a document.

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

                                                                                                DocumentBuilder builder = new DocumentBuilder(doc);

                                                                                                // Images usually are inserted using a URI, or a byte array.
                                                                                                // Every instance of a resource load will call our callback's ResourceLoading method.
                                                                                                builder.InsertImage("Google logo");
                                                                                                builder.InsertImage("Aspose logo");
                                                                                                builder.InsertImage("Watermark");

                                                                                                Assert.That(doc.GetChildNodes(NodeType.Shape, true).Count, Is.EqualTo(3));

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

                                                                                            /// <summary>
                                                                                            /// Allows us to load images into a document using predefined shorthands, as opposed to URIs.
                                                                                            /// This will separate image loading logic from the rest of the document construction.
                                                                                            /// </summary>
                                                                                            private class ImageNameHandler : IResourceLoadingCallback
                                                                                            {
                                                                                                public ResourceLoadingAction ResourceLoading(ResourceLoadingArgs args)
                                                                                                {
                                                                                                    // If this callback encounters one of the image shorthands while loading an image,
                                                                                                    // it will apply unique logic for each defined shorthand instead of treating it as a URI.
                                                                                                    if (args.ResourceType == ResourceType.Image)
                                                                                                        switch (args.OriginalUri)
                                                                                                        {
                                                                                                            case "Google logo":
                                                                                                                using (HttpClient client = new HttpClient())
                                                                                                                {
                                                                                                                    byte[] imageData = client.GetByteArrayAsync("http://www.google.com/images/logos/ps_logo2.png").GetAwaiter().GetResult();
                                                                                                                    args.SetData(imageData);
                                                                                                                }

                                                                                                                return ResourceLoadingAction.UserProvided;

                                                                                                            case "Aspose logo":
                                                                                                                args.SetData(File.ReadAllBytes(ImageDir + "Logo.jpg"));

                                                                                                                return ResourceLoadingAction.UserProvided;

                                                                                                            case "Watermark":
                                                                                                                args.SetData(File.ReadAllBytes(ImageDir + "Transparent background logo.png"));

                                                                                                                return ResourceLoadingAction.UserProvided;
                                                                                                        }

                                                                                                    return ResourceLoadingAction.Default;
                                                                                                }
                                                                                            }

ResourceType

Type of resource.

public ResourceType ResourceType { get; }

Property Value

ResourceType

Examples

Shows how to customize the process of loading external resources into a document.

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

                                                                                                DocumentBuilder builder = new DocumentBuilder(doc);

                                                                                                // Images usually are inserted using a URI, or a byte array.
                                                                                                // Every instance of a resource load will call our callback's ResourceLoading method.
                                                                                                builder.InsertImage("Google logo");
                                                                                                builder.InsertImage("Aspose logo");
                                                                                                builder.InsertImage("Watermark");

                                                                                                Assert.That(doc.GetChildNodes(NodeType.Shape, true).Count, Is.EqualTo(3));

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

                                                                                            /// <summary>
                                                                                            /// Allows us to load images into a document using predefined shorthands, as opposed to URIs.
                                                                                            /// This will separate image loading logic from the rest of the document construction.
                                                                                            /// </summary>
                                                                                            private class ImageNameHandler : IResourceLoadingCallback
                                                                                            {
                                                                                                public ResourceLoadingAction ResourceLoading(ResourceLoadingArgs args)
                                                                                                {
                                                                                                    // If this callback encounters one of the image shorthands while loading an image,
                                                                                                    // it will apply unique logic for each defined shorthand instead of treating it as a URI.
                                                                                                    if (args.ResourceType == ResourceType.Image)
                                                                                                        switch (args.OriginalUri)
                                                                                                        {
                                                                                                            case "Google logo":
                                                                                                                using (HttpClient client = new HttpClient())
                                                                                                                {
                                                                                                                    byte[] imageData = client.GetByteArrayAsync("http://www.google.com/images/logos/ps_logo2.png").GetAwaiter().GetResult();
                                                                                                                    args.SetData(imageData);
                                                                                                                }

                                                                                                                return ResourceLoadingAction.UserProvided;

                                                                                                            case "Aspose logo":
                                                                                                                args.SetData(File.ReadAllBytes(ImageDir + "Logo.jpg"));

                                                                                                                return ResourceLoadingAction.UserProvided;

                                                                                                            case "Watermark":
                                                                                                                args.SetData(File.ReadAllBytes(ImageDir + "Transparent background logo.png"));

                                                                                                                return ResourceLoadingAction.UserProvided;
                                                                                                        }

                                                                                                    return ResourceLoadingAction.Default;
                                                                                                }
                                                                                            }

Uri

URI of the resource which is used for downloading if Aspose.Words.Loading.IResourceLoadingCallback.ResourceLoading(Aspose.Words.Loading.ResourceLoadingArgs) returns Aspose.Words.Loading.ResourceLoadingAction.Default.

Initially it's set to absolute URI of the resource, but user can redefine it to any value.

public string Uri { get; set; }

Property Value

string

Methods

SetData(byte[])

Sets user provided data of the resource which is used if Aspose.Words.Loading.IResourceLoadingCallback.ResourceLoading(Aspose.Words.Loading.ResourceLoadingArgs) returns Aspose.Words.Loading.ResourceLoadingAction.UserProvided.

public void SetData(byte[] data)

Parameters

data byte []

Examples

Shows how to customize the process of loading external resources into a document.

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

                                                                                                DocumentBuilder builder = new DocumentBuilder(doc);

                                                                                                // Images usually are inserted using a URI, or a byte array.
                                                                                                // Every instance of a resource load will call our callback's ResourceLoading method.
                                                                                                builder.InsertImage("Google logo");
                                                                                                builder.InsertImage("Aspose logo");
                                                                                                builder.InsertImage("Watermark");

                                                                                                Assert.That(doc.GetChildNodes(NodeType.Shape, true).Count, Is.EqualTo(3));

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

                                                                                            /// <summary>
                                                                                            /// Allows us to load images into a document using predefined shorthands, as opposed to URIs.
                                                                                            /// This will separate image loading logic from the rest of the document construction.
                                                                                            /// </summary>
                                                                                            private class ImageNameHandler : IResourceLoadingCallback
                                                                                            {
                                                                                                public ResourceLoadingAction ResourceLoading(ResourceLoadingArgs args)
                                                                                                {
                                                                                                    // If this callback encounters one of the image shorthands while loading an image,
                                                                                                    // it will apply unique logic for each defined shorthand instead of treating it as a URI.
                                                                                                    if (args.ResourceType == ResourceType.Image)
                                                                                                        switch (args.OriginalUri)
                                                                                                        {
                                                                                                            case "Google logo":
                                                                                                                using (HttpClient client = new HttpClient())
                                                                                                                {
                                                                                                                    byte[] imageData = client.GetByteArrayAsync("http://www.google.com/images/logos/ps_logo2.png").GetAwaiter().GetResult();
                                                                                                                    args.SetData(imageData);
                                                                                                                }

                                                                                                                return ResourceLoadingAction.UserProvided;

                                                                                                            case "Aspose logo":
                                                                                                                args.SetData(File.ReadAllBytes(ImageDir + "Logo.jpg"));

                                                                                                                return ResourceLoadingAction.UserProvided;

                                                                                                            case "Watermark":
                                                                                                                args.SetData(File.ReadAllBytes(ImageDir + "Transparent background logo.png"));

                                                                                                                return ResourceLoadingAction.UserProvided;
                                                                                                        }

                                                                                                    return ResourceLoadingAction.Default;
                                                                                                }
                                                                                            }
 English