com.aspose.words

Class ResourceLoadingArgs

  • java.lang.Object
    • com.aspose.words.ResourceLoadingArgs
public class ResourceLoadingArgs 
extends java.lang.Object

Example:

Shows how to process inserted resources differently.
public void resourceLoadingCallback() throws Exception {
    Document doc = new Document();

    // Images belong to NodeType.Shape
    // There are none in a blank document
    Assert.assertEquals(doc.getChildNodes(NodeType.SHAPE, true).getCount(), 0);

    // Enable our custom image loading
    doc.setResourceLoadingCallback(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.assertEquals(doc.getChildNodes(NodeType.SHAPE, true).getCount(), 3);

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

private static class ImageNameHandler implements IResourceLoadingCallback {
    public int resourceLoading(final ResourceLoadingArgs args) throws URISyntaxException, IOException {
        if (args.getResourceType() == 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 ("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;
            }

            // 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 ("My Watermark".equals(args.getOriginalUri())) {
                InputStream imageStream = new FileInputStream(getImageDir() + "Watermark.png");
                args.setData(DocumentHelper.getBytesFromStream(imageStream));

                return ResourceLoadingAction.USER_PROVIDED;
            }
        }

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

Property Getters/Setters Summary
java.lang.StringgetOriginalUri()
Original URI of the resource as specified in imported document.
intgetResourceType()
Type of resource. The value of the property is ResourceType integer constant.
java.lang.StringgetUri()
void
setUri(java.lang.Stringvalue)
          

URI of the resource which is used for downloading if IResourceLoadingCallback.resourceLoading(com.aspose.words.ResourceLoadingArgs) returns ResourceLoadingAction.DEFAULT.

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

 
Method Summary
voidsetData(byte[] data)
 

    • Property Getters/Setters Detail

      • getOriginalUri

        public java.lang.String getOriginalUri()
        
        Original URI of the resource as specified in imported document.

        Example:

        Shows how to process inserted resources differently.
        public void resourceLoadingCallback() throws Exception {
            Document doc = new Document();
        
            // Images belong to NodeType.Shape
            // There are none in a blank document
            Assert.assertEquals(doc.getChildNodes(NodeType.SHAPE, true).getCount(), 0);
        
            // Enable our custom image loading
            doc.setResourceLoadingCallback(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.assertEquals(doc.getChildNodes(NodeType.SHAPE, true).getCount(), 3);
        
            doc.save(getArtifactsDir() + "DocumentBase.ResourceLoadingCallback.docx");
        }
        
        private static class ImageNameHandler implements IResourceLoadingCallback {
            public int resourceLoading(final ResourceLoadingArgs args) throws URISyntaxException, IOException {
                if (args.getResourceType() == 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 ("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;
                    }
        
                    // 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 ("My Watermark".equals(args.getOriginalUri())) {
                        InputStream imageStream = new FileInputStream(getImageDir() + "Watermark.png");
                        args.setData(DocumentHelper.getBytesFromStream(imageStream));
        
                        return ResourceLoadingAction.USER_PROVIDED;
                    }
                }
        
                // All other resources such as documents, CSS stylesheets and images passed as uris are handled as they were normally
                return ResourceLoadingAction.DEFAULT;
            }
        }
      • getResourceType

        public int getResourceType()
        
        Type of resource. The value of the property is ResourceType integer constant.

        Example:

        Shows how to process inserted resources differently.
        public void resourceLoadingCallback() throws Exception {
            Document doc = new Document();
        
            // Images belong to NodeType.Shape
            // There are none in a blank document
            Assert.assertEquals(doc.getChildNodes(NodeType.SHAPE, true).getCount(), 0);
        
            // Enable our custom image loading
            doc.setResourceLoadingCallback(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.assertEquals(doc.getChildNodes(NodeType.SHAPE, true).getCount(), 3);
        
            doc.save(getArtifactsDir() + "DocumentBase.ResourceLoadingCallback.docx");
        }
        
        private static class ImageNameHandler implements IResourceLoadingCallback {
            public int resourceLoading(final ResourceLoadingArgs args) throws URISyntaxException, IOException {
                if (args.getResourceType() == 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 ("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;
                    }
        
                    // 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 ("My Watermark".equals(args.getOriginalUri())) {
                        InputStream imageStream = new FileInputStream(getImageDir() + "Watermark.png");
                        args.setData(DocumentHelper.getBytesFromStream(imageStream));
        
                        return ResourceLoadingAction.USER_PROVIDED;
                    }
                }
        
                // All other resources such as documents, CSS stylesheets and images passed as uris are handled as they were normally
                return ResourceLoadingAction.DEFAULT;
            }
        }
    • Method Detail

      • setData

        public void setData(byte[] data)
        Sets user provided data of the resource which is used if IResourceLoadingCallback.resourceLoading(com.aspose.words.ResourceLoadingArgs) returns ResourceLoadingAction.USER_PROVIDED.

        Example:

        Shows how to process inserted resources differently.
        public void resourceLoadingCallback() throws Exception {
            Document doc = new Document();
        
            // Images belong to NodeType.Shape
            // There are none in a blank document
            Assert.assertEquals(doc.getChildNodes(NodeType.SHAPE, true).getCount(), 0);
        
            // Enable our custom image loading
            doc.setResourceLoadingCallback(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.assertEquals(doc.getChildNodes(NodeType.SHAPE, true).getCount(), 3);
        
            doc.save(getArtifactsDir() + "DocumentBase.ResourceLoadingCallback.docx");
        }
        
        private static class ImageNameHandler implements IResourceLoadingCallback {
            public int resourceLoading(final ResourceLoadingArgs args) throws URISyntaxException, IOException {
                if (args.getResourceType() == 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 ("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;
                    }
        
                    // 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 ("My Watermark".equals(args.getOriginalUri())) {
                        InputStream imageStream = new FileInputStream(getImageDir() + "Watermark.png");
                        args.setData(DocumentHelper.getBytesFromStream(imageStream));
        
                        return ResourceLoadingAction.USER_PROVIDED;
                    }
                }
        
                // All other resources such as documents, CSS stylesheets and images passed as uris are handled as they were normally
                return ResourceLoadingAction.DEFAULT;
            }
        }