/// <summary> /// Saves the document in OOXML format. /// </summary> private void SaveInOOXML() { //First create the file if this is a new file if (_isNew && _template != null) { //Create a new file based on the template //Read the template stream inot a new document. using (_template) { using (var documentStream = new MemoryStream((int)_template.Length)) { _template.Position = 0L; byte[] buffer = new byte[2048]; int length = buffer.Length; int size; while ((size = _template.Read(buffer, 0, length)) != 0) { documentStream.Write(buffer, 0, size); } documentStream.Position = 0L; // Modify the document to ensure it is correctly marked as a Document and not Template using (var document = WordprocessingDocument.Open(documentStream, true)) { document.ChangeDocumentType(WordprocessingDocumentType.Document); } //Save the stream to a new file. File.WriteAllBytes(Filename, documentStream.ToArray()); } } } else if (_isNew && !string.IsNullOrWhiteSpace(_filenameTemplate)) { //Create a new file based on the template //Read the file in to a stream. using (var templateStream = File.OpenRead(_filenameTemplate)) { using (var documentStream = new MemoryStream((int)templateStream.Length)) { templateStream.Position = 0L; byte[] buffer = new byte[2048]; int length = buffer.Length; int size; while ((size = templateStream.Read(buffer, 0, length)) != 0) { documentStream.Write(buffer, 0, size); } documentStream.Position = 0L; // Modify the document to ensure it is correctly marked as a Document and not Template using (var document = WordprocessingDocument.Open(documentStream, true)) { document.ChangeDocumentType(WordprocessingDocumentType.Document); } //Save the stream to a new file. File.WriteAllBytes(Filename, documentStream.ToArray()); } } } else if (_isNew) { using (var doc = WordprocessingDocument.Create(Filename, WordprocessingDocumentType.Document)) { var mainPart = doc.AddMainDocumentPart(); var document = new Document( new Body()); document.Save(mainPart); //Create the styles part. var styleDefinitionsPart = mainPart.AddNewPart <StyleDefinitionsPart>(); StyleGenerator.DefaultStyle().Save(styleDefinitionsPart); //Close the document doc.Close(); } } using (var doc = WordprocessingDocument.Open(Filename, true)) { try { var mainPart = doc.MainDocumentPart; /* Create the contents */ var body = mainPart.Document.Body; var imageCount = 1; //This is a counter for setting the image number. var listCount = 0; //This is the counter for the number of lists. //Create the list templates var numberingDefinitionsPart = mainPart.NumberingDefinitionsPart ?? mainPart.AddNewPart <NumberingDefinitionsPart>(); var numbering = ListGenerator.CreateNumbering(numberingDefinitionsPart); foreach (var paragraph in Paragraphs) { //If this is an image we also need to pass the MainpArt as this is needed to generate the image part var img = paragraph.Value as Picture; var table = paragraph.Value as Table; var list = paragraph.Value as List; if (img != null) { foreach (var p in img.GetOOXMLParagraph(mainPart, paragraph.Key, ref imageCount)) { body.Append(p); } } else if (table != null) { //Add the table body.Append(table.GetOOXMLTable()); //Add an extra paragraph so two following tables will not be concatenated. body.Append(Paragraph.EmptyParagraph()); } else if (list != null) { //Add the list listCount++; //Add the reference to the list NumberingInstance numberingInstance = new NumberingInstance { NumberID = listCount }; AbstractNumId abstractNumId = new AbstractNumId { Val = list.Type == ListType.Bullet ? 0 : 1 }; numberingInstance.Append(abstractNumId); numbering.Append(numberingInstance); foreach (var item in list.GetOOXMLList(listCount)) { body.Append(item); } } else { //Add the text paragraph(s) foreach (var p in paragraph.Value.GetOOXMLParagraph()) { body.Append(p); } } } //All the paragraphs are done.Save the numbering. numbering.Save(numberingDefinitionsPart); //Replace the custom tags. foreach (var customPart in _customParts) { customPart.Replace(mainPart, ref imageCount); } /* Save the results and close */ mainPart.Document.Save(); doc.Close(); } catch (Exception ex) { throw new FileLoadException("An error occured when saving the document", Filename, ex); } } }