I think it is because you cannot import an AltChunk
into a document that is opened from a memory stream. I had the same issue. I was opening the template from a memory stream like so:
Private Sub UpdateDoc(templatePath As String)
Using fs As FileStream = File.OpenRead(templatePath)
Using ms As New MemoryStream
CopyStream(fs, ms)
Using doc As WordprocessingDocument = WordprocessingDocument.Open(ms, True)
'update the document
doc.MainDocumentPart.Document.Save()
End Using
End Using
End Using
End Sub
Private Sub CopyStream(source As Stream, target As Stream)
Dim buffer() As Byte
Dim bytesRead As Integer = 1
ReDim buffer(32768)
While bytesRead > 0
bytesRead = 0
bytesRead = source.Read(buffer, 0, buffer.Length)
target.Write(buffer, 0, bytesRead)
End While
End Sub
This works for normal updates of content controls etc. and document is fine when streamed back to client or saved as docx. But it corrupts doc when inserting an AltChunk
.
Opening a doc from a physical file path works when inserting AltChunk
like so:
Using doc As WordprocessingDocument = WordprocessingDocument.Open(strTempFile, True)
Dim altChunkId As String = "AltChunkId1"
Dim mainDocPart As MainDocumentPart = doc.MainDocumentPart
Dim chunk As AlternativeFormatImportPart = mainDocPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Xhtml,
altChunkId)
Dim strHTML As String = "<html><head/><body><h1>Html Heading</h1><p>This is an html document in a string literal.</p></body></html>"
Using chunkStream As Stream = chunk.GetStream(FileMode.Create, FileAccess.Write)
Using sr As StreamWriter = New StreamWriter(chunkStream)
sr.Write(strHTML)
End Using
End Using
Dim altChunk As New AltChunk
altChunk.Id = altChunkId
mainDocPart.Document.Body.InsertAfter(altChunk, mainDocPart.Document.Body.Elements(Of Paragraph)().Last())
mainDocPart.Document.Save()
End Using
It seems you cannot import an AltChunk
into a memory stream, you can only do it when you open the physical file for writing. Can anyone shed some light on this matter?