In short: How can I merge the content of altchunks into the docx source files, to prevent problems opening a docx that has too many altchunks?
Background: I have a docx file with 5,000 altchunks that were dynamically inserted when the file was created. Because there are so many altcunks, each having at least 1 page worth of content, the file won't open in Word -- the Word 2010 reports "Word could not create the work file", Word 2007 reports "Too many windows open", while the Office interop reports "The file appears to be corrupted".
I believe the source of the problem is that the altchunk content is stored in different files within the docx file structure and having to open so many files exceeds some limit. In that case I would be better off merging the altchunk content into the actual source of the docx. How can I do that?
It's ok if I lose the altchunk in the process or its definition gets modified as long as the content appears unchanged.
Here's an example of how a single altchunk is created, given an HTML snippet htmlText
:
''// add a part to the document for the HTML chunk
Const snippetChunkID As String = "HtmlSnippetChunk"
Dim snippetChunkPart As AlternativeFormatImportPart = currentDocument.MainDocumentPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Html, snippetChunkID)
''// feed HTML into new chunk
Dim stream As New MemoryStream(Encoding.Default.GetBytes("<html><body>" & htmlText & "</body></html>"))
snippetChunkPart.FeedData(stream)
stream.Close()
stream.Dispose()
''// insert chunk after the placeholder element
Dim snippetAltChunk As New AltChunk() With {.Id = snippetChunkID}
placeholderElement.InsertAfter(snippetAltChunk, placeholderElement)