com.aspose.words
Class DrawingMLImageData

java.lang.Object
    extended by com.aspose.words.DrawingMLImageData
All Implemented Interfaces:
IImageData

public class DrawingMLImageData 
extends java.lang.Object

Defines an image for a DrawingML picture.

Use the DrawingML.ImageData property to access and modify the image inside a DrawingML picture. You do not create instances of the DrawingMLImageData 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 = java.text.MessageFormat.format(
                    "Image.ExportImages.{0} Out{1}", imageIndex, FileFormatUtil.imageTypeToExtension(shape.getImageData().getImageType()));
            shape.getImageData().save(getMyDir() + imageFileName);
            imageIndex++;
        }
    }

    // Newer Microsoft Word documents (such as DOCX) may contain a different type of image container called DrawingML.
    // Repeat the process to extract these if they are present in the loaded document.
    NodeCollection dmlShapes = doc.getChildNodes(NodeType.DRAWING_ML, true);
    for (DrawingML dml : (Iterable<DrawingML>) dmlShapes)
    {
        if (dml.hasImage())
        {
            String imageFileName = java.text.MessageFormat.format(
                    "Image.ExportImages.{0} Out{1}", imageIndex, FileFormatUtil.imageTypeToExtension(dml.getImageData().getImageType()));
            dml.getImageData().save(getMyDir() + imageFileName);
            imageIndex++;
        }
    }
}

Property Getters/Setters Summary
booleanhasImage()
           Returns true if the shape has image bytes or links an image.
byte[]getImageBytes()
voidsetImageBytes(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 DrawingML picture (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.
 
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 DrawingML picture displays.
voidsetImage(java.lang.String fileName)
           Sets the image that the DrawingML picture 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 DrawingML picture as a java BufferedImage object.
 

Property Getters/Setters Detail

hasImage

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

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).

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.


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 = java.text.MessageFormat.format(
                    "Image.ExportImages.{0} Out{1}", imageIndex, FileFormatUtil.imageTypeToExtension(shape.getImageData().getImageType()));
            shape.getImageData().save(getMyDir() + imageFileName);
            imageIndex++;
        }
    }

    // Newer Microsoft Word documents (such as DOCX) may contain a different type of image container called DrawingML.
    // Repeat the process to extract these if they are present in the loaded document.
    NodeCollection dmlShapes = doc.getChildNodes(NodeType.DRAWING_ML, true);
    for (DrawingML dml : (Iterable<DrawingML>) dmlShapes)
    {
        if (dml.hasImage())
        {
            String imageFileName = java.text.MessageFormat.format(
                    "Image.ExportImages.{0} Out{1}", imageIndex, FileFormatUtil.imageTypeToExtension(dml.getImageData().getImageType()));
            dml.getImageData().save(getMyDir() + imageFileName);
            imageIndex++;
        }
    }
}

isLink

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

isLinkOnly

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

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.


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.

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 = java.text.MessageFormat.format(
                    "Image.ExportImages.{0} Out{1}", imageIndex, FileFormatUtil.imageTypeToExtension(shape.getImageData().getImageType()));
            shape.getImageData().save(getMyDir() + imageFileName);
            imageIndex++;
        }
    }

    // Newer Microsoft Word documents (such as DOCX) may contain a different type of image container called DrawingML.
    // Repeat the process to extract these if they are present in the loaded document.
    NodeCollection dmlShapes = doc.getChildNodes(NodeType.DRAWING_ML, true);
    for (DrawingML dml : (Iterable<DrawingML>) dmlShapes)
    {
        if (dml.hasImage())
        {
            String imageFileName = java.text.MessageFormat.format(
                    "Image.ExportImages.{0} Out{1}", imageIndex, FileFormatUtil.imageTypeToExtension(dml.getImageData().getImageType()));
            dml.getImageData().save(getMyDir() + 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.

setImage

public void setImage(java.io.InputStream stream)
             throws java.lang.Exception
Sets the image that the DrawingML picture 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(java.lang.String fileName)
             throws java.lang.Exception
Sets the image that the DrawingML picture displays.
Parameters:
fileName - The image file. Can be a file name or a URL.

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

toImage

public java.awt.image.BufferedImage toImage()
                     throws java.lang.Exception
Gets the image stored in the DrawingML picture 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:

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