com.aspose.words
Class ImageData

java.lang.Object
    extended by 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 toImage(), toStream() and 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 insert a linked image into a document.
DocumentBuilder builder = new DocumentBuilder();

String imageFileName = getMyDir() + "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(getMyDir() + "Image.CreateLinkedImage Out.doc");

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<Shape> shapes = doc.getChildNodes(NodeType.SHAPE, true, false);
    int imageIndex = 0;
    for(Shape shape :  shapes)
    {
        if (shape.hasImage())
        {
            String extension = imageTypeToExtension(shape.getImageData().getImageType());
            String imageFileName = MessageFormat.format("Image.ExportImages.{0} Out.{1}", imageIndex, extension);
            shape.getImageData().save(getMyDir() + imageFileName);
            imageIndex++;

        }
    }
}

private static String imageTypeToExtension(int imageType) throws Exception
{
    switch (imageType)
    {
        case ImageType.BMP:
            return "bmp";
        case ImageType.EMF:
            return "emf";
        case ImageType.JPEG:
            return "jpeg";
        case ImageType.PICT:
            return "pict";
        case ImageType.PNG:
            return "png";
        case ImageType.WMF:
            return "wmf";
        default:
            throw new Exception("Unknown image type.");
    }
}

Property Getters/Setters Summary
booleangetBiLevel()
voidsetBiLevel(boolean value)
           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()
voidsetBrightness(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).
java.awt.ColorgetChromaKey()
voidsetChromaKey(java.awt.Color value)
           Defines the color value of the image that will be treated as transparent.
doublegetContrast()
voidsetContrast(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).
doublegetCropBottom()
voidsetCropBottom(double value)
           Defines the fraction of picture removal from the bottom side.
doublegetCropLeft()
voidsetCropLeft(double value)
           Defines the fraction of picture removal from the left side.
doublegetCropRight()
voidsetCropRight(double value)
           Defines the fraction of picture removal from the right side.
doublegetCropTop()
voidsetCropTop(double value)
           Defines the fraction of picture removal from the top side.
booleangetGrayScale()
voidsetGrayScale(boolean value)
           Determines whether a picture will display in grayscale mode.
booleanhasImage()
           Returns true if the shape has image bytes or links an image.
byte[]getImageBytes()
           Gets 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()
voidsetSourceFullName(java.lang.String value)
           Gets or sets the path and name of the source file for the linked image.
java.lang.StringgetTitle()
voidsetTitle(java.lang.String value)
           Defines the title of an image.
 
Method Summary
java.lang.ObjectfetchInheritedBorderAttr(int key)
          Reserved for internal use.
java.lang.ObjectgetDirectBorderAttr(int key)
          Reserved for internal use.
voidsave(java.io.OutputStream stream)
           Saves the image into the specified stream.
voidsave(java.lang.String fileName)
           Saves the image into a file.
voidsetBorderAttr(int key, java.lang.Object value)
          Reserved for internal use.
voidsetImage(byte[] imageBytes)
           Sets the image that the shape displays.
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.
java.io.InputStreamtoInputStream()
          
System.IO.StreamtoStream()
           Creates and returns a stream that contains the image bytes.
 

Property Getters/Setters Detail

getImageBytes

public byte[] getImageBytes()
Gets the raw bytes of the image stored in the shape.

Returns null if the image is not stored in the document.

See Also:
setImage(java.lang.String), toByteArray(), toImage(), toStream(), save(java.lang.String)

hasImage

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

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(getMyDir() + "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(getMyDir() + "Image.ScaleImage Out.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<Shape> shapes = doc.getChildNodes(NodeType.SHAPE, true, false);
    int imageIndex = 0;
    for(Shape shape :  shapes)
    {
        if (shape.hasImage())
        {
            String extension = imageTypeToExtension(shape.getImageData().getImageType());
            String imageFileName = MessageFormat.format("Image.ExportImages.{0} Out.{1}", imageIndex, extension);
            shape.getImageData().save(getMyDir() + imageFileName);
            imageIndex++;

        }
    }
}

private static String imageTypeToExtension(int imageType) throws Exception
{
    switch (imageType)
    {
        case ImageType.BMP:
            return "bmp";
        case ImageType.EMF:
            return "emf";
        case ImageType.JPEG:
            return "jpeg";
        case ImageType.PICT:
            return "pict";
        case ImageType.PNG:
            return "png";
        case ImageType.WMF:
            return "wmf";
        default:
            throw new Exception("Unknown image type.");
    }
}

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.

If SourceFullName is not an empty string, but ImageBytes is null, then the image is linked and not stored in the document.

Example:

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

String imageFileName = getMyDir() + "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(getMyDir() + "Image.CreateLinkedImage Out.doc");

isLink

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

isLinkOnly

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

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.


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.


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.


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.


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.


getBorders

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

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.


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.


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.


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.


getGrayScale/setGrayScale

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

The default value is false.


Method Detail

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.

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 = getMyDir() + "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(getMyDir() + "Image.CreateLinkedImage Out.doc");

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.

setImage

public void setImage(byte[] imageBytes)
             throws java.lang.Exception
Sets the image that the shape displays.
Parameters:
imageBytes - Bytes of the image.

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.


toInputStream

public java.io.InputStream toInputStream()
                         throws java.lang.Exception
Deprecated! Please use toStream() instead.

toStream

public System.IO.Stream toStream()
               throws java.lang.Exception
Creates and returns a stream that contains the image bytes.

If the image bytes are stored in the shape, creates and returns a java.io.ByteArrayInputStream object.

If the image is linked and stored in a file, opens the file and returns a java.io.FileInputStream object.

If the image is linked and stored in an external URL, opens the URL and returns a java.io.InputStream object.

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


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.

See Also:
ImageBytes

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.

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<Shape> shapes = doc.getChildNodes(NodeType.SHAPE, true, false);
    int imageIndex = 0;
    for(Shape shape :  shapes)
    {
        if (shape.hasImage())
        {
            String extension = imageTypeToExtension(shape.getImageData().getImageType());
            String imageFileName = MessageFormat.format("Image.ExportImages.{0} Out.{1}", imageIndex, extension);
            shape.getImageData().save(getMyDir() + imageFileName);
            imageIndex++;

        }
    }
}

private static String imageTypeToExtension(int imageType) throws Exception
{
    switch (imageType)
    {
        case ImageType.BMP:
            return "bmp";
        case ImageType.EMF:
            return "emf";
        case ImageType.JPEG:
            return "jpeg";
        case ImageType.PICT:
            return "pict";
        case ImageType.PNG:
            return "png";
        case ImageType.WMF:
            return "wmf";
        default:
            throw new Exception("Unknown image type.");
    }
}

getDirectBorderAttr

public java.lang.Object getDirectBorderAttr(int key)
Reserved for internal use.

fetchInheritedBorderAttr

public java.lang.Object fetchInheritedBorderAttr(int key)
                               throws java.lang.Exception
Reserved for internal use.

setBorderAttr

public void setBorderAttr(int key, java.lang.Object value)
Reserved for internal use.

See Also:
          Aspose.Words Documentation - the home page for the Aspose.Words Product Documentation.
          Aspose.Words Support Forum - our preferred method of support.