Click here to Skip to main content
Click here to Skip to main content

How to Insert an OLE Object (Adobe Acrobat Document) in Word with C#

By , 11 Dec 2012
 

Introduction

OLE (Object Linking and Embedding) allows applications to link with each other as objects of software. It is often used to create a compound document to collect a variety of elements together, such as text, sound, image, video, table, application etc.

Microsoft Word enables users to insert OLE objects in documents. After inserting, we can find that the object contents which are created in another program are available in Word as well.

This article focuses on how to insert an OLE object, which is a PDF document in my example, by using C#, easily and quickly without Microsoft Automation.

Background

In Microsoft Word, there will be an image, which is related to this object contents, shown on a page after inserting an OLE object. For example, the image will be data information in a worksheet if we insert a Workbook as OLE Object. By clicking the image, the workbook will pop up and data in it can be modified. 

Steps  

Step 1: Get Image Source

Declare a new DocPicture and use the picture.LoadImage(Image) method to get the image which will be displayed on the Word page.  

Note: the image is the first page of the PDF document which I want to insert as OLE object. I export the page as image by using a PDFViewer

DocPicture picture = new DocPicture(mydoc);
Image image = Image.FromFile(@"E:\PDFImage.jpg");
picture.LoadImage(image);

Step 2: Set Image Size

In order to make image size suitable for the page, I set the image size, including width and height.

picture.Width = 500;
picture.Height = 700;

Step 3: Insert OLE Object

At first, I add a section and a paragraph in the section after creating a new Word document. Then, insert an OLE Object in this paragraph by invoking the paragraph.AppendOleObject(filePath, DocPicture, and OleOobjectType) method. 

DocOleObject obj = para.AppendOleObject(@"E:\work\Documents\PDF\5 Ways " + 
  @"to Reduce Travel Marketing Costs.pdf", picture, 
  Spire.Doc.Documents.OleObjectType.AdobeAcrobatDocument);

Full Code Sample 

//Create Document
Document mydoc = new Document();
Section mysec = mydoc.AddSection();
Paragraph para = mysec.AddParagraph();

//Get Image Source
DocPicture picture = new DocPicture(mydoc);
Image image = Image.FromFile(@"E:\PDFImage.jpg");
picture.LoadImage(image);

//Set Image Size
picture.Width = 500;
picture.Height = 700;

//Insert OLE Object
DocOleObject obj = para.AppendOleObject(@"E:\work\Documents\PDF\5 Ways to " + 
   @"Reduce Travel Marketing Costs.pdf", picture, 
   Spire.Doc.Documents.OleObjectType.AdobeAcrobatDocument);

//Save and Launch
mydoc.SaveToFile("DocOLEObject.docx", FileFormat.Docx2010);
System.Diagnostics.Process.Start("DocOLEObject.docx");

After running, we will get results as in the following screenshot:

Conclusion

This article focuses on how to insert an OLE Object (PDF document) in Word by using C# based on a .NET Word component. There are two elements which must be prepared, OLE Picture and OLE Object file at the beginning. The image can be any image or related to the OLE object contents. Also, we can convert the OLE object to an image in code.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

SummiG
New Zealand New Zealand
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 11 Dec 2012
Article Copyright 2012 by SummiG
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid