Welcome to OpenXML Developer Sign in | Join | Help

Embedding files (doc, xls, pdf, jpg, gif) in WordML

Last post 08-13-2008, 3:07 AM by Sanpreet. 5 replies.
Sort Posts: Previous Next
  •  07-17-2008, 7:46 AM 3477

    Embedding files (doc, xls, pdf, jpg, gif) in WordML

    Hi,

    I have several files (doc, xls, pdf, ...) that are related to one product description.
    I wanted to package all files and the description in a monolithic wordml file.

    So if someone opens the wordml-file the product description and the related files (as objects) are shown.

    I'm pretty new to wordml and not quite sure how to begin. There are several low level approaches e. g. you can create ole objects, convert them as base64 string and embed them in the wordml document. But I couldn't find appropriate components to do this. I also think that there must be better alternatives to this - at least I hope so.

    Can any of you give me an advice where to begin? I'd like to use c# if possible.

    Cheers,
    Helmut




  •  07-18-2008, 10:18 AM 3481 in reply to 3477

    Re: Embedding files (doc, xls, pdf, jpg, gif) in WordML

    Create the destination part of the file.

    PackagePart embeddedPart = package.createPart("/word/embeddings/MyWorkSheet.xls", "application/vnd.openxmlformats-officedocument.spreadsheet.sheet")

    Get the stream of the files you want to add to your Wordml document.
    Write this stream to the stream of the embedded part.
    Create a relationship.

    Add the markups to your main document part to put the embedded part where you want.





  •  07-21-2008, 7:04 AM 3490 in reply to 3481

    Re: Embedding files (doc, xls, pdf, jpg, gif) in WordML

    Hi,

    thanks a lot.

    There are two issues that stay unclear to me:
    1. When I create a docx with an embedded .doc document by hand with Office 2007, the following code is created  in document.xml
    <o:OLEObject Type="Embed" ProgID="Word.Document.8" ShapeID="_x0000_i1025" DrawAspect="Icon" ObjectID="_1278147184" r:id="rId5">
                            <o:FieldCodes>\s</o:FieldCodes>
                        </o:OLEObject>

    In this code there's an ObjectID. Where comes this ObjectID from? When I embed a file with .Net I don't have an Object Id.

    2. When I create a docx and embed a PDF document by hand in Office 2007, the following code is created in document.xml
    <o:OLEObject Type="Embed" ProgID="Package" ShapeID="_x0000_i1026" DrawAspect="Content" ObjectID="_1277900222" r:id="rId7"/>

    Associated to this object is a file embedded/oleObject1.bin. This file isn't the original pdf file. So what is it then and how can I create it with .Net?


  •  07-21-2008, 11:25 AM 3491 in reply to 3490

    Re: Embedding files (doc, xls, pdf, jpg, gif) in WordML

    Hi,

    1. http://openxmldeveloper.org/forums/thread/1791.aspx

    2. The .bin is your PDF file. I don't really know how Word converts the PDF file into a binary file when you insert it in a document. Maybe these links could help you understand (never read it myself) :
    http://www.codeproject.com/KB/cs/office2007bin.aspx
    http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=3642085&SiteID=1

    Maybe this could help :
    using (WordprocessingDocument test = WordprocessingDocument.Open("C:\\testOleObject.docx",true))
                {
                    MainDocumentPart mainpart = test.MainDocumentPart;
                    EmbeddedObjectPart embed = mainpart.AddEmbeddedObjectPart("application/vnd.openxmlformats-officedocument.oleObject");
                    Stream fileStr = (Stream)File.Open("C:\\MyPDFFile.pdf", FileMode.Open);
                    Stream partStream = embed.GetStream();

                    const int size = 4096;
                    byte[] bytes = new byte[4096];
                    int numBytes;
                    while ((numBytes = fileStr.Read(bytes, 0, size)) > 0)
                        partStream.Write(bytes, 0, numBytes);
                }

    Regards
  •  07-23-2008, 3:38 AM 3499 in reply to 3491

    Re: Embedding files (doc, xls, pdf, jpg, gif) in WordML

    Hi,

    thanks again!

    Have you or anyone else already been able to create a docx with an embedded PDF object?

    I've created a docx-package with my pdf file in it and a relationship entry in word\_rels\document.xml.rels

    But as soon as I try link to that object in Word Word2007 can't open the file anymore.

    The .bin generated by word when importing the pdf is indeed an ole object. You can unzip it using 7-zip. Then you'll find a couple of streams in it. One is the imported pdf.

    Cheers


  •  08-13-2008, 3:07 AM 3566 in reply to 3491

    Re: Embedding files (doc, xls, pdf, jpg, gif) in WordML

     

    using (WordprocessingDocument test = WordprocessingDocument.Open("C:\\testOleObject.docx",true))
                {
                    MainDocumentPart mainpart = test.MainDocumentPart;
                    EmbeddedObjectPart embed = mainpart.AddEmbeddedObjectPart("application/vnd.openxmlformats-officedocument.oleObject");
                    Stream fileStr = (Stream)File.Open("C:\\MyPDFFile.pdf", FileMode.Open);
                    Stream partStream = embed.GetStream();

                    const int size = 4096;
                    byte[] bytes = new byte[4096];
                    int numBytes;
                    while ((numBytes = fileStr.Read(bytes, 0, size)) > 0)
                        partStream.Write(bytes, 0, numBytes);
                }

    Kirikou,

       i am able to run this code, i am able to embed a .pdf file into a word doc but cannot see the file

     the .pdf file is getting embedded into it, as the size of the file increases but i cannot see the file.

    can you let me know what else do i need to do.

    thanks

    Sanpreet

View as RSS news feed in XML