java.lang.Objectcom.aspose.words.OleFormat
public class OleFormat
Use the Example:
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());
Property Getters/Setters Summary | ||
---|---|---|
boolean | getAutoUpdate() | |
void | setAutoUpdate(boolean value) | |
Specifies whether the link to the OLE object is automatically updated or not in Microsoft Word. | ||
java.util.UUID | getClsid() | |
Gets the CLSID of the OLE object. | ||
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. |
||
boolean | isLink() | |
Returns true if the OLE object is linked (when |
||
boolean | isLocked() | |
void | isLocked(boolean value) | |
Specifies whether the link to the OLE object is locked from updates. | ||
OleControl | getOleControl() | |
Gets |
||
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. | ||
OlePackage | getOlePackage() | |
Provide access to |
||
java.lang.String | getProgId() | |
void | setProgId(java.lang.String value) | |
Gets or sets the ProgID of the OLE object. | ||
java.lang.String | getSourceFullName() | |
void | setSourceFullName(java.lang.String value) | |
Gets or sets the path and name of the source file for the linked OLE object. | ||
java.lang.String | getSourceItem() | |
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. | ||
java.lang.String | getSuggestedExtension() | |
Gets the file extension suggested for the current embedded object if you want to save it into a file. | ||
java.lang.String | getSuggestedFileName() | |
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. | ||
void | save(java.io.OutputStream stream) | |
Saves the data of the embedded object into the specified stream. | ||
void | save(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) |
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() |
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() |
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() |
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) |
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() |
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() |
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() |
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) |
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) |
The default value is an empty string.
If
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) |
The default value is an empty string.
For example, if the source file is a Microsoft Excel workbook, the
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() |
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() |
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) |
oleEntryName
- Case-sensitive name of the OLE data stream.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 |
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 |
It is the responsibility of the caller to dispose the stream.
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 |
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());