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

Following, I will share a post which is published on Codeproject. This post introduces a method about inserting OLE Object (Adobe Acrobat Document) in Word by using C#. I share here because we can realize the function to insert OLE Object in Word document easily and quickly.

By the way, I have gotten the permission from article author to repost the post on my blog. Original post link: http://www.codeproject.com/Tips/467739/How-to-Insert-OLE-Object-Adobe-Acrobat-Document-in

Introduction

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

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

This article focuses on how to insert 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 image shown on page after inserting OLE object. For example, the image will be data information in worksheet if we insert a workbook as OLE Object. By clicking the image, we can get the workbook directly.

Steps

Step 1, Get Image Source

Declare a new DocPicture and use picture.LoadImage(Image) method to get image

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 suit for page, I set image size, including Width and Height.

            //Set Image Size

            picture.Width = 500;

            picture.Height = 700;

Step 3, Insert OLE Object

At first, I add section and paragraph in section after creating a new Word document. Then, insert OLE Object in this paragraph by using para.AppendOleObject() method. Three parameters are passed to this method, file path (PDF document path), OLE picture and OLE object type.

            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

using System.Drawing;

using Spire.Doc;

using Spire.Doc.Documents;

using Spire.Doc.Fields;

 

namespace InsertOleObject

{

    class Program

    {

        static void Main(string[] args)

        {

            //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 can get result as the following screenshot:

Conclusion

This article focuses on how to insert 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 image in coding.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s