Quantcast
Channel: Open XML Format SDK forum
Viewing all articles
Browse latest Browse all 1288

AlternativeFormatImportPart (AltChunk) and WordprocessingDocument in MemoryStream creates a corrupt file

$
0
0

Hi, I have a project where i need to add plain text as well as the content of small .docx files to a main .docx.
I have created a test case, and with the use of AlternativeFormatImportParti I can successfully perform the following code to add plain text in my files.

string chunkId = "D"+Regex.Replace(DateTime.Now.ToString(), @"[^\d]", "");

AlternativeFormatImportPart chunk = _mainDocPart.AddAlternativeFormatImportPart(
AlternativeFormatImportPartType.TextPlain, chunkId);

using (var memstream = new MemoryStream(Encoding.UTF8.GetBytes(text)))
{
	chunk.FeedData(memstream);
}

AltChunk altChunk = new AltChunk();
altChunk.Id = chunkId;

OpenXmlElement parent = placeHolder.Parent;
parent.InsertBefore(altChunk, placeHolder);

The problem is that in my actual environment I need to load WordprocessingDocument  from a memory stream.
It seems like when i do this the saved file cannot be opened by Word (Corrupt file)

I can add paragraphs and other stuff, but not AltChunck when I initiate the WordprocessingDocument as follows

var byteArrayWithFileFrom360 = ProcessFileHandler.GetFileContent(204735);

var wordDocMemoryStream = new MemoryStream();
wordDocMemoryStream.Write(byteArrayWithFileFrom360, 0, byteArrayWithFileFrom360.Length);

var myDoc = WordprocessingDocument.Open(wordDocMemoryStream, true);

 

Viewing all articles
Browse latest Browse all 1288

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>