1

I would like to embed one Word document (call it "hidden.docx") into another Word document (call it "host.docx"). The document hidden.docx would not be visible at all when host.docx is opened in Word by an end-user. Document hidden.docx would only be carried inside host.docx, sort of as unstructured cargo data.

All research I have done points me to the use of something called altChunk offered by the Open XML SDK. I have installed Open XML SDK and got a sample working: http://msdn.microsoft.com/en-us/library/gg490656%28v=office.14%29.aspx

My question: In order to insert an altChunk into a docx, do I really need the Open XML SDK? Can this not be accomplished using VSTO? If so, how?

[PS: My ultimate goal is, for a pair of documents where one document is the original text and the other is its translated version in another language, to be able to preserve the original document within the translated document, so as not to lose it. For any document pair, there's always the risk that the two documents become separated through misplacement of one of them.]

Sabuncu
  • 5,095
  • 5
  • 55
  • 89
  • 2
    I suspect that the only way to insert Altchunk XML code using VSTO would be to to use Range.InsertXml. But that will probably result in integration of the chunk with the rest of the document, not addition of a hidden part. What I would probably do is create a custom XML part and see if i could put the entire text of the document (in Flat OPC format) there. If you just need to save text, another format might be appropriate. Word used to use PRIVATE fields to store such stuff, e.g. When converting from WordPerfect format, but they are easy to destroy accidentally. –  Aug 18 '13 at 07:44
  • @bibadiak Thank you, I was not aware of the Flat OPC format, and was thinking I would have to separately handle each file within the zip folder. Your comment was very helpful. The more I find out about the complexity of using custom XML parts, the more I am inclined to serialize the file (Flat OPC, then base64) and embed it inside an XML comment within settings.xml. I tried this and it works. Would welcome your opinion. – Sabuncu Aug 18 '13 at 10:34
  • Re. embedding inside a comment, if it works, it works. The main thing I'd want to research is what simple operations result in data loss (e.g., saving as .doc might & if you can, it is probably worth testing what happens when you save in non-compatibility mode and/or Strict ISO format from Word 2013). Suggest you modify your original message to include the code you've used to populate settings if you seek comments from others. –  Aug 18 '13 at 13:00
  • I am going to not worry about compatibility for the time being. Re: Comments in the settings.xml file. Turns out I spoke too soon. After modifying and saving the file, the comments are deleted, probably because Word generates a brand new file. Hate to go back to custom XML stuff - it's like trying to solve a puzzle! – Sabuncu Aug 18 '13 at 13:50

2 Answers2

2

Yes and No.

1.) That's not what AltChunks do. AltChunks are a way to embed one document into another document such that they get merged together. They are not hidden. If you create a docx package with an AltChunk in it, and then open up Word, Word will immediately merge that AltChunk into the document. (If that AltChunk is another Word document that also contains child AltChunks they will be recursively merged into the parent as well.) Basically, it's an easy way to merge content together without having to reconsolidate all their styles, rIDs, etc. -- if you save the document and examine it, the AltChunk will be gone, and you will notice that Word has merged the document back together into a single document again.

2.) Range.InsertXML, if provided a valid Flat Package for a full Word document will, under the hood, invoke that same merge functionality (down to having the same bugs, etc.) that you would get from an AltChunk. The two behave identical, and you can even create a document package with the OpenXML SDK that contains embedded AltChunks, and insert those (I've done this in Word 2007, 2010, and 2013) -- of course, as I mentioned above, the AltChunks are never persisted, they're immediately merged into the document.

If you want to save hidden data in a document, I recommend using Custom XML (take a look at Document.CustomXMLParts). Keep in mind though, at least in Word 2010, Undo does not revert changes to CustomXML parts.

BrainSlugs83
  • 6,214
  • 7
  • 50
  • 56
1

If you simply want to include some file into the Open XML package, then the simplest way is to use API from the System.IO.Packaging namespace (First obtain the reference to the main document part of the host part):

EmbeddedPackagePart hiddenDocumentPart = mainDocumentPart.AddEmbeddedPackagePart(@"application/vnd.openxmlformats-officedocument.wordprocessingml.document");
hiddenDocumentPart.FeedData(File.Open(hiddenDocumentFile, FileMode.Open));

Just to be sure, this way the hidden document will be in no way part of the host document content. It will only be part of its file (package). You can later extract it with a similar method: Get the main part of the host document, find the embedded (hidden) part and get/read the data from it.