OLE 代表 對象鏈接和嵌入。它由 Microsoft 提供,允許您創建和編輯包含由各種應用程序創建的項目或對象的文檔。

例如,您可以將電子表格、圖像和聲音剪輯作為 OLE 對象嵌入到 Word 文檔中。您可以在 Word 文檔中使用這些 OLE 對象,而不必擔心一次又一次地切換到多個應用程序。您可以在 C# 中使用 OLE 以編程方式嵌入或插入此類對象。

本文將指導您如何:

本文中的步驟和代碼示例使用 GroupDocs.Merger for .NET。因此,請確保通過以下任一方法安裝 API:

  • 使用 NuGet 包管理器安裝。
  • 下載 the DLL and reference it into the project.

在 C# 中將 PDF 作為 OLE 對象插入到 MS Word 文檔中

在 C# 的 Word 文檔中將 PDF 作為 OLE 插入

以下是展示如何將 PDF 文件作為 OLE 對象嵌入 Word 文檔的步驟和 C# 代碼示例:

  1. 使用嵌入選項和要嵌入 Word 文檔的文檔實例化 OleWordProcessingOptions
  2. 現在用源 Word 文檔路徑或流實例化 Merger 對象。
  3. 調用 ImportDocument 方法並傳遞在步驟 1 中設置的 OLE 文字處理選項的對象。
  4. 而已。調用 Save 方法以獲取將 PDF 文檔作為 OLE 對象的結果 Word 文檔。
// Embed a PDF file into a Word document as an OLE Object in C#
int pageNumber = 2;
OleWordProcessingOptions oleWordProcessingOptions = new OleWordProcessingOptions(@"embedded-doc.pdf", pageNumber)
{ 
    Width = 300, // Just setting the height & width, you have more options.
    Height = 300
};
// Use Merger class to start with source Word document and embed PDF as OLE object.
using (Merger merger = new Merger(@"source-doc.docx"))
{
    merger.ImportDocument(oleWordProcessingOptions);
    merger.Save(@"word-document-with-OLE.docx");
}

在 C# 中將 Word 文檔作為 OLE 對象插入到 Excel 電子表格中

在 C# 的 Excel 電子表格中插入 Word 文件的 OLE

我們可以將 OLE 對象嵌入到 Excel 電子表格中。下面的 CSharp 代碼示例和步驟解釋瞭如何將 Word 文檔作為 OLE 對象添加到 Excel 電子表格中:

  1. 使用嵌入選項和要嵌入 Excel 電子表格的文檔實例化 OleSpreadsheetOptions
  2. 現在用源電子表格路徑或流實例化 Merger 對象。
  3. 現在調用 ImportDocument 方法並傳遞在步驟 1 中設置的 OLE 電子表格選項的對象。
  4. 最後,調用Save方法得到以Word文檔作為OLE對象的生成的Excel電子表格。
// Embed a Word file into an Excel Spreadsheet as an OLE Object in C#
int pageNumber = 2;
OleSpreadsheetOptions oleSpreadsheetOptions = new OleSpreadsheetOptions(@"embedded-doc.docx", pageNumber)
{
    RowIndex = 2, // Setting the Row & height Index, you have more options.
    ColumnIndex = 2
};
// Using Merger class with source spreadsheet and embedding a Word document as an OLE object.
using (Merger merger = new Merger(@"sample-doc.xlsx"))
{
    merger.ImportDocument(oleSpreadsheetOptions);
    merger.Save(@"excel-sheet-with-ole.xlsx");
}

在 C# 中將 PDF 作為 OLE 對象添加到 PowerPoint 演示文稿

在 C# 中的 PowerPoint 演示文稿中將 PDF 作為 OLE 插入

同樣,這裡我們在 PowerPoint 演示文稿中插入對象。

  1. 使用嵌入選項和要嵌入 PowerPoint 演示文稿的文檔實例化 OlePresentationOptions
  2. 現在用源演示路徑或流實例化 Merger 對象。
  3. 調用 ImportDocument 方法並傳遞在步驟 1 中設置的 OLE Presentation Options 對象。
  4. 最後,調用 Save 方法以獲取作為 OLE 對象的 PDF 文檔的結果 PowerPoint 演示文稿。
// Embed a PDF file into an Excel Spreadsheet as an OLE Object in C#
int pageNumber = 2;
OlePresentationOptions olePresentationOptions = new OlePresentationOptions(@"embedded.pdf", pageNumber)
{
    X = 10, // Setting only X & Y coordinates, you can customize more.
    Y = 10
};
// Using Merger class to embed a PDF file as an OLE object in the PowerPoint presentation.
using (Merger merger = new Merger(@"sample-presentation.ppt"))
{
    merger.ImportDocument(olePresentationOptions);
    merger.Save(@"powerpoint-presentation-with-ole.ppt");
}

結論

我們已經討論了在 C# 中以編程方式在 Word、Excel 或 PowerPoint 文檔中插入 OLE 對像是多麼容易和快速。每個目標的代碼只有很小的差異,即不同的 OLE 選項類及其對每種文件格式的選項:

  • OleWordProcessingOptions 在 Word 文檔中嵌入 OLE 對象。
  • OleSpreadsheetOptions 在 Excel 電子表格中嵌入 OLE 對象。
  • OlePresentationOptions 用於在 PowerPoint 演示文稿中嵌入 OLE 對象。

您可以從 文檔 或讓我們在 免費支持論壇 中了解更多關於 API 的信息