将altchunk内容合并到docx源中

时间:2012-04-09 00:22:58

标签: .net ms-office openxml office-automation

简而言之:如何将altchunks的内容合并到docx源文件中,以防止打开具有太多altchunks的docx时遇到问题?

后台:我有一个包含5,000个alt文件的docx文件,这些文件是在创建文件时动态插入的。因为有太多的altcunks,每个都有至少1页的内容,该文件将无法在Word中打开 - Word 2010报告“Word无法创建工作文件”,Word 2007报告“打开的窗口太多” ,Office互操作报告“该文件似乎已损坏”。

我认为问题的根源是altchunk内容存储在docx文件结构中的不同文件中,并且必须打开这么多文件超过某些限制。在这种情况下,我最好将altchunk内容合并到docx的实际源代码中。我怎么能这样做?

如果我在此过程中丢失了altchunk,或者只要内容 不变,其定义就会被修改,这是可以的。

在给定HTML代码段htmlText的情况下,这是一个如何创建单个altchunk的示例:

''// 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)

0 个答案:

没有答案