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.

Create Barcode in Word Document with C#, VB.NET

Take any products from shelf in supermarket. You can find the graphic which is formed with black/blank stripes and numbers on products. Now, you come to checkstand and put your products on the desk. The cashier scans the graphic and the products price is presented. So, do you know what the graphic is used to do? It is barcode, which shows much information of one product, including country, manufacturer, name, price, date to produce etc. Not only used in supermarket, barcode is also widely used in library, postal management, banking system.

So, how to create a barcode? Of course, you can search for some online tools to generate barcode directly. But it is useless when you want to create one programmatically. This post will show a solution to create barcode in Word by using C#, VB.NET.

I prepare a Word document which I have imported some texts. Then, I need to convert the texts to barcode. Please note that I have downloaded barcode font on my system. Also, in order to realize this function more quickly, I use a .NET Word component in my project.

Firstly, invoke document.FindAllString(string, bool caseSensitive, bool wholeWord) method to find barcode text and save it in TextSelection array. Use foreach sentence to set Text, FontName and FontSize properties for each selection in this TextSelection array. Finally, save this file and you can find the barcode as following screenshot after launching.

barcode

CODE:

C#

using System.Drawing;

using Spire.Doc;

using Spire.Doc.Documents;

using Spire.Doc.Fields;

 

namespace DOCTEST

{

    class BarCode

    {

        static void Main(string[] args)

        {

            Document document = new Document(@”E:\word.docx”);

            TextSelection[] textSelections = document.FindAllString(“Cola Barcode”, true, true);

 

            foreach (TextSelection selection in textSelections)

            {

                selection.GetAsOneRange().Text = “619051207”;

                selection.GetAsOneRange().CharacterFormat.FontName = “C39HrP60DlTt”;

                selection.GetAsOneRange().CharacterFormat.FontSize= 70;

            }

 

            document.SaveToFile(“Barcode.docx”, FileFormat.Docx);

            System.Diagnostics.Process.Start(“Barcode.docx”);

        }

    }

}

VB  

Imports System.Drawing

Imports Spire.Doc

Imports Spire.Doc.Documents

Imports Spire.Doc.Fields

 

Namespace DOCTEST

    Friend Class BarCode

        Shared Sub Main(ByVal args() As String)

            Dim document As New Document(“E:\word.docx”)

            Dim textSelections() As TextSelection = document.FindAllString(“Cola Barcode”, True, True)

 

            For Each selection As TextSelection In textSelections

                selection.GetAsOneRange().Text = “619051207”

                selection.GetAsOneRange().CharacterFormat.FontName = “C39HrP60DlTt”

                selection.GetAsOneRange().CharacterFormat.FontSize = 70

            Next selection

 

            document.SaveToFile(“Barcode.docx”, FileFormat.Docx)

            System.Diagnostics.Process.Start(“Barcode.docx”)

        End Sub

    End Class

End Namespace

Download Spire.Doc for .NET Here