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");
Shape shape = (Shape) doc.getChild(NodeType.SHAPE, 0, true);

// The OLE object in the first shape is a Microsoft Excel spreadsheet.
OleFormat oleFormat = shape.getOleFormat();

Assert.assertEquals("Excel.Sheet.12", oleFormat.getProgId());

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

// If we plan on saving the OLE object to a file in the local file system,
// we can use the "SuggestedExtension" property to determine which file extension to apply to the file.
Assert.assertEquals(".xlsx", oleFormat.getSuggestedExtension());

// Below are two ways of saving an OLE object to a file in the local file system.
// 1 -  Save it via a stream:
OutputStream fs = new FileOutputStream(getArtifactsDir() + "OLE spreadsheet extracted via stream" + oleFormat.getSuggestedExtension());
try {
    oleFormat.save(fs);
} finally {
    if (fs != null) fs.close();
}

// 2 -  Save it directly to a filename:
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");
        Shape shape = (Shape) doc.getChild(NodeType.SHAPE, 0, true);
        
        // The OLE object in the first shape is a Microsoft Excel spreadsheet.
        OleFormat oleFormat = shape.getOleFormat();
        
        Assert.assertEquals("Excel.Sheet.12", oleFormat.getProgId());
        
        // Our object is neither auto updating nor locked from updates.
        Assert.assertFalse(oleFormat.getAutoUpdate());
        Assert.assertEquals(oleFormat.isLocked(), false);
        
        // If we plan on saving the OLE object to a file in the local file system,
        // we can use the "SuggestedExtension" property to determine which file extension to apply to the file.
        Assert.assertEquals(".xlsx", oleFormat.getSuggestedExtension());
        
        // Below are two ways of saving an OLE object to a file in the local file system.
        // 1 -  Save it via a stream:
        OutputStream fs = new FileOutputStream(getArtifactsDir() + "OLE spreadsheet extracted via stream" + oleFormat.getSuggestedExtension());
        try {
            oleFormat.save(fs);
        } finally {
            if (fs != null) fs.close();
        }
        
        // 2 -  Save it directly to a filename:
        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.
        Document doc = new Document(getMyDir() + "OLE ActiveX controls.docm");
        
        // Shapes store and display OLE objects in the document's body.
        Shape shape = (Shape) doc.getChild(NodeType.SHAPE, 0, true);
        
        Assert.assertEquals("6e182020-f460-11ce-9bcd-00aa00608e01", shape.getOleFormat().getClsid().toString());
        
        Forms2OleControl oleControl = (Forms2OleControl) shape.getOleFormat().getOleControl();
        
        // Some OLE controls may contain child controls, such as the one in this document with three options buttons.
        Forms2OleControlCollection oleControlCollection = oleControl.getChildNodes();
        
        Assert.assertEquals(3, oleControlCollection.getCount());
        
        Assert.assertEquals("C#", oleControlCollection.get(0).getCaption());
        Assert.assertEquals("1", oleControlCollection.get(0).getValue());
        
        Assert.assertEquals("Visual Basic", oleControlCollection.get(1).getCaption());
        Assert.assertEquals("0", oleControlCollection.get(1).getValue());
        
        Assert.assertEquals("Delphi", oleControlCollection.get(2).getCaption());
        Assert.assertEquals("0", oleControlCollection.get(2).getValue());
      • 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 into the document as an OLE object.
        builder.insertOleObject(getImageDir() + "Microsoft Visio drawing.vsd", "Package", false, false, new FileInputStream(getImageDir() + "Transparent background logo.png"));
        
        // 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() + "Transparent background logo.png"));
        
        // Inserting OLE objects creates shapes that store these objects.
        List<Shape> shapeList = Arrays.stream(doc.getChildNodes(NodeType.SHAPE, true).toArray())
                .filter(Shape.class::isInstance)
                .map(Shape.class::cast)
                .collect(Collectors.toList());
        
        Assert.assertEquals(2, shapeList.size());
        Assert.assertEquals(2, IterableUtils.countMatches(shapeList, s -> s.getShapeType() == ShapeType.OLE_OBJECT));
        
        // If a shape contains an OLE object, it will have a valid "OleFormat" property,
        // which we can use to verify some aspects of the shape.
        OleFormat oleFormat = shapeList.get(0).getOleFormat();
        
        Assert.assertEquals(false, oleFormat.isLink());
        Assert.assertEquals(false, oleFormat.getOleIcon());
        
        oleFormat = shapeList.get(1).getOleFormat();
        
        Assert.assertEquals(true, oleFormat.isLink());
        Assert.assertEquals(true, oleFormat.getOleIcon());
        
        Assert.assertTrue(oleFormat.getSourceFullName().endsWith("Images" + File.separator + "Microsoft Visio drawing.vsd"));
        Assert.assertEquals("", oleFormat.getSourceItem());
        
        Assert.assertEquals("Microsoft Visio drawing.vsd", oleFormat.getIconCaption());
        
        doc.save(getArtifactsDir() + "Shape.OleLinks.docx");
        
        // If the object contains OLE data, we can access it using a stream.
        byte[] oleEntryBytes = oleFormat.getOleEntry("\u0001CompObj");
        Assert.assertEquals(76, oleEntryBytes.length);
      • 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 into the document as an OLE object.
        builder.insertOleObject(getImageDir() + "Microsoft Visio drawing.vsd", "Package", false, false, new FileInputStream(getImageDir() + "Transparent background logo.png"));
        
        // 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() + "Transparent background logo.png"));
        
        // Inserting OLE objects creates shapes that store these objects.
        List<Shape> shapeList = Arrays.stream(doc.getChildNodes(NodeType.SHAPE, true).toArray())
                .filter(Shape.class::isInstance)
                .map(Shape.class::cast)
                .collect(Collectors.toList());
        
        Assert.assertEquals(2, shapeList.size());
        Assert.assertEquals(2, IterableUtils.countMatches(shapeList, s -> s.getShapeType() == ShapeType.OLE_OBJECT));
        
        // If a shape contains an OLE object, it will have a valid "OleFormat" property,
        // which we can use to verify some aspects of the shape.
        OleFormat oleFormat = shapeList.get(0).getOleFormat();
        
        Assert.assertEquals(false, oleFormat.isLink());
        Assert.assertEquals(false, oleFormat.getOleIcon());
        
        oleFormat = shapeList.get(1).getOleFormat();
        
        Assert.assertEquals(true, oleFormat.isLink());
        Assert.assertEquals(true, oleFormat.getOleIcon());
        
        Assert.assertTrue(oleFormat.getSourceFullName().endsWith("Images" + File.separator + "Microsoft Visio drawing.vsd"));
        Assert.assertEquals("", oleFormat.getSourceItem());
        
        Assert.assertEquals("Microsoft Visio drawing.vsd", oleFormat.getIconCaption());
        
        doc.save(getArtifactsDir() + "Shape.OleLinks.docx");
        
        // If the object contains OLE data, we can access it using a stream.
        byte[] oleEntryBytes = oleFormat.getOleEntry("\u0001CompObj");
        Assert.assertEquals(76, oleEntryBytes.length);
      • 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");
        Shape shape = (Shape) doc.getChild(NodeType.SHAPE, 0, true);
        
        // The OLE object in the first shape is a Microsoft Excel spreadsheet.
        OleFormat oleFormat = shape.getOleFormat();
        
        Assert.assertEquals("Excel.Sheet.12", oleFormat.getProgId());
        
        // Our object is neither auto updating nor locked from updates.
        Assert.assertFalse(oleFormat.getAutoUpdate());
        Assert.assertEquals(oleFormat.isLocked(), false);
        
        // If we plan on saving the OLE object to a file in the local file system,
        // we can use the "SuggestedExtension" property to determine which file extension to apply to the file.
        Assert.assertEquals(".xlsx", oleFormat.getSuggestedExtension());
        
        // Below are two ways of saving an OLE object to a file in the local file system.
        // 1 -  Save it via a stream:
        OutputStream fs = new FileOutputStream(getArtifactsDir() + "OLE spreadsheet extracted via stream" + oleFormat.getSuggestedExtension());
        try {
            oleFormat.save(fs);
        } finally {
            if (fs != null) fs.close();
        }
        
        // 2 -  Save it directly to a filename:
        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 verify the properties of an ActiveX control.
        Document doc = new Document(getMyDir() + "ActiveX controls.docx");
        
        Shape shape = (Shape) doc.getChild(NodeType.SHAPE, 0, true);
        OleControl oleControl = shape.getOleFormat().getOleControl();
        
        Assert.assertEquals(oleControl.getName(), null);
        
        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 into the document as an OLE object.
        builder.insertOleObject(getImageDir() + "Microsoft Visio drawing.vsd", "Package", false, false, new FileInputStream(getImageDir() + "Transparent background logo.png"));
        
        // 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() + "Transparent background logo.png"));
        
        // Inserting OLE objects creates shapes that store these objects.
        List<Shape> shapeList = Arrays.stream(doc.getChildNodes(NodeType.SHAPE, true).toArray())
                .filter(Shape.class::isInstance)
                .map(Shape.class::cast)
                .collect(Collectors.toList());
        
        Assert.assertEquals(2, shapeList.size());
        Assert.assertEquals(2, IterableUtils.countMatches(shapeList, s -> s.getShapeType() == ShapeType.OLE_OBJECT));
        
        // If a shape contains an OLE object, it will have a valid "OleFormat" property,
        // which we can use to verify some aspects of the shape.
        OleFormat oleFormat = shapeList.get(0).getOleFormat();
        
        Assert.assertEquals(false, oleFormat.isLink());
        Assert.assertEquals(false, oleFormat.getOleIcon());
        
        oleFormat = shapeList.get(1).getOleFormat();
        
        Assert.assertEquals(true, oleFormat.isLink());
        Assert.assertEquals(true, oleFormat.getOleIcon());
        
        Assert.assertTrue(oleFormat.getSourceFullName().endsWith("Images" + File.separator + "Microsoft Visio drawing.vsd"));
        Assert.assertEquals("", oleFormat.getSourceItem());
        
        Assert.assertEquals("Microsoft Visio drawing.vsd", oleFormat.getIconCaption());
        
        doc.save(getArtifactsDir() + "Shape.OleLinks.docx");
        
        // If the object contains OLE data, we can access it using a stream.
        byte[] oleEntryBytes = oleFormat.getOleEntry("\u0001CompObj");
        Assert.assertEquals(76, oleEntryBytes.length);
      • 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 an OLE object into a document.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        // OLE objects allow us to open other files in the local file system using another installed application
        // in our operating system by double-clicking on the shape that contains the OLE object in the document body.
        // In this case, our external file will be a ZIP archive.
        byte[] zipFileBytes = DocumentHelper.getBytesFromStream(new FileInputStream(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("Package file name.zip");
            setOlePackage.setDisplayName("Package display name.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");
        Shape shape = (Shape) doc.getChild(NodeType.SHAPE, 0, true);
        
        // The OLE object in the first shape is a Microsoft Excel spreadsheet.
        OleFormat oleFormat = shape.getOleFormat();
        
        Assert.assertEquals("Excel.Sheet.12", oleFormat.getProgId());
        
        // Our object is neither auto updating nor locked from updates.
        Assert.assertFalse(oleFormat.getAutoUpdate());
        Assert.assertEquals(oleFormat.isLocked(), false);
        
        // If we plan on saving the OLE object to a file in the local file system,
        // we can use the "SuggestedExtension" property to determine which file extension to apply to the file.
        Assert.assertEquals(".xlsx", oleFormat.getSuggestedExtension());
        
        // Below are two ways of saving an OLE object to a file in the local file system.
        // 1 -  Save it via a stream:
        OutputStream fs = new FileOutputStream(getArtifactsDir() + "OLE spreadsheet extracted via stream" + oleFormat.getSuggestedExtension());
        try {
            oleFormat.save(fs);
        } finally {
            if (fs != null) fs.close();
        }
        
        // 2 -  Save it directly to a filename:
        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 into the document as an OLE object.
        builder.insertOleObject(getImageDir() + "Microsoft Visio drawing.vsd", "Package", false, false, new FileInputStream(getImageDir() + "Transparent background logo.png"));
        
        // 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() + "Transparent background logo.png"));
        
        // Inserting OLE objects creates shapes that store these objects.
        List<Shape> shapeList = Arrays.stream(doc.getChildNodes(NodeType.SHAPE, true).toArray())
                .filter(Shape.class::isInstance)
                .map(Shape.class::cast)
                .collect(Collectors.toList());
        
        Assert.assertEquals(2, shapeList.size());
        Assert.assertEquals(2, IterableUtils.countMatches(shapeList, s -> s.getShapeType() == ShapeType.OLE_OBJECT));
        
        // If a shape contains an OLE object, it will have a valid "OleFormat" property,
        // which we can use to verify some aspects of the shape.
        OleFormat oleFormat = shapeList.get(0).getOleFormat();
        
        Assert.assertEquals(false, oleFormat.isLink());
        Assert.assertEquals(false, oleFormat.getOleIcon());
        
        oleFormat = shapeList.get(1).getOleFormat();
        
        Assert.assertEquals(true, oleFormat.isLink());
        Assert.assertEquals(true, oleFormat.getOleIcon());
        
        Assert.assertTrue(oleFormat.getSourceFullName().endsWith("Images" + File.separator + "Microsoft Visio drawing.vsd"));
        Assert.assertEquals("", oleFormat.getSourceItem());
        
        Assert.assertEquals("Microsoft Visio drawing.vsd", oleFormat.getIconCaption());
        
        doc.save(getArtifactsDir() + "Shape.OleLinks.docx");
        
        // If the object contains OLE data, we can access it using a stream.
        byte[] oleEntryBytes = oleFormat.getOleEntry("\u0001CompObj");
        Assert.assertEquals(76, oleEntryBytes.length);
      • 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 into the document as an OLE object.
        builder.insertOleObject(getImageDir() + "Microsoft Visio drawing.vsd", "Package", false, false, new FileInputStream(getImageDir() + "Transparent background logo.png"));
        
        // 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() + "Transparent background logo.png"));
        
        // Inserting OLE objects creates shapes that store these objects.
        List<Shape> shapeList = Arrays.stream(doc.getChildNodes(NodeType.SHAPE, true).toArray())
                .filter(Shape.class::isInstance)
                .map(Shape.class::cast)
                .collect(Collectors.toList());
        
        Assert.assertEquals(2, shapeList.size());
        Assert.assertEquals(2, IterableUtils.countMatches(shapeList, s -> s.getShapeType() == ShapeType.OLE_OBJECT));
        
        // If a shape contains an OLE object, it will have a valid "OleFormat" property,
        // which we can use to verify some aspects of the shape.
        OleFormat oleFormat = shapeList.get(0).getOleFormat();
        
        Assert.assertEquals(false, oleFormat.isLink());
        Assert.assertEquals(false, oleFormat.getOleIcon());
        
        oleFormat = shapeList.get(1).getOleFormat();
        
        Assert.assertEquals(true, oleFormat.isLink());
        Assert.assertEquals(true, oleFormat.getOleIcon());
        
        Assert.assertTrue(oleFormat.getSourceFullName().endsWith("Images" + File.separator + "Microsoft Visio drawing.vsd"));
        Assert.assertEquals("", oleFormat.getSourceItem());
        
        Assert.assertEquals("Microsoft Visio drawing.vsd", oleFormat.getIconCaption());
        
        doc.save(getArtifactsDir() + "Shape.OleLinks.docx");
        
        // If the object contains OLE data, we can access it using a stream.
        byte[] oleEntryBytes = oleFormat.getOleEntry("\u0001CompObj");
        Assert.assertEquals(76, oleEntryBytes.length);
      • 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");
        Shape shape = (Shape) doc.getChild(NodeType.SHAPE, 0, true);
        
        // The OLE object in the first shape is a Microsoft Excel spreadsheet.
        OleFormat oleFormat = shape.getOleFormat();
        
        Assert.assertEquals("Excel.Sheet.12", oleFormat.getProgId());
        
        // Our object is neither auto updating nor locked from updates.
        Assert.assertFalse(oleFormat.getAutoUpdate());
        Assert.assertEquals(oleFormat.isLocked(), false);
        
        // If we plan on saving the OLE object to a file in the local file system,
        // we can use the "SuggestedExtension" property to determine which file extension to apply to the file.
        Assert.assertEquals(".xlsx", oleFormat.getSuggestedExtension());
        
        // Below are two ways of saving an OLE object to a file in the local file system.
        // 1 -  Save it via a stream:
        OutputStream fs = new FileOutputStream(getArtifactsDir() + "OLE spreadsheet extracted via stream" + oleFormat.getSuggestedExtension());
        try {
            oleFormat.save(fs);
        } finally {
            if (fs != null) fs.close();
        }
        
        // 2 -  Save it directly to a filename:
        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 an OLE object's suggested file name.
        Document doc = new Document(getMyDir() + "OLE shape.rtf");
        
        Shape oleShape = (Shape) doc.getFirstSection().getBody().getChild(NodeType.SHAPE, 0, true);
        
        // OLE objects can provide a suggested filename and extension,
        // which we can use when saving the object's contents into a file in the local file system.
        String suggestedFileName = oleShape.getOleFormat().getSuggestedFileName();
        
        Assert.assertEquals("CSV.csv", suggestedFileName);
        
        OutputStream fileStream = new FileOutputStream(getArtifactsDir() + suggestedFileName);
        try {
            oleShape.getOleFormat().save(fileStream);
        } finally {
            if (fileStream != null) fileStream.close();
        }
    • 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 into the document as an OLE object.
        builder.insertOleObject(getImageDir() + "Microsoft Visio drawing.vsd", "Package", false, false, new FileInputStream(getImageDir() + "Transparent background logo.png"));
        
        // 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() + "Transparent background logo.png"));
        
        // Inserting OLE objects creates shapes that store these objects.
        List<Shape> shapeList = Arrays.stream(doc.getChildNodes(NodeType.SHAPE, true).toArray())
                .filter(Shape.class::isInstance)
                .map(Shape.class::cast)
                .collect(Collectors.toList());
        
        Assert.assertEquals(2, shapeList.size());
        Assert.assertEquals(2, IterableUtils.countMatches(shapeList, s -> s.getShapeType() == ShapeType.OLE_OBJECT));
        
        // If a shape contains an OLE object, it will have a valid "OleFormat" property,
        // which we can use to verify some aspects of the shape.
        OleFormat oleFormat = shapeList.get(0).getOleFormat();
        
        Assert.assertEquals(false, oleFormat.isLink());
        Assert.assertEquals(false, oleFormat.getOleIcon());
        
        oleFormat = shapeList.get(1).getOleFormat();
        
        Assert.assertEquals(true, oleFormat.isLink());
        Assert.assertEquals(true, oleFormat.getOleIcon());
        
        Assert.assertTrue(oleFormat.getSourceFullName().endsWith("Images" + File.separator + "Microsoft Visio drawing.vsd"));
        Assert.assertEquals("", oleFormat.getSourceItem());
        
        Assert.assertEquals("Microsoft Visio drawing.vsd", oleFormat.getIconCaption());
        
        doc.save(getArtifactsDir() + "Shape.OleLinks.docx");
        
        // If the object contains OLE data, we can access it using a stream.
        byte[] oleEntryBytes = oleFormat.getOleEntry("\u0001CompObj");
        Assert.assertEquals(76, oleEntryBytes.length);
      • getRawData

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

        Example:

        Shows how to access the raw data of an embedded OLE object.
        Document doc = new Document(getMyDir() + "OLE objects.docx");
        
        for (Node shape : (Iterable<Node>) doc.getChildNodes(NodeType.SHAPE, true)) {
            OleFormat oleFormat = ((Shape) shape).getOleFormat();
            if (oleFormat != null) {
                System.out.println("This is {(oleFormat.IsLink ? ");
                byte[] oleRawData = oleFormat.getRawData();
        
                Assert.assertEquals(24576, oleRawData.length);
            }
        }
      • 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");
        Shape shape = (Shape) doc.getChild(NodeType.SHAPE, 0, true);
        
        // The OLE object in the first shape is a Microsoft Excel spreadsheet.
        OleFormat oleFormat = shape.getOleFormat();
        
        Assert.assertEquals("Excel.Sheet.12", oleFormat.getProgId());
        
        // Our object is neither auto updating nor locked from updates.
        Assert.assertFalse(oleFormat.getAutoUpdate());
        Assert.assertEquals(oleFormat.isLocked(), false);
        
        // If we plan on saving the OLE object to a file in the local file system,
        // we can use the "SuggestedExtension" property to determine which file extension to apply to the file.
        Assert.assertEquals(".xlsx", oleFormat.getSuggestedExtension());
        
        // Below are two ways of saving an OLE object to a file in the local file system.
        // 1 -  Save it via a stream:
        OutputStream fs = new FileOutputStream(getArtifactsDir() + "OLE spreadsheet extracted via stream" + oleFormat.getSuggestedExtension());
        try {
            oleFormat.save(fs);
        } finally {
            if (fs != null) fs.close();
        }
        
        // 2 -  Save it directly to a filename:
        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");
        Shape shape = (Shape) doc.getChild(NodeType.SHAPE, 0, true);
        
        // The OLE object in the first shape is a Microsoft Excel spreadsheet.
        OleFormat oleFormat = shape.getOleFormat();
        
        Assert.assertEquals("Excel.Sheet.12", oleFormat.getProgId());
        
        // Our object is neither auto updating nor locked from updates.
        Assert.assertFalse(oleFormat.getAutoUpdate());
        Assert.assertEquals(oleFormat.isLocked(), false);
        
        // If we plan on saving the OLE object to a file in the local file system,
        // we can use the "SuggestedExtension" property to determine which file extension to apply to the file.
        Assert.assertEquals(".xlsx", oleFormat.getSuggestedExtension());
        
        // Below are two ways of saving an OLE object to a file in the local file system.
        // 1 -  Save it via a stream:
        OutputStream fs = new FileOutputStream(getArtifactsDir() + "OLE spreadsheet extracted via stream" + oleFormat.getSuggestedExtension());
        try {
            oleFormat.save(fs);
        } finally {
            if (fs != null) fs.close();
        }
        
        // 2 -  Save it directly to a filename:
        oleFormat.save(getArtifactsDir() + "OLE spreadsheet saved directly" + oleFormat.getSuggestedExtension());