ResourceLoadingAction

Inheritance: java.lang.Object

public class ResourceLoadingAction

Specifies the mode of resource loading.

To learn more, visit the Specify Load Options documentation article.

Examples:

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


 public void resourceLoadingCallback() throws Exception {
     Document doc = new Document();
     doc.setResourceLoadingCallback(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.assertEquals(3, doc.getChildNodes(NodeType.SHAPE, true).getCount());

     doc.save(getArtifactsDir() + "DocumentBase.ResourceLoadingCallback.docx");
 }

 /// 
 /// 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.
 /// 
 private static class ImageNameHandler implements IResourceLoadingCallback {
     public int resourceLoading(final ResourceLoadingArgs args) throws URISyntaxException, IOException {
         if (args.getResourceType() == ResourceType.IMAGE) {
             // 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 ("Google logo".equals(args.getOriginalUri())) {
                 args.setData(DocumentHelper.getBytesFromStream(new URI("http://www.google.com/images/logos/ps_logo2.png").toURL().openStream()));

                 return ResourceLoadingAction.USER_PROVIDED;
             }

             if ("Aspose logo".equals(args.getOriginalUri())) {
                 args.setData(DocumentHelper.getBytesFromStream(getAsposelogoUri().toURL().openStream()));

                 return ResourceLoadingAction.USER_PROVIDED;
             }

             if ("Watermark".equals(args.getOriginalUri())) {
                 InputStream imageStream = new FileInputStream(getImageDir() + "Transparent background logo.png");
                 args.setData(DocumentHelper.getBytesFromStream(imageStream));

                 return ResourceLoadingAction.USER_PROVIDED;
             }
         }

         return ResourceLoadingAction.DEFAULT;
     }
 }
 

Fields

FieldDescription
DEFAULTAspose.Words will load this resource as usual.
SKIPAspose.Words will skip loading of this resource.
USER_PROVIDEDAspose.Words will use byte array provided by user in ResourceLoadingArgs.setData(byte[]) as resource data.
length

Methods

MethodDescription
fromName(String resourceLoadingActionName)
getName(int resourceLoadingAction)
getValues()
toString(int resourceLoadingAction)

DEFAULT

public static int DEFAULT

Aspose.Words will load this resource as usual.

SKIP

public static int SKIP

Aspose.Words will skip loading of this resource. Only link without data will be stored for an image, CSS style sheet will be ignored for HTML format.

USER_PROVIDED

public static int USER_PROVIDED

Aspose.Words will use byte array provided by user in ResourceLoadingArgs.setData(byte[]) as resource data.

length

public static int length

fromName(String resourceLoadingActionName)

public static int fromName(String resourceLoadingActionName)

Parameters:

ParameterTypeDescription
resourceLoadingActionNamejava.lang.String

Returns: int

getName(int resourceLoadingAction)

public static String getName(int resourceLoadingAction)

Parameters:

ParameterTypeDescription
resourceLoadingActionint

Returns: java.lang.String

getValues()

public static int[] getValues()

Returns: int[]

toString(int resourceLoadingAction)

public static String toString(int resourceLoadingAction)

Parameters:

ParameterTypeDescription
resourceLoadingActionint

Returns: java.lang.String