Inserting Content That Contains Images Using altChunk
A fairly common question is how to use altChunk to insert content that contains images into a word-processing document. One approach is to generate an MHTML file (normally with the .MHT extension) that contains html and images. MHTML, short for MIME HTML, is a web page archive format used to assemble resources that are typically represented by external links (such as images) together with HTML code into a single file. You can see what an MHTML file looks like by saving as that file type using Word.
This blog is inactive.
New blog: EricWhite.com/blog
Blog TOCImages are stored in MHTML files using base64 encoding. You can convert to and from base64 encoding using the System.Convert.ToBase64String and the System.Convert.FromBase64CharArray methods. Transforming Open XML Documents to Flat OPC Format contains an example of converting to base64 encoding, and Transforming Flat OPC Format to Open XML Documents contains an example of converting from base64 encoding.
When using the Open XML SDK V2.0, you use the AlternativeFormatImportPartType enum to specify the format of the import part. Zeyad Rajabi has a blog post on using the Open XML SDK V2.0 to import using altChunk.
In a previous blog post, How to Use altChunk for Document Assembly, I explicitly specify the content type for the alternative format import part. The following table lists the content types that Word can import.
AlternativeFormatImportPartType enum |
Content Type |
Html |
text/html |
Mht |
message/rfc822 |
OfficeWordMacroEnabled |
application/vnd.ms-word.document.macroEnabled.main+xml |
OfficeWordMacroEnabledTemplate |
application/vnd.ms-word.template.macroEnabledTemplate.main+xml |
OfficeWordTemplate |
application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml |
Rtf |
application/rtf |
TextPlain |
text/plain |
WordprocessingML |
application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml |
XHtml |
application/xhtml+xml |
Xml * |
application/xml |
* This content type imports Office 2003 Word XML format (schemas) and the Office 2007 flat OPC format.