Embed word document into powerpoint open xml c#

ECIT DEV 26 Reputation points
2021-11-20T02:41:24.767+00:00

i have a requirement to embed a word document in a powerpoint. i am using open xml (c#) for that. used "Docxtosource" to see how the embeding is done. below is the code which i got

private void GenerateEmbeddedObjectPart(ref EmbeddedObjectPart part)
{
MemoryStream ms = new MemoryStream();
using (FileStream file = new FileStream(this.DocumentPath, FileMode.Open, FileAccess.Read))
file.CopyTo(ms);

    try
    {
        part.FeedData(ms);
    }
    finally
    {
        ms.Dispose();
    }
}

The above code embeds the object but when tried to open it shows a prompt of how to open the document. When i relooked the code generated by the DocxToSource code which is as below

private void GenerateEmbeddedObjectPart(ref EmbeddedObjectPart part)
{
string base64 ="base64string generated by the tool";
Stream mem = new MemoryStream(Convert.FromBase64String(base64), false);
try
{
part.FeedData(mem);
}
finally
{
mem.Dispose();
}
}
The base64 string generated by the DocxToSource tool has some headers embded when decoded. So how do we generate this headers and embed into the powerpoint. Or could you please redirect me to a documentation which has this embeding logic. Thanks a ton.

{count} votes

1 answer

Sort by: Most helpful
  1. David 151 Reputation points
    2021-12-02T05:53:48.093+00:00

    If you do not mind using a 3rg party library, i would suggest that you try Free Spire.Office, which is a community edition of Spire.Office.

    Step 1. Install Free Spire.Office for .NET (also available on NuGet).
    Step 2. Refer to the following code example to embed a Word document as an OEL object in a slide.

    using System.IO;
    using Spire.Doc;
    using Spire.Presentation;
    using System.Drawing;
    using Spire.Presentation.Drawing;
    
    namespace EmbedDocxToPptx
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Load Word document
                Document doc = new Document();
                doc.LoadFromFile(@"C:\Users\Administrator\Desktop\Styles.docx");
    
                //Load PowerPoint document
                Presentation ppt = new Presentation();
                ppt.LoadFromFile(@"C:\Users\Administrator\Desktop\source.pptx");
    
                //Load an image, whcih will be used as an display icon
                IImageData oleImage = ppt.Images.Append(Image.FromFile(@"C:\Users\Administrator\Desktop\docx-icon.png"));
    
                //Create a Rectangle object, which defines the position and size of the display icon
                Rectangle rect = new Rectangle(100, 60, oleImage.Width, oleImage.Height);
    
                using (MemoryStream ms = new MemoryStream())
                {
                    //Convert Word to stream
                    doc.SaveToStream(ms, Spire.Doc.FileFormat.Docx2013);
                    ms.Position = 0;
    
                    //Insert the Word document as an OLE object in a slide
                    IOleObject oleObject = ppt.Slides[0].Shapes.AppendOleObject("word", ms.ToArray(), rect);   
    
                    //Set the display icon
                    oleObject.SubstituteImagePictureFillFormat.Picture.EmbedImage = oleImage;            
                    oleObject.IsIconVisible = true;
                    oleObject.ProgId = "Word.Document.12";
                }
    
                //Save the PowerPoint document to another file
                ppt.SaveToFile("InsertOle.pptx", Spire.Presentation.FileFormat.Pptx2013);
            }
        }
    }
    
    0 comments No comments