public class ResourceLoadingArgs
Example:
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");
}
/// <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 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;
}
}
Property Getters/Setters Summary | ||
---|---|---|
java.lang.String | getOriginalUri() | |
Original URI of the resource as specified in imported document.
|
||
int | getResourceType() | |
Type of resource.
The value of the property is ResourceType integer constant. |
||
java.lang.String | getUri() | |
void | setUri(java.lang.Stringvalue) | |
URI of the resource which is used for downloading
if Initially it's set to absolute URI of the resource, but user can redefine it to any value. |
Method Summary | ||
---|---|---|
void | setData(byte[] data) | |
Sets user provided data of the resource which is used
if |
public java.lang.String getOriginalUri()
Example:
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"); } /// <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 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; } }
public int getResourceType()
Example:
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"); } /// <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 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; } }
public java.lang.String getUri() / public void setUri(java.lang.String value)
URI of the resource which is used for downloading
if
Initially it's set to absolute URI of the resource, but user can redefine it to any value.
public void setData(byte[] data)
Example:
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"); } /// <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 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; } }