在 C++ 中管理簡報的 OLE 物件

簡介

Object Linking and Embedding (OLE) 允許 PowerPoint 投影片包含由其他應用程式建立的物件,例如來自 Excel 活頁簿的圖表。嵌入的物件將其資料儲存在簡報中。連結的物件則參考外部檔案。

PowerPoint 可以將物件顯示為圖示或顯示其內容的預覽。雙擊該物件會在相關應用程式可用時啟動編輯。 Aspose.Slides for C++ 將嵌入和連結的物件皆表示為 OleObjectFrame。

將 OLE 物件框架新增至投影片

若要將含圖表的 Excel 活頁簿嵌入,請將活頁簿的位元組及 xlsx 副檔名傳遞給 OleEmbeddedDataInfo。副檔名用來識別檔案類型,以便 PowerPoint 能選取相應的應用程式。在本範例中,AddOleObjectFrame 將活頁簿放置於第一張投影片上,並將框架大小調整為覆蓋整張投影片。

#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideSize.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <Ole/OleEmbeddedDataInfo.h>
#include <drawing/size_f.h>
#include <system/io/file.h>
#include <system/smart_ptr.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::DOM::Ole;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::IO;

auto presentation = MakeObject<Presentation>();
auto slideSize = presentation->get_SlideSize()->get_Size();
auto slide = presentation->get_Slide(0);

auto fileData = File::ReadAllBytes(u"book.xlsx");
auto dataInfo = MakeObject<OleEmbeddedDataInfo>(fileData, u"xlsx");
slide->get_Shapes()->AddOleObjectFrame(0, 0, slideSize.get_Width(), slideSize.get_Height(), dataInfo);

presentation->Save(u"output.pptx", SaveFormat::Pptx);

新增連結的 OLE 物件框架

連結版本的 AddOleObjectFrame 接受 OLE 類別識別碼以及來源檔案的路徑。此處,Excel.Sheet.12 代表 Excel 活頁簿。範例將一個 200 × 150 點的框架置於第一張投影片的上緣與左緣各 20 點處。必須確保活頁簿在儲存的路徑仍可存取,PowerPoint 才能開啟它。

#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/io/path.h>
#include <system/smart_ptr.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::IO;

auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);

auto workbookPath = Path::GetFullPath(u"book.xlsx");
slide->get_Shapes()->AddOleObjectFrame(20, 20, 200, 150, u"Excel.Sheet.12", workbookPath);

presentation->Save(u"output.pptx", SaveFormat::Pptx);

存取 OLE 物件框架

使用 get_EmbeddedData 讀取嵌入 OLE 框架中儲存的檔案。此範例檢查第一張投影片上的第一個圖形,並在有嵌入資料時印出檔案副檔名與位元組數。

#include <DOM/IOleEmbeddedDataInfo.h>
#include <DOM/IOleObjectFrame.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <iostream>
#include <system/smart_ptr.h>

using namespace Aspose::Slides;
using namespace System;

auto presentation = MakeObject<Presentation>(u"sample.pptx");
auto shapes = presentation->get_Slide(0)->get_Shapes();

auto frame = shapes->get_Count() > 0 ? AsCast<IOleObjectFrame>(shapes->idx_get(0)) : nullptr;
if (!frame || frame->get_IsObjectLink())
{
    std::cout << "The first shape is not an embedded OLE object." << std::endl;
    return;
}

auto data = frame->get_EmbeddedData();
auto bytes = data ? data->get_EmbeddedFileData() : nullptr;
if (!bytes || bytes->get_Length() == 0)
{
    std::cout << "The frame contains no embedded file data." << std::endl;
    return;
}

std::wcout << L"Embedded file extension: " << data->get_EmbeddedFileExtension() << L", size: " << bytes->get_Length() << L" bytes." << std::endl;

存取連結的 OLE 物件框架屬性

對於連結的框架,get_IsObjectLink 會回傳 true,而 get_LinkPathLong 可辨識來源檔案。此範例印出第一張投影片上第一個圖形的儲存路徑。PPT 簡報也可以在 get_LinkPathRelative 中儲存相對路徑,範例在可用時會印出該路徑。

#include <DOM/IOleObjectFrame.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <iostream>
#include <system/smart_ptr.h>
#include <system/string.h>

using namespace Aspose::Slides;
using namespace System;

auto presentation = MakeObject<Presentation>(u"sample.ppt");
auto shapes = presentation->get_Slide(0)->get_Shapes();

auto frame = shapes->get_Count() > 0 ? AsCast<IOleObjectFrame>(shapes->idx_get(0)) : nullptr;
if (!frame || !frame->get_IsObjectLink())
{
    std::cout << "The first shape is not a linked OLE object." << std::endl;
    return;
}

std::wcout << L"Linked file: " << frame->get_LinkPathLong() << std::endl;
if (!String::IsNullOrEmpty(frame->get_LinkPathRelative()))
{
    std::wcout << L"Relative path: " << frame->get_LinkPathRelative() << std::endl;
}

變更 OLE 物件資料

此範例預期在第一張投影片的第一個圖形中有嵌入的 XLSX 活頁簿。它從記憶體開啟活頁簿,將標籤與三個數值寫入第一張工作表的 E1:E4 儲存格,並以 XLSX 格式將活頁簿儲存回記憶體。SetEmbeddedData 會取代框架中存放的資料,使編輯過的儲存格在開啟嵌入活頁簿時可用。

#include "Aspose.Cells/Cell.h"
#include "Aspose.Cells/Cells.h"
#include "Aspose.Cells/Initializer.h"
#include "Aspose.Cells/OoxmlSaveOptions.h"
#include "Aspose.Cells/SaveFormat.h"
#include "Aspose.Cells/U16String.h"
#include "Aspose.Cells/Vector.h"
#include "Aspose.Cells/Workbook.h"
#include "Aspose.Cells/Worksheet.h"
#include "Aspose.Cells/WorksheetCollection.h"
#include <DOM/IOleEmbeddedDataInfo.h>
#include <DOM/IOleObjectFrame.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <Ole/OleEmbeddedDataInfo.h>
#include <iostream>
#include <system/smart_ptr.h>
#include <vector>

using namespace Aspose::Slides;
using namespace Aspose::Slides::DOM::Ole;
using namespace Aspose::Slides::Export;
using namespace System;

auto presentation = MakeObject<Presentation>(u"sample.pptx");
auto shapes = presentation->get_Slide(0)->get_Shapes();

auto frame = shapes->get_Count() > 0 ? AsCast<IOleObjectFrame>(shapes->idx_get(0)) : nullptr;
if (!frame || frame->get_IsObjectLink())
{
    std::cout << "The first shape is not an embedded OLE object." << std::endl;
    return;
}

auto data = frame->get_EmbeddedData();
auto bytes = data ? data->get_EmbeddedFileData() : nullptr;
if (!bytes || bytes->get_Length() == 0)
{
    std::cout << "The frame contains no embedded file data." << std::endl;
    return;
}

auto extension = data->get_EmbeddedFileExtension().TrimStart(u'.');
if (!extension.Equals(u"xlsx", StringComparison::OrdinalIgnoreCase))
{
    std::cout << "The embedded file is not an XLSX workbook." << std::endl;
    return;
}

Aspose::Cells::Startup();
{
    Aspose::Cells::Vector<uint8_t> input(bytes->data().data(), bytes->get_Length());
    Aspose::Cells::Workbook workbook(input);
    auto cells = workbook.GetWorksheets().Get(0).GetCells();
    cells.Get(0, 4).PutValue(Aspose::Cells::U16String("E"));
    cells.Get(1, 4).PutValue(12);
    cells.Get(2, 4).PutValue(14);
    cells.Get(3, 4).PutValue(15);

    Aspose::Cells::OoxmlSaveOptions options(Aspose::Cells::SaveFormat::Xlsx);
    auto output = workbook.Save(options);
    auto updatedBytes = MakeArray<uint8_t>(std::vector<uint8_t>(output.GetData(), output.GetData() + output.GetLength()));
    auto updatedData = MakeObject<OleEmbeddedDataInfo>(updatedBytes, u"xlsx");
    frame->SetEmbeddedData(updatedData);
}
Aspose::Cells::Cleanup();

presentation->Save(u"output.pptx", SaveFormat::Pptx);

在投影片中嵌入其他檔案類型

您也可以嵌入 HTML 文件、PDF 與 ZIP 壓縮檔。此範例使用 AddOleObjectFrame 在第一張投影片上以不同框架放置 HTML 文件與 ZIP 壓縮檔。呼叫 set_IsObjectIcon 並傳入 true,可將每個檔案顯示為圖示,讓讀者可雙擊以在相應的應用程式中開啟。

#include <DOM/IOleObjectFrame.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <Ole/OleEmbeddedDataInfo.h>
#include <system/io/file.h>
#include <system/smart_ptr.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::DOM::Ole;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::IO;

auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);

auto htmlData = File::ReadAllBytes(u"sample.html");
auto htmlDataInfo = MakeObject<OleEmbeddedDataInfo>(htmlData, u"html");
auto htmlOleFrame = slide->get_Shapes()->AddOleObjectFrame(150, 120, 50, 50, htmlDataInfo);
htmlOleFrame->set_IsObjectIcon(true);

auto zipData = File::ReadAllBytes(u"sample.zip");
auto zipDataInfo = MakeObject<OleEmbeddedDataInfo>(zipData, u"zip");
auto zipOleFrame = slide->get_Shapes()->AddOleObjectFrame(150, 220, 50, 50, zipDataInfo);
zipOleFrame->set_IsObjectIcon(true);

presentation->Save(u"output.pptx", SaveFormat::Pptx);

設定嵌入物件的檔案類型

OleEmbeddedDataInfo 中的副檔名描述了提供之位元組的格式。如果第一張投影片的第一個圖形已包含副檔名不正確的 ZIP 壓縮檔,請使用 SetEmbeddedData 來更正其類型。此範例會印出目前的副檔名,並以 zip 副檔名儲存相同的壓縮檔位元組。

#include <DOM/IOleEmbeddedDataInfo.h>
#include <DOM/IOleObjectFrame.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <Ole/OleEmbeddedDataInfo.h>
#include <iostream>
#include <system/smart_ptr.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::DOM::Ole;
using namespace Aspose::Slides::Export;
using namespace System;

auto presentation = MakeObject<Presentation>(u"sample.pptx");
auto shapes = presentation->get_Slide(0)->get_Shapes();

auto frame = shapes->get_Count() > 0 ? AsCast<IOleObjectFrame>(shapes->idx_get(0)) : nullptr;
if (!frame || frame->get_IsObjectLink())
{
    std::cout << "The first shape is not an embedded OLE object." << std::endl;
    return;
}

auto data = frame->get_EmbeddedData();
auto bytes = data ? data->get_EmbeddedFileData() : nullptr;
if (!bytes || bytes->get_Length() == 0)
{
    std::cout << "The frame contains no embedded file data." << std::endl;
    return;
}

std::wcout << L"Current embedded file extension: " << data->get_EmbeddedFileExtension() << std::endl;
auto archiveData = MakeObject<OleEmbeddedDataInfo>(bytes, u"zip");
frame->SetEmbeddedData(archiveData);

presentation->Save(u"output.pptx", SaveFormat::Pptx);

設定嵌入物件的圖示影像與標題

若要自訂嵌入物件的圖示,請將簡報影像集合中的影像指派給由 get_SubstitutePictureFormat 回傳之格式的圖片,並使用 set_SubstitutePictureTitle 設定標題。此範例為第一張投影片的第一個圖形使用 PNG 影像,並以 true 呼叫 set_IsObjectIcon 以顯示圖示及標題。

#include <DOM/IImageCollection.h>
#include <DOM/IOleObjectFrame.h>
#include <DOM/IPictureFillFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlidesPicture.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <iostream>
#include <system/io/file.h>
#include <system/smart_ptr.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::IO;

auto presentation = MakeObject<Presentation>(u"sample.pptx");
auto shapes = presentation->get_Slide(0)->get_Shapes();

auto frame = shapes->get_Count() > 0 ? AsCast<IOleObjectFrame>(shapes->idx_get(0)) : nullptr;
if (!frame || frame->get_IsObjectLink())
{
    std::cout << "The first shape is not an embedded OLE object." << std::endl;
    return;
}

auto imageBytes = File::ReadAllBytes(u"image.png");
auto iconImage = presentation->get_Images()->AddImage(imageBytes);
frame->set_IsObjectIcon(true);
frame->get_SubstitutePictureFormat()->get_Picture()->set_Image(iconImage);
frame->set_SubstitutePictureTitle(u"Embedded workbook");

presentation->Save(u"output.pptx", SaveFormat::Pptx);

防止 OLE 物件框架被調整大小或重新定位

當 PowerPoint 重新整理連結物件的預覽時,框架可能會移動或改變大小。呼叫 set_UpdateAutomatic 並傳入 false,即可要求對該連結手動更新。此範例在框架為連結 OLE 框架時,將此設定套用於第一張投影片的第一個圖形。

#include <DOM/IOleObjectFrame.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <iostream>
#include <system/smart_ptr.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;

auto presentation = MakeObject<Presentation>(u"sample.pptx");
auto shapes = presentation->get_Slide(0)->get_Shapes();

auto frame = shapes->get_Count() > 0 ? AsCast<IOleObjectFrame>(shapes->idx_get(0)) : nullptr;
if (!frame || !frame->get_IsObjectLink())
{
    std::cout << "The first shape is not a linked OLE object." << std::endl;
    return;
}

frame->set_UpdateAutomatic(false);

presentation->Save(u"output.pptx", SaveFormat::Pptx);

擷取嵌入檔案

讀取 get_EmbeddedFileData 可擷取原始位元組,並使用 get_EmbeddedFileExtension 取得輸出檔名。此範例掃描第一張投影片的頂層圖形,跳過連結物件,並將每個嵌入檔案寫入當前目錄。圖形索引用於區分具有相同副檔名的檔案。

#include <DOM/IOleEmbeddedDataInfo.h>
#include <DOM/IOleObjectFrame.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <iostream>
#include <system/io/file.h>
#include <system/io/path.h>
#include <system/smart_ptr.h>
#include <system/string.h>

using namespace Aspose::Slides;
using namespace System;
using namespace System::IO;

auto presentation = MakeObject<Presentation>(u"sample.pptx");
auto shapes = presentation->get_Slide(0)->get_Shapes();

auto extracted = 0;
for (auto index = 0; index < shapes->get_Count(); ++index)
{
    auto frame = AsCast<IOleObjectFrame>(shapes->idx_get(index));
    if (!frame || frame->get_IsObjectLink())
        continue;

    auto data = frame->get_EmbeddedData();
    auto bytes = data ? data->get_EmbeddedFileData() : nullptr;
    if (!bytes || bytes->get_Length() == 0)
    {
        std::cout << "Shape " << index << " contains no embedded file data." << std::endl;
        continue;
    }

    auto extension = data->get_EmbeddedFileExtension().TrimStart(u'.');
    auto path = Path::ChangeExtension(String::Format(u"OLE_object_{0}", index), extension);
    File::WriteAllBytes(path, bytes);
    ++extracted;
}

std::cout << "Extracted " << extracted << " embedded files." << std::endl;

列出嵌入與連結的 OLE 物件

get_ObjectProgId 方法可辨識與應用程式相關聯的 OLE 物件類型。例如,Word.Document.12 代表 Word 文件。嵌入檔案的副檔名則是另一個值。單靠 ProgID 無法確認儲存資料的格式或有效性。

此範例掃描 sample.pptx 中的每一張投影片,並依循 IGroupShape::get_Shapes 進入巢狀群組。對於每個 IOleObjectFrame,它會印出投影片編號、shape name、get_IsObjectLink 以及原始 ProgID。未知的 ProgID 會如實顯示。空的 ProgID 則使最後一欄保持空白。此範例僅讀取中繼資料,並未擷取、開啟或啟動 OLE 內容。

#include <DOM/IGroupShape.h>
#include <DOM/IOleObjectFrame.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideCollection.h>
#include <DOM/Presentation.h>
#include <iostream>
#include <system/smart_ptr.h>

using namespace Aspose::Slides;
using namespace System;

auto presentation = MakeObject<Presentation>(u"sample.pptx");

auto reportObjects = [](const auto& self, const SharedPtr<IShapeCollection>& shapes, int slideNumber) -> void
{
    for (auto index = 0; index < shapes->get_Count(); ++index)
    {
        auto shape = shapes->idx_get(index);
        if (auto frame = AsCast<IOleObjectFrame>(shape))
        {
            std::wcout << slideNumber << L" | " << shape->get_Name() << L" | " << std::boolalpha << frame->get_IsObjectLink() << L" | " << frame->get_ObjectProgId() << std::endl;
        }
        else if (auto group = AsCast<IGroupShape>(shape))
        {
            self(self, group->get_Shapes(), slideNumber);
        }
    }
};

std::wcout << L"Slide | Shape | IsObjectLink | ObjectProgId" << std::endl;
for (auto index = 0; index < presentation->get_Slides()->get_Count(); ++index)
{
    auto slide = presentation->get_Slide(index);
    reportObjects(reportObjects, slide->get_Shapes(), slide->get_SlideNumber());
}

常見問題

當我將 OLE 物件匯出為 PDF 或影像時會顯示什麼?

匯出時會顯示投影片上可見的圖示或預覽影像。若要控制其外觀,請在匯出前設定自訂預覽。若要將嵌入檔案作為 PDF 附件包含,請以 true 呼叫 PdfOptions::set_IncludeOleData。此選項預設為停用。請參閱 Preserve Embedded OLE Files as PDF Attachments。

如何防止在 PowerPoint 中意外變更 OLE 框架?

套用 shape-level locks 可限制移動或調整框架大小等操作。

為何在開啟簡報時,連結的 Excel 物件會移動或變更大小?

PowerPoint 可能會使用連結工作表範圍的尺寸來重新整理預覽。為了保持尺寸一致,請將框架調整至符合範圍,或將範圍縮放至固定框架並提供相符的預覽。請參閱 Working Solution for Worksheet Resizing。

PPTX 簡報能否為連結的 OLE 物件儲存相對路徑?

相對路徑僅在舊版 PPT 格式中支援。對於 PPTX,請使用接收者可存取的完整路徑,或將檔案嵌入簡報中。