OLEオブジェクトの操作

OLE(Object Linking and Embedding)は、ユーザーがサードパーティのアプリケーションによって作成または編集された"オブジェクト"を含む文書を操作できる技術です。 つまり、OLEでは、編集アプリケーションがこれらの"オブジェクト"を別の編集アプリケーションにエクスポートし、追加のコンテンツと一緒にインポー

この記事では、OLEオブジェクトを挿入し、そのプロパティをドキュメントに設定する方法について説明します。

OLEオブジェクトの挿入

OLEオブジェクトが必要な場合は、InsertOleObjectメソッドを呼び出し、他のパラメータで明示的にProgIdを渡します。

Oleオブジェクトをドキュメントに挿入する方法を次のコード例に示します:

//For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->InsertOleObject(u"http://www.aspose.com", u"htmlfile", true, true, nullptr);
doc->Save(ArtifactsDir + u"WorkingWithOleObjectsAndActiveX.InsertOleObject.docx");

OLEオブジェクトを挿入するときにファイル名と拡張子を設定する

OLEパッケージは、oleハンドラーが不明な場合に埋め込まれたオブジェクトを格納するためのレガシーで「文書化されていない」方法です。

Windows3.1、95、98などの初期のWindowsバージョンにはパッケージャがありました。ドキュメントに任意のタイプのデータを埋め込むために使用できるexeアプリケーション。 このアプリケーションはWindowsから除外されるようになりましたが、Microsoft Wordおよび他のアプリケーションは、OLEハンドラーがないか不明な場合にデータを埋め込むた OlePackageクラスを使用すると、ユーザーはOLEパッケージのプロパティにアクセスできます。

次のコード例は、OLEパッケージのファイル名、拡張子、および表示名を設定する方法を示しています:

//For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
ArrayPtr<uint8_t> bs = System::IO::File::ReadAllBytes(MyDir + u"Zip file.zip");
{
SharedPtr<System::IO::Stream> stream = MakeObject<System::IO::MemoryStream>(bs);
SharedPtr<Shape> shape = builder->InsertOleObject(stream, u"Package", true, nullptr);
SharedPtr<OlePackage> olePackage = shape->get_OleFormat()->get_OlePackage();
olePackage->set_FileName(u"filename.zip");
olePackage->set_DisplayName(u"displayname.zip");
doc->Save(ArtifactsDir + u"WorkingWithOleObjectsAndActiveX.InsertOleObjectWithOlePackage.docx");
}

OLEオブジェクトの生データへのアクセスを取得する

ユーザーは、OleFormatクラスのさまざまなプロパティとメソッドを使用してOLEオブジェクトデータにアクセスできます。 たとえば、OLEオブジェクトの生データ、またはリンクされたOLEオブジェクトのソースファイルのパスと名前を取得できます。

次のコード例は、GetRawDataメソッドを使用してOLEオブジェクトの生データを取得する方法を示しています:

//For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
auto oleShape = System::DynamicCast<Shape>(doc->GetChild(NodeType::Shape, 0, true));
ArrayPtr<uint8_t> oleRawData = oleShape->get_OleFormat()->GetRawData();

Oleオブジェクトをアイコンとして挿入する

OLEオブジェクトは、画像としてドキュメントに挿入することもできます。

Oleオブジェクトをアイコンとして挿入する方法を次のコード例に示します。 この目的のために、DocumentBuilderクラスはInsertOleObjectAsIconメソッドを公開します:

//For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->InsertOleObjectAsIcon(MyDir + u"Presentation.pptx", false, ImagesDir + u"Logo icon.ico", u"My embedded file");
doc->Save(ArtifactsDir + u"WorkingWithOleObjectsAndActiveX.InsertOleObjectAsIcon.docx");

埋め込みOLEオブジェクトをストリームからドキュメントにアイコンとして挿入する方法を次のコード例に示します:

//For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
{
auto stream = MakeObject<System::IO::MemoryStream>(System::IO::File::ReadAllBytes(MyDir + u"Presentation.pptx"));
builder->InsertOleObjectAsIcon(stream, u"Package", ImagesDir + u"Logo icon.ico", u"My embedded file");
}
doc->Save(ArtifactsDir + u"WorkingWithOleObjectsAndActiveX.InsertOleObjectAsIconUsingStream.docx");

挿入オンラインビデオ

オンラインビデオは、*“Insert” > “Online Video”*タブからWord文書に挿入できます。 InsertOnlineVideoメソッドを呼び出すことで、現在の場所にあるドキュメントにオンラインビデオを挿入できます。

DocumentBuilderクラスには、このメソッドの4つのオーバーロードが導入されています。 最初のものは最も人気のあるビデオリソースで動作し、ビデオのURLをパラメータとして取ります。 例えば、最初の過負荷は、オンラインビデオの簡単な挿入をサポートしています YouTubeVimeo リソース。

次のコード例は、Vimeoのオンラインビデオをドキュメントに挿入する方法を示しています:

//For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->InsertOnlineVideo(u"https://youtu.be/t_1LYZ102RA", 360, 270);
// We can watch the video from Microsoft Word by clicking on the shape.
doc->Save(ArtifactsDir + u"DocumentBuilder.InsertVideoWithUrl.docx");

第二のオーバーロードは、他のすべてのビデオリソースで動作し、パラメータとして埋め込まれたHTMLコードを取ります。 動画を埋め込むためのHTMLコードはプロバイダによって異なる場合がありますので、詳細については各プロバイダにお問い合わせください。

次のコード例は、このようなHTMLコードを使用してオンラインビデオをドキュメントに挿入する方法を示しています:

//For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
String videoUrl = u"https://vimeo.com/52477838";
String videoEmbedCode = String(u"<iframe src=\"https://player.vimeo.com/video/52477838\" width=\"640\" height=\"360\" frameborder=\"0\" ") +
u"title=\"Aspose\" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>";
ArrayPtr<uint8_t> thumbnailImageBytes = System::IO::File::ReadAllBytes(ImageDir + u"Logo.jpg");
{
auto stream = MakeObject<System::IO::MemoryStream>(thumbnailImageBytes);
{
SharedPtr<System::Drawing::Image> image = System::Drawing::Image::FromStream(stream);
// Below are two ways of creating a shape with a custom thumbnail, which links to an online video
// that will play when we click on the shape in Microsoft Word.
// 1 - Insert an inline shape at the builder's node insertion cursor:
builder->InsertOnlineVideo(videoUrl, videoEmbedCode, thumbnailImageBytes, image->get_Width(), image->get_Height());
}
}
doc->Save(ArtifactsDir + u"DocumentBuilder.InsertOnlineVideoCustomThumbnail.docx");