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-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertOleObject("http://www.aspose.com", "htmlfile", true, true, null);
dataDir = dataDir + "DocumentBuilderInsertOleObject_out.doc";
doc.Save(dataDir);

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

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

Windows 3.1、95、98 などの初期の Windows バージョンには、あらゆる種類のデータをドキュメントに埋め込むために使用できる Packager.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-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
byte[] bs = File.ReadAllBytes(dataDir + @"input.zip");
using (Stream stream = new MemoryStream(bs))
{
Shape shape = builder.InsertOleObject(stream, "Package", true, null);
OlePackage olePackage = shape.OleFormat.OlePackage;
olePackage.FileName = "filename.zip";
olePackage.DisplayName = "displayname.zip";
dataDir = dataDir + "DocumentBuilderInsertOleObjectOlePackage_out.doc";
doc.Save(dataDir);
}

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Load document with OLE object.
Document doc = new Document(dataDir + "DocumentBuilderInsertTextInputFormField_out.doc");
Shape oleShape = (Shape)doc.GetChild(NodeType.Shape, 0, true);
byte[] oleRawData = oleShape.OleFormat.GetRawData();

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.InsertOleObjectAsIcon(dataDir + "embedded.xlsx", false, dataDir + "icon.ico", "My embedded file");
doc.Save(dataDir + "EmbeddeWithIcon_out.docx");
Console.WriteLine("The document has been saved with OLE Object as an Icon.");

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.InsertOleObjectAsIcon(dataDir + "embedded.xlsx", false, dataDir + "icon.ico", "My embedded file");
using (MemoryStream stream = new MemoryStream(File.ReadAllBytes(dataDir + "embedded.xlsx")))
builder.InsertOleObjectAsIcon(stream, "Package", dataDir + "icon.ico", "My embedded file");
doc.Save(dataDir + "EmbeddeWithIconUsingStream_out.docx");
Console.WriteLine("The document has been saved with OLE Object as an Icon.");

オンラインビデオを挿入

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

DocumentBuilder クラスでは、このメソッドの 4 つのオーバーロードが導入されています。 1 つ目は最も一般的なビデオ リソースで動作し、ビデオの URL をパラメータとして受け取ります。たとえば、最初のオーバーロードは、YouTube および ヴィメオ リソースからのオンライン ビデオの単純な挿入をサポートします。

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
//The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithOnlineVideo();
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Pass direct url from youtu.be.
string url = "https://youtu.be/t_1LYZ102RA";
double width = 360;
double height = 270;
Shape shape = builder.InsertOnlineVideo(url, width, height);
dataDir = dataDir + "Insert.OnlineVideo_out_.docx";
doc.Save(dataDir);

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
//The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithOnlineVideo();
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Shape width/height.
double width = 360;
double height = 270;
// Poster frame image.
byte[] imageBytes = File.ReadAllBytes("TestImage.jpg");
// Visible url
string vimeoVideoUrl = @"https://vimeo.com/52477838";
// Embed Html code.
string vimeoEmbedCode = "";
builder.InsertOnlineVideo(vimeoVideoUrl, vimeoEmbedCode, imageBytes, width, height);
dataDir = dataDir + "Insert.OnlineVideo_out_.docx";
doc.Save(dataDir);