com.aspose.words

Class OleFormat

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

Provides access to the data of an OLE object or ActiveX control.

Use the Shape.OleFormat property to access the data of an OLE object. You do not create instances of the OleFormat class directly.

Example:

Shows how to extract embedded OLE objects into files.
Document doc = new Document(getMyDir() + "OLE spreadsheet.docm");

// The first shape will contain an OLE object
Shape shape = (Shape) doc.getChild(NodeType.SHAPE, 0, true);

// This object is a Microsoft Excel spreadsheet
OleFormat oleFormat = shape.getOleFormat();
Assert.assertEquals(oleFormat.getProgId(), "Excel.Sheet.12");

// Our object is neither auto updating nor locked from updates
Assert.assertFalse(oleFormat.getAutoUpdate());
Assert.assertEquals(oleFormat.isLocked(), false);

// If we want to extract the OLE object by saving it into our local file system, this property can tell us the relevant file extension
Assert.assertEquals(oleFormat.getSuggestedExtension(), ".xlsx");

// We can save it via a stream
OutputStream stream = new FileOutputStream(getArtifactsDir() + "OLE spreadsheet extracted via stream" + oleFormat.getSuggestedExtension());
try {
    oleFormat.save(stream);
} finally {
    if (stream != null) stream.close();
}

// We can also save it directly to a file
oleFormat.save(getArtifactsDir() + "OLE spreadsheet saved directly" + oleFormat.getSuggestedExtension());
See Also:
Shape.OleFormat

Property Getters/Setters Summary
booleangetAutoUpdate()
void
setAutoUpdate(booleanvalue)
           Specifies whether the link to the OLE object is automatically updated or not in Microsoft Word.
java.util.UUIDgetClsid()
Gets the CLSID of the OLE object.
java.lang.StringgetIconCaption()
Gets icon caption of OLE object.

In case of OLE object is not embedded as icon or caption couldn't be retrieved returns empty string.

booleanisLink()
Returns true if the OLE object is linked (when SourceFullName is specified).
booleanisLocked()
void
isLocked(booleanvalue)
           Specifies whether the link to the OLE object is locked from updates.
OleControlgetOleControl()
Gets OleControl objects if this OLE object is an ActiveX control. Otherwise this property is null.
booleangetOleIcon()
Gets the draw aspect of the OLE object. When true, the OLE object is displayed as an icon. When false, the OLE object is displayed as content.
OlePackagegetOlePackage()
Provide access to OlePackage if OLE object is an OLE Package. Returns null otherwise.
java.lang.StringgetProgId()
void
setProgId(java.lang.Stringvalue)
           Gets or sets the ProgID of the OLE object.
java.lang.StringgetSourceFullName()
void
setSourceFullName(java.lang.Stringvalue)
           Gets or sets the path and name of the source file for the linked OLE object.
java.lang.StringgetSourceItem()
void
setSourceItem(java.lang.Stringvalue)
           Gets or sets a string that is used to identify the portion of the source file that is being linked.
java.lang.StringgetSuggestedExtension()
Gets the file extension suggested for the current embedded object if you want to save it into a file.
java.lang.StringgetSuggestedFileName()
Gets the file name suggested for the current embedded object if you want to save it into a file.
 
Method Summary
byte[]getOleEntry(java.lang.String oleEntryName)
Gets OLE object data entry.
byte[]getRawData()
Gets OLE object raw data.
voidsave(java.io.OutputStream stream)
Saves the data of the embedded object into the specified stream.
voidsave(java.lang.String fileName)
Saves the data of the embedded object into a file with the specified name.
 

    • Property Getters/Setters Detail

      • getAutoUpdate/setAutoUpdate

        public boolean getAutoUpdate() / public void setAutoUpdate(boolean value)
        
        Specifies whether the link to the OLE object is automatically updated or not in Microsoft Word.

        The default value is false.

        Example:

        Shows how to extract embedded OLE objects into files.
        Document doc = new Document(getMyDir() + "OLE spreadsheet.docm");
        
        // The first shape will contain an OLE object
        Shape shape = (Shape) doc.getChild(NodeType.SHAPE, 0, true);
        
        // This object is a Microsoft Excel spreadsheet
        OleFormat oleFormat = shape.getOleFormat();
        Assert.assertEquals(oleFormat.getProgId(), "Excel.Sheet.12");
        
        // Our object is neither auto updating nor locked from updates
        Assert.assertFalse(oleFormat.getAutoUpdate());
        Assert.assertEquals(oleFormat.isLocked(), false);
        
        // If we want to extract the OLE object by saving it into our local file system, this property can tell us the relevant file extension
        Assert.assertEquals(oleFormat.getSuggestedExtension(), ".xlsx");
        
        // We can save it via a stream
        OutputStream stream = new FileOutputStream(getArtifactsDir() + "OLE spreadsheet extracted via stream" + oleFormat.getSuggestedExtension());
        try {
            oleFormat.save(stream);
        } finally {
            if (stream != null) stream.close();
        }
        
        // We can also save it directly to a file
        oleFormat.save(getArtifactsDir() + "OLE spreadsheet saved directly" + oleFormat.getSuggestedExtension());
      • getClsid

        public java.util.UUID getClsid()
        
        Gets the CLSID of the OLE object.

        Example:

        Shows how to access an OLE control embedded in a document and its child controls.
        // Open a document that contains a Microsoft Forms OLE control with child controls
        Document doc = new Document(getMyDir() + "OLE ActiveX controls.docm");
        
        // Get the shape that contains the control
        Shape shape = (Shape) doc.getChild(NodeType.SHAPE, 0, true);
        
        Assert.assertEquals(shape.getOleFormat().getClsid().toString(), "6e182020-f460-11ce-9bcd-00aa00608e01");
        
        Forms2OleControl oleControl = (Forms2OleControl) shape.getOleFormat().getOleControl();
        
        // Some controls contain child controls
        Forms2OleControlCollection oleControlCollection = oleControl.getChildNodes();
        
        // In this case, the child controls are 3 option buttons
        Assert.assertEquals(oleControlCollection.getCount(), 3);
        
        Assert.assertEquals(oleControlCollection.get(0).getCaption(), "C#");
        Assert.assertEquals(oleControlCollection.get(0).getValue(), "1");
        
        Assert.assertEquals(oleControlCollection.get(1).getCaption(), "Visual Basic");
        Assert.assertEquals(oleControlCollection.get(1).getValue(), "0");
        
        Assert.assertEquals(oleControlCollection.get(2).getCaption(), "Delphi");
        Assert.assertEquals(oleControlCollection.get(2).getValue(), "0");
      • getIconCaption

        public java.lang.String getIconCaption()
        
        Gets icon caption of OLE object.

        In case of OLE object is not embedded as icon or caption couldn't be retrieved returns empty string.

        Example:

        Shows how to insert linked and unlinked OLE objects.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        // Embed a Microsoft Visio drawing as an OLE object into the document
        builder.insertOleObject(getImageDir() + "Microsoft Visio drawing.vsd", "Package", false, false, new FileInputStream(getImageDir() + "Logo.jpg"));
        
        // Insert a link to the file in the local file system and display it as an icon
        builder.insertOleObject(getImageDir() + "Microsoft Visio drawing.vsd", "Package", true, true, new FileInputStream(getImageDir() + "Logo.jpg"));
        
        // Both the OLE objects are stored within shapes
        NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true);
        Assert.assertEquals(shapes.getCount(), 2);
        
        // If the shape is an OLE object, it will have a valid OleFormat property
        // We can use it check if it is linked or displayed as an icon, among other things
        Shape firstShape = (Shape) shapes.get(0);
        OleFormat oleFormat = firstShape.getOleFormat();
        Assert.assertEquals(oleFormat.isLink(), false);
        Assert.assertEquals(oleFormat.getOleIcon(), false);
        
        Shape secondShape = (Shape) shapes.get(1);
        oleFormat = secondShape.getOleFormat();
        Assert.assertEquals(oleFormat.isLink(), true);
        Assert.assertEquals(oleFormat.getOleIcon(), true);
        
        // Get the name or the source file and verify that the whole file is linked
        Assert.assertTrue(oleFormat.getSourceFullName().endsWith("Images" + File.separatorChar + "Microsoft Visio drawing.vsd"));
        Assert.assertEquals(oleFormat.getSourceItem(), "");
        Assert.assertEquals(oleFormat.getIconCaption(), "Microsoft Visio drawing.vsd");
        
        doc.save(getArtifactsDir() + "Shape.OleLinks.docx");
        
        // We can get a stream with the OLE data entry, if the object has this
        byte[] oleEntryBytes = oleFormat.getOleEntry("\u0001CompObj");
        Assert.assertEquals(oleEntryBytes.length, 76);
      • isLink

        public boolean isLink()
        
        Returns true if the OLE object is linked (when SourceFullName is specified).

        Example:

        Shows how to insert linked and unlinked OLE objects.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        // Embed a Microsoft Visio drawing as an OLE object into the document
        builder.insertOleObject(getImageDir() + "Microsoft Visio drawing.vsd", "Package", false, false, new FileInputStream(getImageDir() + "Logo.jpg"));
        
        // Insert a link to the file in the local file system and display it as an icon
        builder.insertOleObject(getImageDir() + "Microsoft Visio drawing.vsd", "Package", true, true, new FileInputStream(getImageDir() + "Logo.jpg"));
        
        // Both the OLE objects are stored within shapes
        NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true);
        Assert.assertEquals(shapes.getCount(), 2);
        
        // If the shape is an OLE object, it will have a valid OleFormat property
        // We can use it check if it is linked or displayed as an icon, among other things
        Shape firstShape = (Shape) shapes.get(0);
        OleFormat oleFormat = firstShape.getOleFormat();
        Assert.assertEquals(oleFormat.isLink(), false);
        Assert.assertEquals(oleFormat.getOleIcon(), false);
        
        Shape secondShape = (Shape) shapes.get(1);
        oleFormat = secondShape.getOleFormat();
        Assert.assertEquals(oleFormat.isLink(), true);
        Assert.assertEquals(oleFormat.getOleIcon(), true);
        
        // Get the name or the source file and verify that the whole file is linked
        Assert.assertTrue(oleFormat.getSourceFullName().endsWith("Images" + File.separatorChar + "Microsoft Visio drawing.vsd"));
        Assert.assertEquals(oleFormat.getSourceItem(), "");
        Assert.assertEquals(oleFormat.getIconCaption(), "Microsoft Visio drawing.vsd");
        
        doc.save(getArtifactsDir() + "Shape.OleLinks.docx");
        
        // We can get a stream with the OLE data entry, if the object has this
        byte[] oleEntryBytes = oleFormat.getOleEntry("\u0001CompObj");
        Assert.assertEquals(oleEntryBytes.length, 76);
      • isLocked/isLocked

        public boolean isLocked() / public void isLocked(boolean value)
        
        Specifies whether the link to the OLE object is locked from updates.

        The default value is false.

        Example:

        Shows how to extract embedded OLE objects into files.
        Document doc = new Document(getMyDir() + "OLE spreadsheet.docm");
        
        // The first shape will contain an OLE object
        Shape shape = (Shape) doc.getChild(NodeType.SHAPE, 0, true);
        
        // This object is a Microsoft Excel spreadsheet
        OleFormat oleFormat = shape.getOleFormat();
        Assert.assertEquals(oleFormat.getProgId(), "Excel.Sheet.12");
        
        // Our object is neither auto updating nor locked from updates
        Assert.assertFalse(oleFormat.getAutoUpdate());
        Assert.assertEquals(oleFormat.isLocked(), false);
        
        // If we want to extract the OLE object by saving it into our local file system, this property can tell us the relevant file extension
        Assert.assertEquals(oleFormat.getSuggestedExtension(), ".xlsx");
        
        // We can save it via a stream
        OutputStream stream = new FileOutputStream(getArtifactsDir() + "OLE spreadsheet extracted via stream" + oleFormat.getSuggestedExtension());
        try {
            oleFormat.save(stream);
        } finally {
            if (stream != null) stream.close();
        }
        
        // We can also save it directly to a file
        oleFormat.save(getArtifactsDir() + "OLE spreadsheet saved directly" + oleFormat.getSuggestedExtension());
      • getOleControl

        public OleControl getOleControl()
        
        Gets OleControl objects if this OLE object is an ActiveX control. Otherwise this property is null.

        Example:

        Shows how to get ActiveX control and properties from the document.
        Document doc = new Document(getMyDir() + "ActiveX controls.docx");
        
        // Get ActiveX control from the document
        Shape shape = (Shape) doc.getChild(NodeType.SHAPE, 0, true);
        OleControl oleControl = shape.getOleFormat().getOleControl();
        
        Assert.assertEquals(oleControl.getName(), null);
        
        // Get ActiveX control properties
        if (oleControl.isForms2OleControl()) {
            Forms2OleControl checkBox = (Forms2OleControl) oleControl;
            Assert.assertEquals(checkBox.getCaption(), "Первый");
            Assert.assertEquals(checkBox.getValue(), "0");
            Assert.assertEquals(checkBox.getEnabled(), true);
            Assert.assertEquals(checkBox.getType(), Forms2OleControlType.CHECK_BOX);
            Assert.assertEquals(checkBox.getChildNodes(), null);
        }
      • getOleIcon

        public boolean getOleIcon()
        
        Gets the draw aspect of the OLE object. When true, the OLE object is displayed as an icon. When false, the OLE object is displayed as content.

        Aspose.Words does not allow to set this property to avoid confusion. If you were able to change the draw aspect in Aspose.Words, Microsoft Word would still display the OLE object in its original draw aspect until you edit or update the OLE object in Microsoft Word.

        Example:

        Shows how to insert linked and unlinked OLE objects.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        // Embed a Microsoft Visio drawing as an OLE object into the document
        builder.insertOleObject(getImageDir() + "Microsoft Visio drawing.vsd", "Package", false, false, new FileInputStream(getImageDir() + "Logo.jpg"));
        
        // Insert a link to the file in the local file system and display it as an icon
        builder.insertOleObject(getImageDir() + "Microsoft Visio drawing.vsd", "Package", true, true, new FileInputStream(getImageDir() + "Logo.jpg"));
        
        // Both the OLE objects are stored within shapes
        NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true);
        Assert.assertEquals(shapes.getCount(), 2);
        
        // If the shape is an OLE object, it will have a valid OleFormat property
        // We can use it check if it is linked or displayed as an icon, among other things
        Shape firstShape = (Shape) shapes.get(0);
        OleFormat oleFormat = firstShape.getOleFormat();
        Assert.assertEquals(oleFormat.isLink(), false);
        Assert.assertEquals(oleFormat.getOleIcon(), false);
        
        Shape secondShape = (Shape) shapes.get(1);
        oleFormat = secondShape.getOleFormat();
        Assert.assertEquals(oleFormat.isLink(), true);
        Assert.assertEquals(oleFormat.getOleIcon(), true);
        
        // Get the name or the source file and verify that the whole file is linked
        Assert.assertTrue(oleFormat.getSourceFullName().endsWith("Images" + File.separatorChar + "Microsoft Visio drawing.vsd"));
        Assert.assertEquals(oleFormat.getSourceItem(), "");
        Assert.assertEquals(oleFormat.getIconCaption(), "Microsoft Visio drawing.vsd");
        
        doc.save(getArtifactsDir() + "Shape.OleLinks.docx");
        
        // We can get a stream with the OLE data entry, if the object has this
        byte[] oleEntryBytes = oleFormat.getOleEntry("\u0001CompObj");
        Assert.assertEquals(oleEntryBytes.length, 76);
      • getOlePackage

        public OlePackage getOlePackage()
        
        Provide access to OlePackage if OLE object is an OLE Package. Returns null otherwise. OLE Package is a legacy technology that allows to wrap any file format not present in the OLE registry of a Windows system into a generic package allowing to embed almost anything into a document. See OlePackage type for more info.

        Example:

        Shows how insert ole object as ole package and set it file name and display name.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        byte[] zipFileBytes = Files.readAllBytes(Paths.get(getDatabaseDir() + "cat001.zip"));
        
        InputStream stream = new ByteArrayInputStream(zipFileBytes);
        InputStream representingImage = new FileInputStream(getImageDir() + "Logo.jpg");
        try {
            Shape shape = builder.insertOleObject(stream, "Package", true, representingImage);
        
            OlePackage setOlePackage = shape.getOleFormat().getOlePackage();
            setOlePackage.setFileName("Cat FileName.zip");
            setOlePackage.setDisplayName("Cat DisplayName.zip");
        
            doc.save(getArtifactsDir() + "Shape.InsertOlePackage.docx");
        } finally {
            if (stream != null) {
                stream.close();
            }
        }
      • getProgId/setProgId

        public java.lang.String getProgId() / public void setProgId(java.lang.String value)
        
        Gets or sets the ProgID of the OLE object.

        The ProgID property is not always present in Microsoft Word documents and cannot be relied upon.

        Cannot be null.

        The default value is an empty string.

        Example:

        Shows how to extract embedded OLE objects into files.
        Document doc = new Document(getMyDir() + "OLE spreadsheet.docm");
        
        // The first shape will contain an OLE object
        Shape shape = (Shape) doc.getChild(NodeType.SHAPE, 0, true);
        
        // This object is a Microsoft Excel spreadsheet
        OleFormat oleFormat = shape.getOleFormat();
        Assert.assertEquals(oleFormat.getProgId(), "Excel.Sheet.12");
        
        // Our object is neither auto updating nor locked from updates
        Assert.assertFalse(oleFormat.getAutoUpdate());
        Assert.assertEquals(oleFormat.isLocked(), false);
        
        // If we want to extract the OLE object by saving it into our local file system, this property can tell us the relevant file extension
        Assert.assertEquals(oleFormat.getSuggestedExtension(), ".xlsx");
        
        // We can save it via a stream
        OutputStream stream = new FileOutputStream(getArtifactsDir() + "OLE spreadsheet extracted via stream" + oleFormat.getSuggestedExtension());
        try {
            oleFormat.save(stream);
        } finally {
            if (stream != null) stream.close();
        }
        
        // We can also save it directly to a file
        oleFormat.save(getArtifactsDir() + "OLE spreadsheet saved directly" + oleFormat.getSuggestedExtension());
      • 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 OLE object.

        The default value is an empty string.

        If SourceFullName is not an empty string, the OLE object is linked.

        Example:

        Shows how to insert linked and unlinked OLE objects.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        // Embed a Microsoft Visio drawing as an OLE object into the document
        builder.insertOleObject(getImageDir() + "Microsoft Visio drawing.vsd", "Package", false, false, new FileInputStream(getImageDir() + "Logo.jpg"));
        
        // Insert a link to the file in the local file system and display it as an icon
        builder.insertOleObject(getImageDir() + "Microsoft Visio drawing.vsd", "Package", true, true, new FileInputStream(getImageDir() + "Logo.jpg"));
        
        // Both the OLE objects are stored within shapes
        NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true);
        Assert.assertEquals(shapes.getCount(), 2);
        
        // If the shape is an OLE object, it will have a valid OleFormat property
        // We can use it check if it is linked or displayed as an icon, among other things
        Shape firstShape = (Shape) shapes.get(0);
        OleFormat oleFormat = firstShape.getOleFormat();
        Assert.assertEquals(oleFormat.isLink(), false);
        Assert.assertEquals(oleFormat.getOleIcon(), false);
        
        Shape secondShape = (Shape) shapes.get(1);
        oleFormat = secondShape.getOleFormat();
        Assert.assertEquals(oleFormat.isLink(), true);
        Assert.assertEquals(oleFormat.getOleIcon(), true);
        
        // Get the name or the source file and verify that the whole file is linked
        Assert.assertTrue(oleFormat.getSourceFullName().endsWith("Images" + File.separatorChar + "Microsoft Visio drawing.vsd"));
        Assert.assertEquals(oleFormat.getSourceItem(), "");
        Assert.assertEquals(oleFormat.getIconCaption(), "Microsoft Visio drawing.vsd");
        
        doc.save(getArtifactsDir() + "Shape.OleLinks.docx");
        
        // We can get a stream with the OLE data entry, if the object has this
        byte[] oleEntryBytes = oleFormat.getOleEntry("\u0001CompObj");
        Assert.assertEquals(oleEntryBytes.length, 76);
      • getSourceItem/setSourceItem

        public java.lang.String getSourceItem() / public void setSourceItem(java.lang.String value)
        
        Gets or sets a string that is used to identify the portion of the source file that is being linked.

        The default value is an empty string.

        For example, if the source file is a Microsoft Excel workbook, the SourceItem property might return "Workbook1!R3C1:R4C2" if the OLE object contains only a few cells from the worksheet.

        Example:

        Shows how to insert linked and unlinked OLE objects.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        // Embed a Microsoft Visio drawing as an OLE object into the document
        builder.insertOleObject(getImageDir() + "Microsoft Visio drawing.vsd", "Package", false, false, new FileInputStream(getImageDir() + "Logo.jpg"));
        
        // Insert a link to the file in the local file system and display it as an icon
        builder.insertOleObject(getImageDir() + "Microsoft Visio drawing.vsd", "Package", true, true, new FileInputStream(getImageDir() + "Logo.jpg"));
        
        // Both the OLE objects are stored within shapes
        NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true);
        Assert.assertEquals(shapes.getCount(), 2);
        
        // If the shape is an OLE object, it will have a valid OleFormat property
        // We can use it check if it is linked or displayed as an icon, among other things
        Shape firstShape = (Shape) shapes.get(0);
        OleFormat oleFormat = firstShape.getOleFormat();
        Assert.assertEquals(oleFormat.isLink(), false);
        Assert.assertEquals(oleFormat.getOleIcon(), false);
        
        Shape secondShape = (Shape) shapes.get(1);
        oleFormat = secondShape.getOleFormat();
        Assert.assertEquals(oleFormat.isLink(), true);
        Assert.assertEquals(oleFormat.getOleIcon(), true);
        
        // Get the name or the source file and verify that the whole file is linked
        Assert.assertTrue(oleFormat.getSourceFullName().endsWith("Images" + File.separatorChar + "Microsoft Visio drawing.vsd"));
        Assert.assertEquals(oleFormat.getSourceItem(), "");
        Assert.assertEquals(oleFormat.getIconCaption(), "Microsoft Visio drawing.vsd");
        
        doc.save(getArtifactsDir() + "Shape.OleLinks.docx");
        
        // We can get a stream with the OLE data entry, if the object has this
        byte[] oleEntryBytes = oleFormat.getOleEntry("\u0001CompObj");
        Assert.assertEquals(oleEntryBytes.length, 76);
      • getSuggestedExtension

        public java.lang.String getSuggestedExtension()
        
        Gets the file extension suggested for the current embedded object if you want to save it into a file.

        Example:

        Shows how to extract embedded OLE objects into files.
        Document doc = new Document(getMyDir() + "OLE spreadsheet.docm");
        
        // The first shape will contain an OLE object
        Shape shape = (Shape) doc.getChild(NodeType.SHAPE, 0, true);
        
        // This object is a Microsoft Excel spreadsheet
        OleFormat oleFormat = shape.getOleFormat();
        Assert.assertEquals(oleFormat.getProgId(), "Excel.Sheet.12");
        
        // Our object is neither auto updating nor locked from updates
        Assert.assertFalse(oleFormat.getAutoUpdate());
        Assert.assertEquals(oleFormat.isLocked(), false);
        
        // If we want to extract the OLE object by saving it into our local file system, this property can tell us the relevant file extension
        Assert.assertEquals(oleFormat.getSuggestedExtension(), ".xlsx");
        
        // We can save it via a stream
        OutputStream stream = new FileOutputStream(getArtifactsDir() + "OLE spreadsheet extracted via stream" + oleFormat.getSuggestedExtension());
        try {
            oleFormat.save(stream);
        } finally {
            if (stream != null) stream.close();
        }
        
        // We can also save it directly to a file
        oleFormat.save(getArtifactsDir() + "OLE spreadsheet saved directly" + oleFormat.getSuggestedExtension());
      • getSuggestedFileName

        public java.lang.String getSuggestedFileName()
        
        Gets the file name suggested for the current embedded object if you want to save it into a file.

        Example:

        Shows how to get suggested file name from the object.
        Document doc = new Document(getMyDir() + "OLE shape.rtf");
        
        // Gets the file name suggested for the current embedded object if you want to save it into a file
        Shape oleShape = (Shape) doc.getFirstSection().getBody().getChild(NodeType.SHAPE, 0, true);
        String suggestedFileName = oleShape.getOleFormat().getSuggestedFileName();
        
        Assert.assertEquals(suggestedFileName, "CSV.csv");
    • Method Detail

      • getOleEntry

        public byte[] getOleEntry(java.lang.String oleEntryName)
        Gets OLE object data entry.
        Parameters:
        oleEntryName - Case-sensitive name of the OLE data stream.
        Returns:
        An OLE data byte array or null.

        Example:

        Shows how to insert linked and unlinked OLE objects.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        // Embed a Microsoft Visio drawing as an OLE object into the document
        builder.insertOleObject(getImageDir() + "Microsoft Visio drawing.vsd", "Package", false, false, new FileInputStream(getImageDir() + "Logo.jpg"));
        
        // Insert a link to the file in the local file system and display it as an icon
        builder.insertOleObject(getImageDir() + "Microsoft Visio drawing.vsd", "Package", true, true, new FileInputStream(getImageDir() + "Logo.jpg"));
        
        // Both the OLE objects are stored within shapes
        NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true);
        Assert.assertEquals(shapes.getCount(), 2);
        
        // If the shape is an OLE object, it will have a valid OleFormat property
        // We can use it check if it is linked or displayed as an icon, among other things
        Shape firstShape = (Shape) shapes.get(0);
        OleFormat oleFormat = firstShape.getOleFormat();
        Assert.assertEquals(oleFormat.isLink(), false);
        Assert.assertEquals(oleFormat.getOleIcon(), false);
        
        Shape secondShape = (Shape) shapes.get(1);
        oleFormat = secondShape.getOleFormat();
        Assert.assertEquals(oleFormat.isLink(), true);
        Assert.assertEquals(oleFormat.getOleIcon(), true);
        
        // Get the name or the source file and verify that the whole file is linked
        Assert.assertTrue(oleFormat.getSourceFullName().endsWith("Images" + File.separatorChar + "Microsoft Visio drawing.vsd"));
        Assert.assertEquals(oleFormat.getSourceItem(), "");
        Assert.assertEquals(oleFormat.getIconCaption(), "Microsoft Visio drawing.vsd");
        
        doc.save(getArtifactsDir() + "Shape.OleLinks.docx");
        
        // We can get a stream with the OLE data entry, if the object has this
        byte[] oleEntryBytes = oleFormat.getOleEntry("\u0001CompObj");
        Assert.assertEquals(oleEntryBytes.length, 76);
      • getRawData

        public byte[] getRawData()
                         throws java.lang.Exception
        Gets OLE object raw data.

        Example:

        Shows how to get access to OLE object raw data.
        // Open a document that contains OLE objects
        Document doc = new Document(getMyDir() + "OLE objects.docx");
        
        for (Node shape : (Iterable<Node>) doc.getChildNodes(NodeType.SHAPE, true)) {
            // Get access to OLE data
            OleFormat oleFormat = ((Shape) shape).getOleFormat();
            if (oleFormat != null) {
                System.out.println("This is {(oleFormat.IsLink ? ");
                byte[] oleRawData = oleFormat.getRawData();
            }
        }
      • save

        public void save(java.io.OutputStream stream)
                 throws java.lang.Exception
        Saves the data of the embedded object into the specified stream.

        It is the responsibility of the caller to dispose the stream.

        Parameters:
        stream - Where to save the object data.

        Example:

        Shows how to extract embedded OLE objects into files.
        Document doc = new Document(getMyDir() + "OLE spreadsheet.docm");
        
        // The first shape will contain an OLE object
        Shape shape = (Shape) doc.getChild(NodeType.SHAPE, 0, true);
        
        // This object is a Microsoft Excel spreadsheet
        OleFormat oleFormat = shape.getOleFormat();
        Assert.assertEquals(oleFormat.getProgId(), "Excel.Sheet.12");
        
        // Our object is neither auto updating nor locked from updates
        Assert.assertFalse(oleFormat.getAutoUpdate());
        Assert.assertEquals(oleFormat.isLocked(), false);
        
        // If we want to extract the OLE object by saving it into our local file system, this property can tell us the relevant file extension
        Assert.assertEquals(oleFormat.getSuggestedExtension(), ".xlsx");
        
        // We can save it via a stream
        OutputStream stream = new FileOutputStream(getArtifactsDir() + "OLE spreadsheet extracted via stream" + oleFormat.getSuggestedExtension());
        try {
            oleFormat.save(stream);
        } finally {
            if (stream != null) stream.close();
        }
        
        // We can also save it directly to a file
        oleFormat.save(getArtifactsDir() + "OLE spreadsheet saved directly" + oleFormat.getSuggestedExtension());
      • save

        public void save(java.lang.String fileName)
                 throws java.lang.Exception
        Saves the data of the embedded object into a file with the specified name.
        Parameters:
        fileName - Name of the file to save the OLE object data.

        Example:

        Shows how to extract embedded OLE objects into files.
        Document doc = new Document(getMyDir() + "OLE spreadsheet.docm");
        
        // The first shape will contain an OLE object
        Shape shape = (Shape) doc.getChild(NodeType.SHAPE, 0, true);
        
        // This object is a Microsoft Excel spreadsheet
        OleFormat oleFormat = shape.getOleFormat();
        Assert.assertEquals(oleFormat.getProgId(), "Excel.Sheet.12");
        
        // Our object is neither auto updating nor locked from updates
        Assert.assertFalse(oleFormat.getAutoUpdate());
        Assert.assertEquals(oleFormat.isLocked(), false);
        
        // If we want to extract the OLE object by saving it into our local file system, this property can tell us the relevant file extension
        Assert.assertEquals(oleFormat.getSuggestedExtension(), ".xlsx");
        
        // We can save it via a stream
        OutputStream stream = new FileOutputStream(getArtifactsDir() + "OLE spreadsheet extracted via stream" + oleFormat.getSuggestedExtension());
        try {
            oleFormat.save(stream);
        } finally {
            if (stream != null) stream.close();
        }
        
        // We can also save it directly to a file
        oleFormat.save(getArtifactsDir() + "OLE spreadsheet saved directly" + oleFormat.getSuggestedExtension());