I am inserting html text into a docx word document. All works okay except the end result docx file has a BOM at the beginning of the document.xml. I do not understand this although I suspect an encoding issue. Example code below. Would appreciate anyone's
thoughts on this.
void AddAltChunkHTML(MainDocumentPart mainPart, Word.SdtElement sdt, SPFile filename)
{
string altChunkId = "AltChunkId" + id;
id++;
// read insert data
byte[] byteArray = filename.OpenBinary();
AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Xhtml, altChunkId);
// load the insert data into the chunk
using (MemoryStream mem = new MemoryStream())
{
mem.Write(byteArray, 0, (int)byteArray.Length);
mem.Seek(0, SeekOrigin.Begin);
chunk.FeedData(mem);
}
Word.AltChunk altChunk = new Word.AltChunk();
altChunk.Id = altChunkId;
//Replace content control with altChunk information
OpenXmlElement parent = sdt.Parent;
parent.InsertAfter(altChunk, sdt);
sdt.Remove();
}
void AddAltChunkHTML(MainDocumentPart mainPart, Word.SdtElement sdt, SPFile filename)
{
string altChunkId = "AltChunkId" + id;
id++;
// read insert data
byte[] byteArray = filename.OpenBinary();
AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Xhtml, altChunkId);
// load the insert data into the chunk
using (MemoryStream mem = new MemoryStream())
{
mem.Write(byteArray, 0, (int)byteArray.Length);
mem.Seek(0, SeekOrigin.Begin);
chunk.FeedData(mem);
}
Word.AltChunk altChunk = new Word.AltChunk();
altChunk.Id = altChunkId;
//Replace content control with altChunk information
OpenXmlElement parent = sdt.Parent;
parent.InsertAfter(altChunk, sdt);
sdt.Remove();
}