com.aspose.words

Class ImageData

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

Defines an image for a shape.

Use the Shape.ImageData property to access and modify the image inside a shape. You do not create instances of the ImageData class directly.

An image can be stored inside a shape, linked to external file or both (linked and stored in the document).

Regardless of whether the image is stored inside the shape or linked, you can always access the actual image using the toByteArray(), toImage() or save(java.lang.String) methods. If the image is stored inside the shape, you can also directly access it using the ImageBytes property.

To store an image inside a shape use the setImage(java.lang.String) method. To link an image to a shape, set the SourceFullName property.

Example:

Shows how to extract images from a document and save them as files.
public void extractImagesToFiles() throws Exception {
    Document doc = new Document(getMyDir() + "Image.SampleImages.doc");

    NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true);
    int imageIndex = 0;
    for (Shape shape : (Iterable<Shape>) shapes) {
        if (shape.hasImage()) {
            String imageFileName = MessageFormat.format("Image.ExportImages.{0}{1}", imageIndex,
                    FileFormatUtil.imageTypeToExtension(shape.getImageData().getImageType()));
            shape.getImageData().save(getArtifactsDir() + imageFileName);
            imageIndex++;
        }
    }
}

Example:

Shows how to insert a linked image into a document.
DocumentBuilder builder = new DocumentBuilder();

String imageFileName = getImageDir() + "Hammer.wmf";

builder.write("Image linked, not stored in the document: ");

Shape linkedOnly = new Shape(builder.getDocument(), ShapeType.IMAGE);
linkedOnly.setWrapType(WrapType.INLINE);
linkedOnly.getImageData().setSourceFullName(imageFileName);

builder.insertNode(linkedOnly);
builder.writeln();

builder.write("Image linked and stored in the document: ");

Shape linkedAndStored = new Shape(builder.getDocument(), ShapeType.IMAGE);
linkedAndStored.setWrapType(WrapType.INLINE);
linkedAndStored.getImageData().setSourceFullName(imageFileName);
linkedAndStored.getImageData().setImage(imageFileName);

builder.insertNode(linkedAndStored);
builder.writeln();

builder.write("Image stored in the document, but not linked: ");

Shape stored = new Shape(builder.getDocument(), ShapeType.IMAGE);
stored.setWrapType(WrapType.INLINE);
stored.getImageData().setImage(imageFileName);

builder.insertNode(stored);
builder.writeln();

builder.getDocument().save(getArtifactsDir() + "Image.CreateLinkedImage.doc");

Property Getters/Setters Summary
booleangetBiLevel()
void
setBiLevel(booleanvalue)
           Determines whether an image will be displayed in black and white.
BorderCollectiongetBorders()
Gets the collection of borders of the image. Borders only have effect for inline images.
doublegetBrightness()
void
setBrightness(doublevalue)
           Gets or sets the brightness of the picture. The value for this property must be a number from 0.0 (dimmest) to 1.0 (brightest).
java.awt.ColorgetChromaKey()
void
setChromaKey(java.awt.Colorvalue)
           Defines the color value of the image that will be treated as transparent.
doublegetContrast()
void
setContrast(doublevalue)
           Gets or sets the contrast for the specified picture. The value for this property must be a number from 0.0 (the least contrast) to 1.0 (the greatest contrast).
doublegetCropBottom()
void
setCropBottom(doublevalue)
           Defines the fraction of picture removal from the bottom side.
doublegetCropLeft()
void
setCropLeft(doublevalue)
           Defines the fraction of picture removal from the left side.
doublegetCropRight()
void
setCropRight(doublevalue)
           Defines the fraction of picture removal from the right side.
doublegetCropTop()
void
setCropTop(doublevalue)
           Defines the fraction of picture removal from the top side.
booleangetGrayScale()
void
setGrayScale(booleanvalue)
           Determines whether a picture will display in grayscale mode.
booleanhasImage()
Returns true if the shape has image bytes or links an image.
byte[]getImageBytes()
void
setImageBytes(byte[]value)
           Gets or sets the raw bytes of the image stored in the shape.
ImageSizegetImageSize()
Gets the information about image size and resolution.
intgetImageType()
Gets the type of the image. The value of the property is ImageType integer constant.
booleanisLink()
Returns true if the image is linked to the shape (when SourceFullName is specified).
booleanisLinkOnly()
Returns true if the image is linked and not stored in the document.
java.lang.StringgetSourceFullName()
void
setSourceFullName(java.lang.Stringvalue)
           Gets or sets the path and name of the source file for the linked image.
java.lang.StringgetTitle()
void
setTitle(java.lang.Stringvalue)
           Defines the title of an image.
 
Method Summary
voidsave(java.io.OutputStream stream)
Saves the image into the specified stream.
voidsave(java.lang.String fileName)
Saves the image into a file.
voidsetImage(java.awt.image.BufferedImage image)
Sets the image that the shape displays.
voidsetImage(java.io.InputStream stream)
Sets the image that the shape displays.
voidsetImage(java.lang.String fileName)
Sets the image that the shape displays.
byte[]toByteArray()
Returns image bytes for any image regardless whether the image is stored or linked.
java.awt.image.BufferedImagetoImage()
Gets the image stored in the shape as a java BufferedImage object.
 

    • Property Getters/Setters Detail

      • getBiLevel/setBiLevel

        public boolean getBiLevel() / public void setBiLevel(boolean value)
        
        Determines whether an image will be displayed in black and white.

        The default value is false.

        Example:

        Shows how to edit images using the ImageData attribute.
        // Open a document that contains images
        Document imgSourceDoc = new Document(getMyDir() + "Image.SampleImages.doc");
        
        Shape sourceShape = (Shape) imgSourceDoc.getChildNodes(NodeType.SHAPE, true).get(0);
        
        Document dstDoc = new Document();
        
        // Import a shape from the source document and append it to the first paragraph, effectively cloning it
        Shape importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        // Get the ImageData of the imported shape
        ImageData imageData = importedShape.getImageData();
        imageData.setTitle("Imported Image");
        
        // If an image appears to have no borders, its ImageData object will still have them, but in an unspecified color
        Assert.assertEquals(imageData.getBorders().getCount(), 4);
        Assert.assertEquals(imageData.getBorders().get(0).getColor(), new Color(0, true));
        
        Assert.assertTrue(imageData.hasImage());
        
        // This image is not linked to a shape or to an image in the file system
        Assert.assertFalse(imageData.isLink());
        Assert.assertFalse(imageData.isLinkOnly());
        
        // Brightness and contrast are defined on a 0-1 scale, with 0.5 being the default value
        imageData.setBrightness(0.8d);
        imageData.setContrast(1.0d);
        
        // Our image will have a lot of white now that we've changed the brightness and contrast like that
        // We can treat white as transparent with the following attribute
        imageData.setChromaKey(Color.WHITE);
        
        // Import the source shape again, set it to black and white
        importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        importedShape.getImageData().setGrayScale(true);
        
        // Import the source shape again to create a third image, and set it to BiLevel
        // Unlike greyscale, which preserves the brightness of the original colors,
        // BiLevel sets every pixel to either black or white, whichever is closer to the original color
        importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        importedShape.getImageData().setBiLevel(true);
        
        // Cropping is determined on a 0-1 scale
        // Cropping a side by 0.3 will crop 30% of the image out at that side
        importedShape.getImageData().setCropBottom(0.3d);
        importedShape.getImageData().setCropLeft(0.3d);
        importedShape.getImageData().setCropTop(0.3d);
        importedShape.getImageData().setCropRight(0.3d);
        
        dstDoc.save(getArtifactsDir() + "ImageData.EditedImages.docx");
      • getBorders

        public BorderCollection getBorders()
        
        Gets the collection of borders of the image. Borders only have effect for inline images.

        Example:

        Shows how to edit images using the ImageData attribute.
        // Open a document that contains images
        Document imgSourceDoc = new Document(getMyDir() + "Image.SampleImages.doc");
        
        Shape sourceShape = (Shape) imgSourceDoc.getChildNodes(NodeType.SHAPE, true).get(0);
        
        Document dstDoc = new Document();
        
        // Import a shape from the source document and append it to the first paragraph, effectively cloning it
        Shape importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        // Get the ImageData of the imported shape
        ImageData imageData = importedShape.getImageData();
        imageData.setTitle("Imported Image");
        
        // If an image appears to have no borders, its ImageData object will still have them, but in an unspecified color
        Assert.assertEquals(imageData.getBorders().getCount(), 4);
        Assert.assertEquals(imageData.getBorders().get(0).getColor(), new Color(0, true));
        
        Assert.assertTrue(imageData.hasImage());
        
        // This image is not linked to a shape or to an image in the file system
        Assert.assertFalse(imageData.isLink());
        Assert.assertFalse(imageData.isLinkOnly());
        
        // Brightness and contrast are defined on a 0-1 scale, with 0.5 being the default value
        imageData.setBrightness(0.8d);
        imageData.setContrast(1.0d);
        
        // Our image will have a lot of white now that we've changed the brightness and contrast like that
        // We can treat white as transparent with the following attribute
        imageData.setChromaKey(Color.WHITE);
        
        // Import the source shape again, set it to black and white
        importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        importedShape.getImageData().setGrayScale(true);
        
        // Import the source shape again to create a third image, and set it to BiLevel
        // Unlike greyscale, which preserves the brightness of the original colors,
        // BiLevel sets every pixel to either black or white, whichever is closer to the original color
        importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        importedShape.getImageData().setBiLevel(true);
        
        // Cropping is determined on a 0-1 scale
        // Cropping a side by 0.3 will crop 30% of the image out at that side
        importedShape.getImageData().setCropBottom(0.3d);
        importedShape.getImageData().setCropLeft(0.3d);
        importedShape.getImageData().setCropTop(0.3d);
        importedShape.getImageData().setCropRight(0.3d);
        
        dstDoc.save(getArtifactsDir() + "ImageData.EditedImages.docx");
      • getBrightness/setBrightness

        public double getBrightness() / public void setBrightness(double value)
        
        Gets or sets the brightness of the picture. The value for this property must be a number from 0.0 (dimmest) to 1.0 (brightest).

        The default value is 0.5.

        Example:

        Shows how to edit images using the ImageData attribute.
        // Open a document that contains images
        Document imgSourceDoc = new Document(getMyDir() + "Image.SampleImages.doc");
        
        Shape sourceShape = (Shape) imgSourceDoc.getChildNodes(NodeType.SHAPE, true).get(0);
        
        Document dstDoc = new Document();
        
        // Import a shape from the source document and append it to the first paragraph, effectively cloning it
        Shape importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        // Get the ImageData of the imported shape
        ImageData imageData = importedShape.getImageData();
        imageData.setTitle("Imported Image");
        
        // If an image appears to have no borders, its ImageData object will still have them, but in an unspecified color
        Assert.assertEquals(imageData.getBorders().getCount(), 4);
        Assert.assertEquals(imageData.getBorders().get(0).getColor(), new Color(0, true));
        
        Assert.assertTrue(imageData.hasImage());
        
        // This image is not linked to a shape or to an image in the file system
        Assert.assertFalse(imageData.isLink());
        Assert.assertFalse(imageData.isLinkOnly());
        
        // Brightness and contrast are defined on a 0-1 scale, with 0.5 being the default value
        imageData.setBrightness(0.8d);
        imageData.setContrast(1.0d);
        
        // Our image will have a lot of white now that we've changed the brightness and contrast like that
        // We can treat white as transparent with the following attribute
        imageData.setChromaKey(Color.WHITE);
        
        // Import the source shape again, set it to black and white
        importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        importedShape.getImageData().setGrayScale(true);
        
        // Import the source shape again to create a third image, and set it to BiLevel
        // Unlike greyscale, which preserves the brightness of the original colors,
        // BiLevel sets every pixel to either black or white, whichever is closer to the original color
        importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        importedShape.getImageData().setBiLevel(true);
        
        // Cropping is determined on a 0-1 scale
        // Cropping a side by 0.3 will crop 30% of the image out at that side
        importedShape.getImageData().setCropBottom(0.3d);
        importedShape.getImageData().setCropLeft(0.3d);
        importedShape.getImageData().setCropTop(0.3d);
        importedShape.getImageData().setCropRight(0.3d);
        
        dstDoc.save(getArtifactsDir() + "ImageData.EditedImages.docx");
      • getChromaKey/setChromaKey

        public java.awt.Color getChromaKey() / public void setChromaKey(java.awt.Color value)
        
        Defines the color value of the image that will be treated as transparent.

        The default value is 0.

        Example:

        Shows how to edit images using the ImageData attribute.
        // Open a document that contains images
        Document imgSourceDoc = new Document(getMyDir() + "Image.SampleImages.doc");
        
        Shape sourceShape = (Shape) imgSourceDoc.getChildNodes(NodeType.SHAPE, true).get(0);
        
        Document dstDoc = new Document();
        
        // Import a shape from the source document and append it to the first paragraph, effectively cloning it
        Shape importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        // Get the ImageData of the imported shape
        ImageData imageData = importedShape.getImageData();
        imageData.setTitle("Imported Image");
        
        // If an image appears to have no borders, its ImageData object will still have them, but in an unspecified color
        Assert.assertEquals(imageData.getBorders().getCount(), 4);
        Assert.assertEquals(imageData.getBorders().get(0).getColor(), new Color(0, true));
        
        Assert.assertTrue(imageData.hasImage());
        
        // This image is not linked to a shape or to an image in the file system
        Assert.assertFalse(imageData.isLink());
        Assert.assertFalse(imageData.isLinkOnly());
        
        // Brightness and contrast are defined on a 0-1 scale, with 0.5 being the default value
        imageData.setBrightness(0.8d);
        imageData.setContrast(1.0d);
        
        // Our image will have a lot of white now that we've changed the brightness and contrast like that
        // We can treat white as transparent with the following attribute
        imageData.setChromaKey(Color.WHITE);
        
        // Import the source shape again, set it to black and white
        importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        importedShape.getImageData().setGrayScale(true);
        
        // Import the source shape again to create a third image, and set it to BiLevel
        // Unlike greyscale, which preserves the brightness of the original colors,
        // BiLevel sets every pixel to either black or white, whichever is closer to the original color
        importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        importedShape.getImageData().setBiLevel(true);
        
        // Cropping is determined on a 0-1 scale
        // Cropping a side by 0.3 will crop 30% of the image out at that side
        importedShape.getImageData().setCropBottom(0.3d);
        importedShape.getImageData().setCropLeft(0.3d);
        importedShape.getImageData().setCropTop(0.3d);
        importedShape.getImageData().setCropRight(0.3d);
        
        dstDoc.save(getArtifactsDir() + "ImageData.EditedImages.docx");
      • getContrast/setContrast

        public double getContrast() / public void setContrast(double value)
        
        Gets or sets the contrast for the specified picture. The value for this property must be a number from 0.0 (the least contrast) to 1.0 (the greatest contrast).

        The default value is 0.5.

        Example:

        Shows how to edit images using the ImageData attribute.
        // Open a document that contains images
        Document imgSourceDoc = new Document(getMyDir() + "Image.SampleImages.doc");
        
        Shape sourceShape = (Shape) imgSourceDoc.getChildNodes(NodeType.SHAPE, true).get(0);
        
        Document dstDoc = new Document();
        
        // Import a shape from the source document and append it to the first paragraph, effectively cloning it
        Shape importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        // Get the ImageData of the imported shape
        ImageData imageData = importedShape.getImageData();
        imageData.setTitle("Imported Image");
        
        // If an image appears to have no borders, its ImageData object will still have them, but in an unspecified color
        Assert.assertEquals(imageData.getBorders().getCount(), 4);
        Assert.assertEquals(imageData.getBorders().get(0).getColor(), new Color(0, true));
        
        Assert.assertTrue(imageData.hasImage());
        
        // This image is not linked to a shape or to an image in the file system
        Assert.assertFalse(imageData.isLink());
        Assert.assertFalse(imageData.isLinkOnly());
        
        // Brightness and contrast are defined on a 0-1 scale, with 0.5 being the default value
        imageData.setBrightness(0.8d);
        imageData.setContrast(1.0d);
        
        // Our image will have a lot of white now that we've changed the brightness and contrast like that
        // We can treat white as transparent with the following attribute
        imageData.setChromaKey(Color.WHITE);
        
        // Import the source shape again, set it to black and white
        importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        importedShape.getImageData().setGrayScale(true);
        
        // Import the source shape again to create a third image, and set it to BiLevel
        // Unlike greyscale, which preserves the brightness of the original colors,
        // BiLevel sets every pixel to either black or white, whichever is closer to the original color
        importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        importedShape.getImageData().setBiLevel(true);
        
        // Cropping is determined on a 0-1 scale
        // Cropping a side by 0.3 will crop 30% of the image out at that side
        importedShape.getImageData().setCropBottom(0.3d);
        importedShape.getImageData().setCropLeft(0.3d);
        importedShape.getImageData().setCropTop(0.3d);
        importedShape.getImageData().setCropRight(0.3d);
        
        dstDoc.save(getArtifactsDir() + "ImageData.EditedImages.docx");
      • getCropBottom/setCropBottom

        public double getCropBottom() / public void setCropBottom(double value)
        
        Defines the fraction of picture removal from the bottom side.

        The amount of cropping can range from -1.0 to 1.0. The default value is 0. Note that a value of 1 will display no picture at all. Negative values will result in the picture being squeezed inward from the edge being cropped (the empty space between the picture and the cropped edge will be filled by the fill color of the shape). Positive values less than 1 will result in the remaining picture being stretched to fit the shape.

        The default value is 0.

        Example:

        Shows how to edit images using the ImageData attribute.
        // Open a document that contains images
        Document imgSourceDoc = new Document(getMyDir() + "Image.SampleImages.doc");
        
        Shape sourceShape = (Shape) imgSourceDoc.getChildNodes(NodeType.SHAPE, true).get(0);
        
        Document dstDoc = new Document();
        
        // Import a shape from the source document and append it to the first paragraph, effectively cloning it
        Shape importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        // Get the ImageData of the imported shape
        ImageData imageData = importedShape.getImageData();
        imageData.setTitle("Imported Image");
        
        // If an image appears to have no borders, its ImageData object will still have them, but in an unspecified color
        Assert.assertEquals(imageData.getBorders().getCount(), 4);
        Assert.assertEquals(imageData.getBorders().get(0).getColor(), new Color(0, true));
        
        Assert.assertTrue(imageData.hasImage());
        
        // This image is not linked to a shape or to an image in the file system
        Assert.assertFalse(imageData.isLink());
        Assert.assertFalse(imageData.isLinkOnly());
        
        // Brightness and contrast are defined on a 0-1 scale, with 0.5 being the default value
        imageData.setBrightness(0.8d);
        imageData.setContrast(1.0d);
        
        // Our image will have a lot of white now that we've changed the brightness and contrast like that
        // We can treat white as transparent with the following attribute
        imageData.setChromaKey(Color.WHITE);
        
        // Import the source shape again, set it to black and white
        importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        importedShape.getImageData().setGrayScale(true);
        
        // Import the source shape again to create a third image, and set it to BiLevel
        // Unlike greyscale, which preserves the brightness of the original colors,
        // BiLevel sets every pixel to either black or white, whichever is closer to the original color
        importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        importedShape.getImageData().setBiLevel(true);
        
        // Cropping is determined on a 0-1 scale
        // Cropping a side by 0.3 will crop 30% of the image out at that side
        importedShape.getImageData().setCropBottom(0.3d);
        importedShape.getImageData().setCropLeft(0.3d);
        importedShape.getImageData().setCropTop(0.3d);
        importedShape.getImageData().setCropRight(0.3d);
        
        dstDoc.save(getArtifactsDir() + "ImageData.EditedImages.docx");
      • getCropLeft/setCropLeft

        public double getCropLeft() / public void setCropLeft(double value)
        
        Defines the fraction of picture removal from the left side.

        The amount of cropping can range from -1.0 to 1.0. The default value is 0. Note that a value of 1 will display no picture at all. Negative values will result in the picture being squeezed inward from the edge being cropped (the empty space between the picture and the cropped edge will be filled by the fill color of the shape). Positive values less than 1 will result in the remaining picture being stretched to fit the shape.

        The default value is 0.

        Example:

        Shows how to edit images using the ImageData attribute.
        // Open a document that contains images
        Document imgSourceDoc = new Document(getMyDir() + "Image.SampleImages.doc");
        
        Shape sourceShape = (Shape) imgSourceDoc.getChildNodes(NodeType.SHAPE, true).get(0);
        
        Document dstDoc = new Document();
        
        // Import a shape from the source document and append it to the first paragraph, effectively cloning it
        Shape importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        // Get the ImageData of the imported shape
        ImageData imageData = importedShape.getImageData();
        imageData.setTitle("Imported Image");
        
        // If an image appears to have no borders, its ImageData object will still have them, but in an unspecified color
        Assert.assertEquals(imageData.getBorders().getCount(), 4);
        Assert.assertEquals(imageData.getBorders().get(0).getColor(), new Color(0, true));
        
        Assert.assertTrue(imageData.hasImage());
        
        // This image is not linked to a shape or to an image in the file system
        Assert.assertFalse(imageData.isLink());
        Assert.assertFalse(imageData.isLinkOnly());
        
        // Brightness and contrast are defined on a 0-1 scale, with 0.5 being the default value
        imageData.setBrightness(0.8d);
        imageData.setContrast(1.0d);
        
        // Our image will have a lot of white now that we've changed the brightness and contrast like that
        // We can treat white as transparent with the following attribute
        imageData.setChromaKey(Color.WHITE);
        
        // Import the source shape again, set it to black and white
        importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        importedShape.getImageData().setGrayScale(true);
        
        // Import the source shape again to create a third image, and set it to BiLevel
        // Unlike greyscale, which preserves the brightness of the original colors,
        // BiLevel sets every pixel to either black or white, whichever is closer to the original color
        importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        importedShape.getImageData().setBiLevel(true);
        
        // Cropping is determined on a 0-1 scale
        // Cropping a side by 0.3 will crop 30% of the image out at that side
        importedShape.getImageData().setCropBottom(0.3d);
        importedShape.getImageData().setCropLeft(0.3d);
        importedShape.getImageData().setCropTop(0.3d);
        importedShape.getImageData().setCropRight(0.3d);
        
        dstDoc.save(getArtifactsDir() + "ImageData.EditedImages.docx");
      • getCropRight/setCropRight

        public double getCropRight() / public void setCropRight(double value)
        
        Defines the fraction of picture removal from the right side.

        The amount of cropping can range from -1.0 to 1.0. The default value is 0. Note that a value of 1 will display no picture at all. Negative values will result in the picture being squeezed inward from the edge being cropped (the empty space between the picture and the cropped edge will be filled by the fill color of the shape). Positive values less than 1 will result in the remaining picture being stretched to fit the shape.

        The default value is 0.

        Example:

        Shows how to edit images using the ImageData attribute.
        // Open a document that contains images
        Document imgSourceDoc = new Document(getMyDir() + "Image.SampleImages.doc");
        
        Shape sourceShape = (Shape) imgSourceDoc.getChildNodes(NodeType.SHAPE, true).get(0);
        
        Document dstDoc = new Document();
        
        // Import a shape from the source document and append it to the first paragraph, effectively cloning it
        Shape importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        // Get the ImageData of the imported shape
        ImageData imageData = importedShape.getImageData();
        imageData.setTitle("Imported Image");
        
        // If an image appears to have no borders, its ImageData object will still have them, but in an unspecified color
        Assert.assertEquals(imageData.getBorders().getCount(), 4);
        Assert.assertEquals(imageData.getBorders().get(0).getColor(), new Color(0, true));
        
        Assert.assertTrue(imageData.hasImage());
        
        // This image is not linked to a shape or to an image in the file system
        Assert.assertFalse(imageData.isLink());
        Assert.assertFalse(imageData.isLinkOnly());
        
        // Brightness and contrast are defined on a 0-1 scale, with 0.5 being the default value
        imageData.setBrightness(0.8d);
        imageData.setContrast(1.0d);
        
        // Our image will have a lot of white now that we've changed the brightness and contrast like that
        // We can treat white as transparent with the following attribute
        imageData.setChromaKey(Color.WHITE);
        
        // Import the source shape again, set it to black and white
        importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        importedShape.getImageData().setGrayScale(true);
        
        // Import the source shape again to create a third image, and set it to BiLevel
        // Unlike greyscale, which preserves the brightness of the original colors,
        // BiLevel sets every pixel to either black or white, whichever is closer to the original color
        importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        importedShape.getImageData().setBiLevel(true);
        
        // Cropping is determined on a 0-1 scale
        // Cropping a side by 0.3 will crop 30% of the image out at that side
        importedShape.getImageData().setCropBottom(0.3d);
        importedShape.getImageData().setCropLeft(0.3d);
        importedShape.getImageData().setCropTop(0.3d);
        importedShape.getImageData().setCropRight(0.3d);
        
        dstDoc.save(getArtifactsDir() + "ImageData.EditedImages.docx");
      • getCropTop/setCropTop

        public double getCropTop() / public void setCropTop(double value)
        
        Defines the fraction of picture removal from the top side.

        The amount of cropping can range from -1.0 to 1.0. The default value is 0. Note that a value of 1 will display no picture at all. Negative values will result in the picture being squeezed inward from the edge being cropped (the empty space between the picture and the cropped edge will be filled by the fill color of the shape). Positive values less than 1 will result in the remaining picture being stretched to fit the shape.

        The default value is 0.

        Example:

        Shows how to edit images using the ImageData attribute.
        // Open a document that contains images
        Document imgSourceDoc = new Document(getMyDir() + "Image.SampleImages.doc");
        
        Shape sourceShape = (Shape) imgSourceDoc.getChildNodes(NodeType.SHAPE, true).get(0);
        
        Document dstDoc = new Document();
        
        // Import a shape from the source document and append it to the first paragraph, effectively cloning it
        Shape importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        // Get the ImageData of the imported shape
        ImageData imageData = importedShape.getImageData();
        imageData.setTitle("Imported Image");
        
        // If an image appears to have no borders, its ImageData object will still have them, but in an unspecified color
        Assert.assertEquals(imageData.getBorders().getCount(), 4);
        Assert.assertEquals(imageData.getBorders().get(0).getColor(), new Color(0, true));
        
        Assert.assertTrue(imageData.hasImage());
        
        // This image is not linked to a shape or to an image in the file system
        Assert.assertFalse(imageData.isLink());
        Assert.assertFalse(imageData.isLinkOnly());
        
        // Brightness and contrast are defined on a 0-1 scale, with 0.5 being the default value
        imageData.setBrightness(0.8d);
        imageData.setContrast(1.0d);
        
        // Our image will have a lot of white now that we've changed the brightness and contrast like that
        // We can treat white as transparent with the following attribute
        imageData.setChromaKey(Color.WHITE);
        
        // Import the source shape again, set it to black and white
        importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        importedShape.getImageData().setGrayScale(true);
        
        // Import the source shape again to create a third image, and set it to BiLevel
        // Unlike greyscale, which preserves the brightness of the original colors,
        // BiLevel sets every pixel to either black or white, whichever is closer to the original color
        importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        importedShape.getImageData().setBiLevel(true);
        
        // Cropping is determined on a 0-1 scale
        // Cropping a side by 0.3 will crop 30% of the image out at that side
        importedShape.getImageData().setCropBottom(0.3d);
        importedShape.getImageData().setCropLeft(0.3d);
        importedShape.getImageData().setCropTop(0.3d);
        importedShape.getImageData().setCropRight(0.3d);
        
        dstDoc.save(getArtifactsDir() + "ImageData.EditedImages.docx");
      • getGrayScale/setGrayScale

        public boolean getGrayScale() / public void setGrayScale(boolean value)
        
        Determines whether a picture will display in grayscale mode.

        The default value is false.

        Example:

        Shows how to edit images using the ImageData attribute.
        // Open a document that contains images
        Document imgSourceDoc = new Document(getMyDir() + "Image.SampleImages.doc");
        
        Shape sourceShape = (Shape) imgSourceDoc.getChildNodes(NodeType.SHAPE, true).get(0);
        
        Document dstDoc = new Document();
        
        // Import a shape from the source document and append it to the first paragraph, effectively cloning it
        Shape importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        // Get the ImageData of the imported shape
        ImageData imageData = importedShape.getImageData();
        imageData.setTitle("Imported Image");
        
        // If an image appears to have no borders, its ImageData object will still have them, but in an unspecified color
        Assert.assertEquals(imageData.getBorders().getCount(), 4);
        Assert.assertEquals(imageData.getBorders().get(0).getColor(), new Color(0, true));
        
        Assert.assertTrue(imageData.hasImage());
        
        // This image is not linked to a shape or to an image in the file system
        Assert.assertFalse(imageData.isLink());
        Assert.assertFalse(imageData.isLinkOnly());
        
        // Brightness and contrast are defined on a 0-1 scale, with 0.5 being the default value
        imageData.setBrightness(0.8d);
        imageData.setContrast(1.0d);
        
        // Our image will have a lot of white now that we've changed the brightness and contrast like that
        // We can treat white as transparent with the following attribute
        imageData.setChromaKey(Color.WHITE);
        
        // Import the source shape again, set it to black and white
        importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        importedShape.getImageData().setGrayScale(true);
        
        // Import the source shape again to create a third image, and set it to BiLevel
        // Unlike greyscale, which preserves the brightness of the original colors,
        // BiLevel sets every pixel to either black or white, whichever is closer to the original color
        importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        importedShape.getImageData().setBiLevel(true);
        
        // Cropping is determined on a 0-1 scale
        // Cropping a side by 0.3 will crop 30% of the image out at that side
        importedShape.getImageData().setCropBottom(0.3d);
        importedShape.getImageData().setCropLeft(0.3d);
        importedShape.getImageData().setCropTop(0.3d);
        importedShape.getImageData().setCropRight(0.3d);
        
        dstDoc.save(getArtifactsDir() + "ImageData.EditedImages.docx");
      • hasImage

        public boolean hasImage()
        
        Returns true if the shape has image bytes or links an image.

        Example:

        Shows how to save all the images from a document to the file system.
        Document imgSourceDoc = new Document(getMyDir() + "Image.SampleImages.doc");
        
        // Images are stored as shapes
        // Get into the document's shape collection to verify that it contains 6 images
        NodeCollection shapes = imgSourceDoc.getChildNodes(NodeType.SHAPE, true);
        Assert.assertEquals(shapes.getCount(), 6);
        
        // Go over all of the document's shapes
        // If a shape contains image data, save the image in the local file system
        for (int i = 0; i < shapes.getCount(); i++) {
            Shape imageShape = (Shape) shapes.get(i);
            ImageData imageData = imageShape.getImageData();
        
            if (imageData.hasImage()) {
                OutputStream fileStream = new FileOutputStream(getArtifactsDir() + MessageFormat.format("Image from shape {0}.jpeg", i));
                try {
                    imageData.save(fileStream);
                } finally {
                    if (fileStream != null) fileStream.close();
                }
            }
        }
      • getImageBytes/setImageBytes

        public byte[] getImageBytes() / public void setImageBytes(byte[] value)
        
        Gets or sets the raw bytes of the image stored in the shape.

        Setting the value to null or an empty array will remove the image from the shape.

        Returns null if the image is not stored in the document (e.g the image is probably linked in this case).

        Example:

        Shows how to access raw image data in a shape's ImageData object.
        public void getDataFromImage() throws Exception {
        
            Document imgSourceDoc = new Document(getMyDir() + "Image.SampleImages.doc");
        
            // Images are stored as shapes
            // Get into the document's shape collection to verify that it contains 6 images
            NodeCollection shapes = imgSourceDoc.getChildNodes(NodeType.SHAPE, true);
            Assert.assertEquals(shapes.getCount(), 6);
        
            Shape imageShape = (Shape) shapes.get(0);
        
            // ToByteArray() returns the value of the ImageBytes property
            Assert.assertEquals(imageShape.getImageData().getImageBytes(), imageShape.getImageData().toByteArray());
        
            // Put the shape's image data into a stream
            // Then, put the image data from that stream into another stream which creates an image file in the local file system
            InputStream imgStream = imageShape.getImageData().toStream();
        
            try {
                File imageFile = new File(getArtifactsDir() + "MyImg.png");
                imageFile.createNewFile();
                copyInputStreamToFile(imgStream, imageFile);
            } finally {
                if (imgStream != null) imgStream.close();
            }
        }
        
        private void copyInputStreamToFile(InputStream in, File file) {
            try {
                OutputStream out = new FileOutputStream(file);
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                out.close();
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        See Also:
        setImage(java.lang.String), toByteArray(), toImage(), save(java.lang.String)
      • getImageSize

        public ImageSize getImageSize()
        
        Gets the information about image size and resolution.

        If the image is linked only and not stored in the document, returns zero size.

        Example:

        Shows how to resize an image shape.
        DocumentBuilder builder = new DocumentBuilder();
        
        // By default, the image is inserted at 100% scale.
        Shape shape = builder.insertImage(getImageDir() + "Aspose.Words.gif");
        
        // It is easy to change the shape size. In this case, make it 50% relative to the current shape size.
        shape.setWidth(shape.getWidth() * 0.5);
        shape.setHeight(shape.getHeight() * 0.5);
        
        // However, we can also go back to the original image size and scale from there, say 110%.
        ImageSize imageSize = shape.getImageData().getImageSize();
        shape.setWidth(imageSize.getWidthPoints() * 1.1);
        shape.setHeight(imageSize.getHeightPoints() * 1.1);
        
        builder.getDocument().save(getArtifactsDir() + "Image.ScaleImage.doc");
      • getImageType

        public int getImageType()
        
        Gets the type of the image. The value of the property is ImageType integer constant.

        Example:

        Shows how to extract images from a document and save them as files.
        public void extractImagesToFiles() throws Exception {
            Document doc = new Document(getMyDir() + "Image.SampleImages.doc");
        
            NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true);
            int imageIndex = 0;
            for (Shape shape : (Iterable<Shape>) shapes) {
                if (shape.hasImage()) {
                    String imageFileName = MessageFormat.format("Image.ExportImages.{0}{1}", imageIndex,
                            FileFormatUtil.imageTypeToExtension(shape.getImageData().getImageType()));
                    shape.getImageData().save(getArtifactsDir() + imageFileName);
                    imageIndex++;
                }
            }
        }
      • isLink

        public boolean isLink()
        
        Returns true if the image is linked to the shape (when SourceFullName is specified).

        Example:

        Shows how to edit images using the ImageData attribute.
        // Open a document that contains images
        Document imgSourceDoc = new Document(getMyDir() + "Image.SampleImages.doc");
        
        Shape sourceShape = (Shape) imgSourceDoc.getChildNodes(NodeType.SHAPE, true).get(0);
        
        Document dstDoc = new Document();
        
        // Import a shape from the source document and append it to the first paragraph, effectively cloning it
        Shape importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        // Get the ImageData of the imported shape
        ImageData imageData = importedShape.getImageData();
        imageData.setTitle("Imported Image");
        
        // If an image appears to have no borders, its ImageData object will still have them, but in an unspecified color
        Assert.assertEquals(imageData.getBorders().getCount(), 4);
        Assert.assertEquals(imageData.getBorders().get(0).getColor(), new Color(0, true));
        
        Assert.assertTrue(imageData.hasImage());
        
        // This image is not linked to a shape or to an image in the file system
        Assert.assertFalse(imageData.isLink());
        Assert.assertFalse(imageData.isLinkOnly());
        
        // Brightness and contrast are defined on a 0-1 scale, with 0.5 being the default value
        imageData.setBrightness(0.8d);
        imageData.setContrast(1.0d);
        
        // Our image will have a lot of white now that we've changed the brightness and contrast like that
        // We can treat white as transparent with the following attribute
        imageData.setChromaKey(Color.WHITE);
        
        // Import the source shape again, set it to black and white
        importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        importedShape.getImageData().setGrayScale(true);
        
        // Import the source shape again to create a third image, and set it to BiLevel
        // Unlike greyscale, which preserves the brightness of the original colors,
        // BiLevel sets every pixel to either black or white, whichever is closer to the original color
        importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        importedShape.getImageData().setBiLevel(true);
        
        // Cropping is determined on a 0-1 scale
        // Cropping a side by 0.3 will crop 30% of the image out at that side
        importedShape.getImageData().setCropBottom(0.3d);
        importedShape.getImageData().setCropLeft(0.3d);
        importedShape.getImageData().setCropTop(0.3d);
        importedShape.getImageData().setCropRight(0.3d);
        
        dstDoc.save(getArtifactsDir() + "ImageData.EditedImages.docx");
      • isLinkOnly

        public boolean isLinkOnly()
        
        Returns true if the image is linked and not stored in the document.

        Example:

        Shows how to edit images using the ImageData attribute.
        // Open a document that contains images
        Document imgSourceDoc = new Document(getMyDir() + "Image.SampleImages.doc");
        
        Shape sourceShape = (Shape) imgSourceDoc.getChildNodes(NodeType.SHAPE, true).get(0);
        
        Document dstDoc = new Document();
        
        // Import a shape from the source document and append it to the first paragraph, effectively cloning it
        Shape importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        // Get the ImageData of the imported shape
        ImageData imageData = importedShape.getImageData();
        imageData.setTitle("Imported Image");
        
        // If an image appears to have no borders, its ImageData object will still have them, but in an unspecified color
        Assert.assertEquals(imageData.getBorders().getCount(), 4);
        Assert.assertEquals(imageData.getBorders().get(0).getColor(), new Color(0, true));
        
        Assert.assertTrue(imageData.hasImage());
        
        // This image is not linked to a shape or to an image in the file system
        Assert.assertFalse(imageData.isLink());
        Assert.assertFalse(imageData.isLinkOnly());
        
        // Brightness and contrast are defined on a 0-1 scale, with 0.5 being the default value
        imageData.setBrightness(0.8d);
        imageData.setContrast(1.0d);
        
        // Our image will have a lot of white now that we've changed the brightness and contrast like that
        // We can treat white as transparent with the following attribute
        imageData.setChromaKey(Color.WHITE);
        
        // Import the source shape again, set it to black and white
        importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        importedShape.getImageData().setGrayScale(true);
        
        // Import the source shape again to create a third image, and set it to BiLevel
        // Unlike greyscale, which preserves the brightness of the original colors,
        // BiLevel sets every pixel to either black or white, whichever is closer to the original color
        importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        importedShape.getImageData().setBiLevel(true);
        
        // Cropping is determined on a 0-1 scale
        // Cropping a side by 0.3 will crop 30% of the image out at that side
        importedShape.getImageData().setCropBottom(0.3d);
        importedShape.getImageData().setCropLeft(0.3d);
        importedShape.getImageData().setCropTop(0.3d);
        importedShape.getImageData().setCropRight(0.3d);
        
        dstDoc.save(getArtifactsDir() + "ImageData.EditedImages.docx");
      • getSourceFullName/setSourceFullName

        public java.lang.String getSourceFullName() / public void setSourceFullName(java.lang.String value)
        
        Gets or sets the path and name of the source file for the linked image.

        The default value is an empty string.

        If SourceFullName is not an empty string, the image is linked.

        Example:

        Shows how to insert a linked image into a document.
        DocumentBuilder builder = new DocumentBuilder();
        
        String imageFileName = getImageDir() + "Hammer.wmf";
        
        builder.write("Image linked, not stored in the document: ");
        
        Shape linkedOnly = new Shape(builder.getDocument(), ShapeType.IMAGE);
        linkedOnly.setWrapType(WrapType.INLINE);
        linkedOnly.getImageData().setSourceFullName(imageFileName);
        
        builder.insertNode(linkedOnly);
        builder.writeln();
        
        builder.write("Image linked and stored in the document: ");
        
        Shape linkedAndStored = new Shape(builder.getDocument(), ShapeType.IMAGE);
        linkedAndStored.setWrapType(WrapType.INLINE);
        linkedAndStored.getImageData().setSourceFullName(imageFileName);
        linkedAndStored.getImageData().setImage(imageFileName);
        
        builder.insertNode(linkedAndStored);
        builder.writeln();
        
        builder.write("Image stored in the document, but not linked: ");
        
        Shape stored = new Shape(builder.getDocument(), ShapeType.IMAGE);
        stored.setWrapType(WrapType.INLINE);
        stored.getImageData().setImage(imageFileName);
        
        builder.insertNode(stored);
        builder.writeln();
        
        builder.getDocument().save(getArtifactsDir() + "Image.CreateLinkedImage.doc");
      • getTitle/setTitle

        public java.lang.String getTitle() / public void setTitle(java.lang.String value)
        
        Defines the title of an image.

        The default value is an empty string.

        Example:

        Shows how to edit images using the ImageData attribute.
        // Open a document that contains images
        Document imgSourceDoc = new Document(getMyDir() + "Image.SampleImages.doc");
        
        Shape sourceShape = (Shape) imgSourceDoc.getChildNodes(NodeType.SHAPE, true).get(0);
        
        Document dstDoc = new Document();
        
        // Import a shape from the source document and append it to the first paragraph, effectively cloning it
        Shape importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        // Get the ImageData of the imported shape
        ImageData imageData = importedShape.getImageData();
        imageData.setTitle("Imported Image");
        
        // If an image appears to have no borders, its ImageData object will still have them, but in an unspecified color
        Assert.assertEquals(imageData.getBorders().getCount(), 4);
        Assert.assertEquals(imageData.getBorders().get(0).getColor(), new Color(0, true));
        
        Assert.assertTrue(imageData.hasImage());
        
        // This image is not linked to a shape or to an image in the file system
        Assert.assertFalse(imageData.isLink());
        Assert.assertFalse(imageData.isLinkOnly());
        
        // Brightness and contrast are defined on a 0-1 scale, with 0.5 being the default value
        imageData.setBrightness(0.8d);
        imageData.setContrast(1.0d);
        
        // Our image will have a lot of white now that we've changed the brightness and contrast like that
        // We can treat white as transparent with the following attribute
        imageData.setChromaKey(Color.WHITE);
        
        // Import the source shape again, set it to black and white
        importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        importedShape.getImageData().setGrayScale(true);
        
        // Import the source shape again to create a third image, and set it to BiLevel
        // Unlike greyscale, which preserves the brightness of the original colors,
        // BiLevel sets every pixel to either black or white, whichever is closer to the original color
        importedShape = (Shape) dstDoc.importNode(sourceShape, true);
        dstDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
        
        importedShape.getImageData().setBiLevel(true);
        
        // Cropping is determined on a 0-1 scale
        // Cropping a side by 0.3 will crop 30% of the image out at that side
        importedShape.getImageData().setCropBottom(0.3d);
        importedShape.getImageData().setCropLeft(0.3d);
        importedShape.getImageData().setCropTop(0.3d);
        importedShape.getImageData().setCropRight(0.3d);
        
        dstDoc.save(getArtifactsDir() + "ImageData.EditedImages.docx");
    • Method Detail

      • save

        public void save(java.io.OutputStream stream)
                 throws java.lang.Exception
        Saves the image into the specified stream.

        Is it the responsibility of the caller to dispose the stream object.

        Parameters:
        stream - The stream where to save the image to.

        Example:

        Shows how to save all the images from a document to the file system.
        Document imgSourceDoc = new Document(getMyDir() + "Image.SampleImages.doc");
        
        // Images are stored as shapes
        // Get into the document's shape collection to verify that it contains 6 images
        NodeCollection shapes = imgSourceDoc.getChildNodes(NodeType.SHAPE, true);
        Assert.assertEquals(shapes.getCount(), 6);
        
        // Go over all of the document's shapes
        // If a shape contains image data, save the image in the local file system
        for (int i = 0; i < shapes.getCount(); i++) {
            Shape imageShape = (Shape) shapes.get(i);
            ImageData imageData = imageShape.getImageData();
        
            if (imageData.hasImage()) {
                OutputStream fileStream = new FileOutputStream(getArtifactsDir() + MessageFormat.format("Image from shape {0}.jpeg", i));
                try {
                    imageData.save(fileStream);
                } finally {
                    if (fileStream != null) fileStream.close();
                }
            }
        }
      • save

        public void save(java.lang.String fileName)
                 throws java.lang.Exception
        Saves the image into a file.
        Parameters:
        fileName - The file name where to save the image.

        Example:

        Shows how to extract images from a document and save them as files.
        public void extractImagesToFiles() throws Exception {
            Document doc = new Document(getMyDir() + "Image.SampleImages.doc");
        
            NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true);
            int imageIndex = 0;
            for (Shape shape : (Iterable<Shape>) shapes) {
                if (shape.hasImage()) {
                    String imageFileName = MessageFormat.format("Image.ExportImages.{0}{1}", imageIndex,
                            FileFormatUtil.imageTypeToExtension(shape.getImageData().getImageType()));
                    shape.getImageData().save(getArtifactsDir() + imageFileName);
                    imageIndex++;
                }
            }
        }
      • setImage

        public void setImage(java.awt.image.BufferedImage image)
                     throws java.lang.Exception
        Sets the image that the shape displays.
        Parameters:
        image - The image object.

        Example:

        Shows two ways of importing images from the local file system into a document.
        Document doc = new Document();
        
        // We can get an image from a file, set it as the image of a shape and append it to a paragraph
        BufferedImage srcImage = ImageIO.read(new File(getImageDir() + "Aspose.Words.gif"));
        
        Shape imgShape = new Shape(doc, ShapeType.IMAGE);
        doc.getFirstSection().getBody().getFirstParagraph().appendChild(imgShape);
        imgShape.getImageData().setImage(srcImage);
        srcImage.flush();
        
        // We can also open an image file using a stream and set its contents as a shape's image
        InputStream stream = new FileInputStream(getImageDir() + "Aspose.Words.gif");
        try {
            imgShape = new Shape(doc, ShapeType.IMAGE);
            doc.getFirstSection().getBody().getFirstParagraph().appendChild(imgShape);
            imgShape.getImageData().setImage(stream);
            imgShape.setLeft(150.0f);
        } finally {
            if (stream != null) stream.close();
        }
        
        doc.save(getArtifactsDir() + "ImageData.ImportedImage.docx");
      • setImage

        public void setImage(java.io.InputStream stream)
                     throws java.lang.Exception
        Sets the image that the shape displays.
        Parameters:
        stream - The stream that contains the image. The stream will be read from the current position, so one should be careful about stream position.

        Example:

        Shows two ways of importing images from the local file system into a document.
        Document doc = new Document();
        
        // We can get an image from a file, set it as the image of a shape and append it to a paragraph
        BufferedImage srcImage = ImageIO.read(new File(getImageDir() + "Aspose.Words.gif"));
        
        Shape imgShape = new Shape(doc, ShapeType.IMAGE);
        doc.getFirstSection().getBody().getFirstParagraph().appendChild(imgShape);
        imgShape.getImageData().setImage(srcImage);
        srcImage.flush();
        
        // We can also open an image file using a stream and set its contents as a shape's image
        InputStream stream = new FileInputStream(getImageDir() + "Aspose.Words.gif");
        try {
            imgShape = new Shape(doc, ShapeType.IMAGE);
            doc.getFirstSection().getBody().getFirstParagraph().appendChild(imgShape);
            imgShape.getImageData().setImage(stream);
            imgShape.setLeft(150.0f);
        } finally {
            if (stream != null) stream.close();
        }
        
        doc.save(getArtifactsDir() + "ImageData.ImportedImage.docx");
      • setImage

        public void setImage(java.lang.String fileName)
                     throws java.lang.Exception
        Sets the image that the shape displays.
        Parameters:
        fileName - The image file. Can be a file name or a URL.

        Example:

        Shows how to insert a linked image into a document.
        DocumentBuilder builder = new DocumentBuilder();
        
        String imageFileName = getImageDir() + "Hammer.wmf";
        
        builder.write("Image linked, not stored in the document: ");
        
        Shape linkedOnly = new Shape(builder.getDocument(), ShapeType.IMAGE);
        linkedOnly.setWrapType(WrapType.INLINE);
        linkedOnly.getImageData().setSourceFullName(imageFileName);
        
        builder.insertNode(linkedOnly);
        builder.writeln();
        
        builder.write("Image linked and stored in the document: ");
        
        Shape linkedAndStored = new Shape(builder.getDocument(), ShapeType.IMAGE);
        linkedAndStored.setWrapType(WrapType.INLINE);
        linkedAndStored.getImageData().setSourceFullName(imageFileName);
        linkedAndStored.getImageData().setImage(imageFileName);
        
        builder.insertNode(linkedAndStored);
        builder.writeln();
        
        builder.write("Image stored in the document, but not linked: ");
        
        Shape stored = new Shape(builder.getDocument(), ShapeType.IMAGE);
        stored.setWrapType(WrapType.INLINE);
        stored.getImageData().setImage(imageFileName);
        
        builder.insertNode(stored);
        builder.writeln();
        
        builder.getDocument().save(getArtifactsDir() + "Image.CreateLinkedImage.doc");
      • toByteArray

        public byte[] toByteArray()
                          throws java.lang.Exception
        Returns image bytes for any image regardless whether the image is stored or linked.

        If the image is linked, downloads the image every time it is called.

        Returns:
        See Also:
        ImageBytes

        Example:

        Shows how to access raw image data in a shape's ImageData object.
        public void getDataFromImage() throws Exception {
        
            Document imgSourceDoc = new Document(getMyDir() + "Image.SampleImages.doc");
        
            // Images are stored as shapes
            // Get into the document's shape collection to verify that it contains 6 images
            NodeCollection shapes = imgSourceDoc.getChildNodes(NodeType.SHAPE, true);
            Assert.assertEquals(shapes.getCount(), 6);
        
            Shape imageShape = (Shape) shapes.get(0);
        
            // ToByteArray() returns the value of the ImageBytes property
            Assert.assertEquals(imageShape.getImageData().getImageBytes(), imageShape.getImageData().toByteArray());
        
            // Put the shape's image data into a stream
            // Then, put the image data from that stream into another stream which creates an image file in the local file system
            InputStream imgStream = imageShape.getImageData().toStream();
        
            try {
                File imageFile = new File(getArtifactsDir() + "MyImg.png");
                imageFile.createNewFile();
                copyInputStreamToFile(imgStream, imageFile);
            } finally {
                if (imgStream != null) imgStream.close();
            }
        }
        
        private void copyInputStreamToFile(InputStream in, File file) {
            try {
                OutputStream out = new FileOutputStream(file);
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                out.close();
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
      • toImage

        public java.awt.image.BufferedImage toImage()
                             throws java.lang.Exception
        Gets the image stored in the shape as a java BufferedImage object.

        Tries to create a new java.awt.image.BufferedImage object from image bytes every time this method is called. If javax.imageio.ImageReader can't read image bytes (emf, wmf, tiff, etc.) the method returns null.

        It is the responsibility of the caller to dispose the image object.

        Returns:

        Example:

        Shows how to save all the images from a document to the file system.
        Document imgSourceDoc = new Document(getMyDir() + "Image.SampleImages.doc");
        
        // Images are stored as shapes
        // Get into the document's shape collection to verify that it contains 6 images
        NodeCollection shapes = imgSourceDoc.getChildNodes(NodeType.SHAPE, true);
        Assert.assertEquals(shapes.getCount(), 6);
        
        // Go over all of the document's shapes
        // If a shape contains image data, save the image in the local file system
        for (int i = 0; i < shapes.getCount(); i++) {
            Shape imageShape = (Shape) shapes.get(i);
            ImageData imageData = imageShape.getImageData();
        
            if (imageData.hasImage()) {
                OutputStream fileStream = new FileOutputStream(getArtifactsDir() + MessageFormat.format("Image from shape {0}.jpeg", i));
                try {
                    imageData.save(fileStream);
                } finally {
                    if (fileStream != null) fileStream.close();
                }
            }
        }