1

How to embed a word document into another word document via OpenXML SDK, but showing content, not an icon of word? Such, as we do it manually in word: Insert object from file -> WITHOUT checking "Dispaly as icon"?

I've found this article, but it uses an icon. I've also tried to use OpenXML SDK Productivity Tool, but shows only generated binary data.

EDITED: I use the following code:

DrawAspect = OleDrawAspectValues.Content

and then i add image part:

var imagePart = mainDocumentPart.AddNewPart<ImagePart>("image/x-emf", imagePartId);
GenerateImagePart(imagePart);

But my image part - is just an array of bytes of word's icon. So, in this case happens the following: when i open generated document, it shows embedded document as an icon, but when i double click this embedded document, edit it and save changes, the embedded document is shown as a content, so maybe it's possible in some way to show this content without editing embedded document? Should i use instead of array of bytes of word's icon an array of bytes of doc's screenshot? Not sure i described it clear, so please ask

Alex Ovechkin
  • 810
  • 6
  • 20

2 Answers2

3

I'm afraid what you are asking for is almost impossible. The only difference as far as the word file is concerned between the icon and the embedded file, is the image. When you don't use a icon Word pretty much just take a screenshot of the document you are embedding and inserts that in place of the Icon graphic.

I've uploaded an example I grabbed from a Word file I made. Found this little gem in the /media folder inside the .docx file.

So basicly, your only choice in resolving this if you can't live with the Icon is to somehow grab a picture of the word-file you want to embed and insert that instead of the Icon image.

How you'd go about that can't be pretty. First of all the open xml sdk contains no such functionality. I tried playing a bit around with office interop as well, but no luck.

I only see two possible ways to achieve this.

First one is via Interop. You'll need to install a "pretend printer" like the ones that print to PDF instead of sending it to a printer. This one however needs to print to an image format. The format of the file in the Media folder was .emf but I'm not positive thats a requirement.

Anyways, should the above somehow be possible you could embed that picture, pretty much using the example you link from Microsoft, and just change this size of the "icon" which now would be an image of the document.

Second possibility would be to open the word document as a process, set the document size to 72% (or whatever makes the document be the only one on screen on your desktop) and the grab a print screen and cut it down to just the document and the use that as your image for the embedding.

For the record, I don't recommend you do any of the above, but thoose are the only options I see.

Should someone have a better solution to this I'm all ears.

Finally, should you decide that you want to push on with this, I'll be happy to code up an example of option number 2 if you reply and tell me you'd like that.

  • Kaspar

enter image description here

Kaspar Kjeldsen
  • 936
  • 1
  • 13
  • 30
  • Thanks for your reply, but i don't think the 2nd option is good enaugh, 'case this app should process a lot of docs in threads, so to run a word process is not a very good idea. I've also was playing around with draw aspect (sorry didn't mention it) and i found interesting, that if you specify it to "Content", then AFTER editing a embedded document and saving it - you will get the content in your host document instead of icon. The question is how to achieve it without editing embedded doc. Also i think your first idea is worth thinking, thanx – Alex Ovechkin Apr 22 '13 at 14:24
  • Could you elaborate a bit further? I'm not sure I understand. It's my understanding, that however you embed a document, what you actually see when you open the main document is just a picture representing that document. I don't think it's possible, but maybe you can trick word to generate the image when the document is opened. That would just seem strange, since word generates an image and embeds that itself, when you are embedding a word file, and not using an Icon. Again, I'm not sure I'm understand what you are saying, so please elaborate :) – Kaspar Kjeldsen Apr 22 '13 at 14:59
2

There is a nice wrapper API (Document Builder 2.2) around open xml specially designed to merge documents, with flexibility of choosing the paragraphs to merge etc. You can download it from here.

Using this tool you can embed a paragraph of another word document or entire word document as per your requirement.

The documentation and screen casts on how to use it are here.

Hope this helps.

Flowerking
  • 2,551
  • 1
  • 20
  • 30
  • AFAIK, Document Builder merges documents - i mean inserts content of one to another, but i need to embed it, so that you can double-click and edit it – Alex Ovechkin Apr 18 '13 at 12:42
  • Ok Sorry, I missunderstood your question. Please check similar answer [here](http://stackoverflow.com/a/3322259/860243) is of any use to you – Flowerking Apr 18 '13 at 12:47