3

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

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

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

如果我在过程中丢失了 altchunk 或者它的定义被修改了,只要内容看起来没有改变就可以了。

下面是一个如何创建单个 altchunk 的示例,给定一个 HTML 片段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)
4

0 回答 0