在 Android 上管理簡報中的 OLE 物件
簡介
Object Linking and Embedding (OLE) 允許 PowerPoint 投影片包含在其他應用程式中建立的物件,例如來自 Excel 活頁簿的圖表。嵌入的物件會將其資料儲存在簡報中。連結的物件則指向外部檔案。
PowerPoint 可以將物件顯示為圖示或顯示其內容的預覽。當相關應用程式可用時,雙擊該物件會啟動編輯。Aspose.Slides for Android via Java 會將嵌入與連結的物件皆表示為一個 OleObjectFrame。
將 OLE 物件框架新增至投影片
要將含圖表的 Excel 活頁簿嵌入,請將活頁簿的位元組與 xlsx 副檔名傳遞給 OleEmbeddedDataInfo。副檔名用來辨識檔案類型,使 PowerPoint 能選取相應的應用程式。在此範例中,addOleObjectFrame 會將活頁簿放置於第一張投影片,並將框架大小調整至覆蓋整張投影片。
import com.aspose.slides.*;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import com.aspose.slides.android.SizeF;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
SizeF slideSize = presentation.getSlideSize().getSize();
File bookFile = new File("book.xlsx");
byte[] bytes = new byte[(int) bookFile.length()];
try (DataInputStream input = new DataInputStream(new FileInputStream(bookFile))) {
input.readFully(bytes);
}
OleEmbeddedDataInfo data = new OleEmbeddedDataInfo(bytes, "xlsx");
slide.getShapes().addOleObjectFrame(0, 0, slideSize.getWidth(), slideSize.getHeight(), data);
presentation.save("output.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
新增連結的 OLE 物件框架
addOleObjectFrame 的連結重載接受 OLE 類別識別碼與來源檔案路徑。此處 Excel.Sheet.12 代表 Excel 活頁簿。範例在第一張投影片的左上角 20 點處放置一個 200 × 150 點的框架。活頁簿必須保持在儲存的路徑可存取,PowerPoint 才能開啟它。
import com.aspose.slides.*;
import java.io.File;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
String workbookPath = new File("book.xlsx").getCanonicalPath();
slide.getShapes().addOleObjectFrame(20, 20, 200, 150, "Excel.Sheet.12", workbookPath);
presentation.save("output.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
存取 OLE 物件框架
使用 getEmbeddedData 讀取嵌入 OLE 框架中儲存的檔案。此範例檢查第一張投影片的第一個圖形,當有嵌入資料時會印出檔案副檔名與位元組數。
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample.pptx");
try {
IShapeCollection shapes = presentation.getSlides().get_Item(0).getShapes();
if (shapes.size() == 0 || !(shapes.get_Item(0) instanceof IOleObjectFrame)) {
System.out.println("The first shape is not an embedded OLE object.");
return;
}
IOleObjectFrame frame = (IOleObjectFrame) shapes.get_Item(0);
if (frame.isObjectLink()) {
System.out.println("The first shape is not an embedded OLE object.");
return;
}
IOleEmbeddedDataInfo data = frame.getEmbeddedData();
byte[] bytes = data == null ? null : data.getEmbeddedFileData();
if (bytes == null || bytes.length == 0) {
System.out.println("The frame contains no embedded file data.");
return;
}
System.out.printf("Embedded file extension: %s, size: %d bytes.%n", data.getEmbeddedFileExtension(), bytes.length);
} finally {
presentation.dispose();
}
存取連結 OLE 物件框架屬性
對於連結框架,isObjectLink 會回傳 true,而 getLinkPathLong 會指出來源檔案。範例會印出第一張投影片第一個圖形的儲存路徑。PPT 簡報也可以儲存相對路徑,範例會使用 getLinkPathRelative 讀取並在可用時印出。
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample.ppt");
try {
IShapeCollection shapes = presentation.getSlides().get_Item(0).getShapes();
if (shapes.size() == 0 || !(shapes.get_Item(0) instanceof IOleObjectFrame)) {
System.out.println("The first shape is not a linked OLE object.");
return;
}
IOleObjectFrame frame = (IOleObjectFrame) shapes.get_Item(0);
if (!frame.isObjectLink()) {
System.out.println("The first shape is not a linked OLE object.");
return;
}
System.out.println("Linked file: " + frame.getLinkPathLong());
String relativePath = frame.getLinkPathRelative();
if (relativePath != null && !relativePath.isEmpty()) {
System.out.println("Relative path: " + relativePath);
}
} finally {
presentation.dispose();
}
變更 OLE 物件資料
Note
此範例使用 Aspose.Cells for Android via Java 編輯嵌入的活頁簿。此範例預期第一張投影片第一個圖形中有一個嵌入的 XLSX 活頁簿。它會從記憶體開啟活頁簿,在第一個工作表的 E1:E4 儲存格寫入標籤與三個值,然後以 XLSX 格式將活頁簿儲存回記憶體。使用 setEmbeddedData 取代框架中儲存的資料,使編輯過的儲存格在開啟嵌入的活頁簿時可使用。
import com.aspose.slides.*;
import com.aspose.cells.Cells;
import com.aspose.cells.Workbook;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import com.aspose.cells.FileFormatType;
Presentation presentation = new Presentation("sample.pptx");
try {
IShapeCollection shapes = presentation.getSlides().get_Item(0).getShapes();
if (shapes.size() == 0 || !(shapes.get_Item(0) instanceof IOleObjectFrame)) {
System.out.println("The first shape is not an embedded OLE object.");
return;
}
IOleObjectFrame frame = (IOleObjectFrame) shapes.get_Item(0);
if (frame.isObjectLink()) {
System.out.println("The first shape is not an embedded OLE object.");
return;
}
IOleEmbeddedDataInfo data = frame.getEmbeddedData();
byte[] bytes = data == null ? null : data.getEmbeddedFileData();
if (bytes == null || bytes.length == 0) {
System.out.println("The frame contains no embedded file data.");
return;
}
String extension = data.getEmbeddedFileExtension();
if (!"xlsx".equalsIgnoreCase(extension) && !".xlsx".equalsIgnoreCase(extension)) {
System.out.println("The embedded file is not an XLSX workbook.");
return;
}
ByteArrayInputStream input = new ByteArrayInputStream(bytes);
Workbook workbook = new Workbook(input);
try {
Cells cells = workbook.getWorksheets().get(0).getCells();
cells.get("E1").putValue("E");
cells.get("E2").putValue(12);
cells.get("E3").putValue(14);
cells.get("E4").putValue(15);
ByteArrayOutputStream output = new ByteArrayOutputStream();
workbook.save(output, FileFormatType.XLSX);
byte[] updatedBytes = output.toByteArray();
OleEmbeddedDataInfo updatedData = new OleEmbeddedDataInfo(updatedBytes, "xlsx");
frame.setEmbeddedData(updatedData);
} finally {
workbook.dispose();
}
presentation.save("output.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
在投影片中嵌入其他檔案類型
您也可以嵌入 HTML 文件、PDF 以及 ZIP 壓縮檔。此範例使用 addOleObjectFrame 將 HTML 文件與 ZIP 壓縮檔分別放置於第一張投影片的不同框架中。呼叫 setObjectIcon 並傳入 true,會將每個檔案顯示為圖示,讀者可雙擊圖示以在相應的應用程式中開啟。
import com.aspose.slides.*;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
File htmlFile = new File("sample.html");
byte[] htmlBytes = new byte[(int) htmlFile.length()];
try (DataInputStream input = new DataInputStream(new FileInputStream(htmlFile))) {
input.readFully(htmlBytes);
}
OleEmbeddedDataInfo htmlData = new OleEmbeddedDataInfo(htmlBytes, "html");
IOleObjectFrame htmlFrame = slide.getShapes().addOleObjectFrame(150, 120, 50, 50, htmlData);
htmlFrame.setObjectIcon(true);
File zipFile = new File("sample.zip");
byte[] zipBytes = new byte[(int) zipFile.length()];
try (DataInputStream input = new DataInputStream(new FileInputStream(zipFile))) {
input.readFully(zipBytes);
}
OleEmbeddedDataInfo zipData = new OleEmbeddedDataInfo(zipBytes, "zip");
IOleObjectFrame zipFrame = slide.getShapes().addOleObjectFrame(150, 220, 50, 50, zipData);
zipFrame.setObjectIcon(true);
presentation.save("output.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
設定嵌入物件的檔案類型
在 OleEmbeddedDataInfo 中的副檔名說明所提供位元組的格式。如果第一張投影片的第一個圖形已經包含副檔名不正確的 ZIP 壓縮檔,請使用 setEmbeddedData 來修正其類型。範例會印出目前的副檔名,並以 zip 副檔名儲存相同的壓縮檔位元組。
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample.pptx");
try {
IShapeCollection shapes = presentation.getSlides().get_Item(0).getShapes();
if (shapes.size() == 0 || !(shapes.get_Item(0) instanceof IOleObjectFrame)) {
System.out.println("The first shape is not an embedded OLE object.");
return;
}
IOleObjectFrame frame = (IOleObjectFrame) shapes.get_Item(0);
if (frame.isObjectLink()) {
System.out.println("The first shape is not an embedded OLE object.");
return;
}
IOleEmbeddedDataInfo data = frame.getEmbeddedData();
byte[] bytes = data == null ? null : data.getEmbeddedFileData();
if (bytes == null || bytes.length == 0) {
System.out.println("The frame contains no embedded file data.");
return;
}
System.out.println("Current embedded file extension: " + data.getEmbeddedFileExtension());
OleEmbeddedDataInfo archiveData = new OleEmbeddedDataInfo(bytes, "zip");
frame.setEmbeddedData(archiveData);
presentation.save("output.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
設定嵌入物件的圖示影像與標題
若要自訂嵌入物件的圖示,請透過 getSubstitutePictureFormat 取得的圖片格式,從簡報的圖片集合中指派影像,並使用 setSubstitutePictureTitle 設定說明文字。範例對第一張投影片的第一個圖形使用 PNG 影像,並以 true 呼叫 setObjectIcon 以顯示圖示與說明文字。
import com.aspose.slides.*;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
Presentation presentation = new Presentation("sample.pptx");
try {
IShapeCollection shapes = presentation.getSlides().get_Item(0).getShapes();
if (shapes.size() == 0 || !(shapes.get_Item(0) instanceof IOleObjectFrame)) {
System.out.println("The first shape is not an embedded OLE object.");
return;
}
IOleObjectFrame frame = (IOleObjectFrame) shapes.get_Item(0);
if (frame.isObjectLink()) {
System.out.println("The first shape is not an embedded OLE object.");
return;
}
File imageFile = new File("image.png");
byte[] imageBytes = new byte[(int) imageFile.length()];
try (DataInputStream input = new DataInputStream(new FileInputStream(imageFile))) {
input.readFully(imageBytes);
}
IPPImage iconImage = presentation.getImages().addImage(imageBytes);
frame.setObjectIcon(true);
frame.getSubstitutePictureFormat().getPicture().setImage(iconImage);
frame.setSubstitutePictureTitle("Embedded workbook");
presentation.save("output.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
防止 OLE 物件框架被重新調整大小或重新定位
當 PowerPoint 重新整理連結物件的預覽時,框架可能會移動或改變大小。將 setUpdateAutomatic 設為 false,即可要求對該連結手動更新。範例對第一張投影片的第一個圖形(若為連結 OLE 框架)套用此設定。
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample.pptx");
try {
IShapeCollection shapes = presentation.getSlides().get_Item(0).getShapes();
if (shapes.size() == 0 || !(shapes.get_Item(0) instanceof IOleObjectFrame)) {
System.out.println("The first shape is not a linked OLE object.");
return;
}
IOleObjectFrame frame = (IOleObjectFrame) shapes.get_Item(0);
if (!frame.isObjectLink()) {
System.out.println("The first shape is not a linked OLE object.");
return;
}
frame.setUpdateAutomatic(false);
presentation.save("output.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
擷取嵌入檔案
讀取 getEmbeddedFileData 以擷取原始位元組,並使用 getEmbeddedFileExtension 取得輸出檔名。此範例會掃描第一張投影片的頂層圖形,跳過連結物件,並將每個嵌入檔案寫入當前目錄。圖形索引用來區分具有相同副檔名的檔案。
import com.aspose.slides.*;
import java.io.FileOutputStream;
Presentation presentation = new Presentation("sample.pptx");
try {
IShapeCollection shapes = presentation.getSlides().get_Item(0).getShapes();
int extracted = 0;
for (int index = 0; index < shapes.size(); index++) {
IShape shape = shapes.get_Item(index);
if (!(shape instanceof IOleObjectFrame)) {
continue;
}
IOleObjectFrame frame = (IOleObjectFrame) shape;
if (frame.isObjectLink()) {
continue;
}
IOleEmbeddedDataInfo data = frame.getEmbeddedData();
byte[] bytes = data == null ? null : data.getEmbeddedFileData();
if (bytes == null || bytes.length == 0) {
System.out.printf("Shape %d contains no embedded file data.%n", index);
continue;
}
String extension = data.getEmbeddedFileExtension();
String suffix = extension == null ? "" : extension.replaceFirst("^\\.+", "");
String fileName = "OLE_object_" + index + (suffix.isEmpty() ? "" : "." + suffix);
try (FileOutputStream output = new FileOutputStream(fileName)) {
output.write(bytes);
}
extracted++;
}
System.out.printf("Extracted %d embedded files.%n", extracted);
} finally {
presentation.dispose();
}
列出嵌入和連結的 OLE 物件
getObjectProgId 方法可辨識與應用程式關聯的 OLE 物件類型。例如,Word.Document.12 代表 Word 文件。嵌入的檔案副檔名則是另一個值。僅憑 ProgID 無法確認儲存資料的格式或有效性。
範例會掃描 sample.pptx 中的每張投影片,並透過 IGroupShape.getShapes 進入巢狀群組。對於每個 IOleObjectFrame,它會印出投影片編號、shape name、isObjectLink 與原始 ProgID。未認識的 ProgID 會如實顯示。若 ProgID 為空,最後一欄會留白。此範例僅讀取中繼資料,並不會提取、開啟或啟動 OLE 內容。
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample.pptx");
try {
class ObjectReporter {
void reportObjects(IShapeCollection shapes, int slideNumber) {
for (IShape shape : shapes) {
if (shape instanceof IOleObjectFrame) {
IOleObjectFrame frame = (IOleObjectFrame) shape;
String progId = frame.getObjectProgId();
System.out.printf("%d | %s | %b | %s%n", slideNumber, shape.getName(), frame.isObjectLink(), progId == null ? "" : progId);
} else if (shape instanceof IGroupShape) {
IGroupShape group = (IGroupShape) shape;
reportObjects(group.getShapes(), slideNumber);
}
}
}
}
System.out.println("Slide | Shape | IsObjectLink | ObjectProgId");
ObjectReporter reporter = new ObjectReporter();
for (ISlide slide : presentation.getSlides()) {
reporter.reportObjects(slide.getShapes(), slide.getSlideNumber());
}
} finally {
presentation.dispose();
}
FAQ
匯出 OLE 物件為 PDF 或影像時會出現什麼?
匯出會顯示投影片上可見的圖示或預覽影像。若要控制其外觀,請在匯出前設定自訂預覽。若要將嵌入檔案作為 PDF 附件包含,請將 PdfOptions.setIncludeOleData 設為 true。此選項預設為停用。請參閱 Preserve Embedded OLE Files as PDF Attachments。
如何防止在 PowerPoint 中意外更改 OLE 框架?
套用 shape-level locks 以限制移動或調整大小等動作。
為什麼在開啟簡報時連結的 Excel 物件會移動或改變大小?
PowerPoint 可能會使用連結工作表範圍的尺寸重新整理預覽。為了保持尺寸一致,請將框架調整至匹配範圍,或將範圍縮放至固定框架,並提供相符的預覽。請參閱 Working Solution for Worksheet Resizing。
PPTX 簡報可以為連結的 OLE 物件儲存相對路徑嗎?
相對路徑僅在較舊的 PPT 格式中可用。對於 PPTX,請使用收件者可存取的完整路徑,或將檔案嵌入簡報中。