using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; using System.IO; using ToolsOffice.Implements.WordOpenXML.Extenstions; using ToolsOffice.Implements.WordOpenXML.Models; using ToolsOffice.Interfaces.Word; using ToolsOffice.Interfaces.Word.Models; namespace ToolsOffice.Implements.WordOpenXML { public class BuilderWordDocumentOpenXML : BuilderWordDocument { private WordprocessingDocument _wordDocument; private Body _docBody; private MemoryStream _memoryStream; public override void CreateDocument(ICreateWordModel model) { _memoryStream = new MemoryStream(); _wordDocument = WordprocessingDocument.Create(_memoryStream, WordprocessingDocumentType.Document); var mainPart = _wordDocument.AddMainDocumentPart(); mainPart.Document = new Document(); _docBody = mainPart.Document.AppendChild(new Body()); if (model is WordCreateDocument document) { mainPart.AddParts(document.WordDocumentParts); } } public override void CreateParagraph(IParagraphWordModel model) { if (model is WordParagraph paragraph) { var docParagraph = new Paragraph(); docParagraph.AddParagraphProperties(paragraph.ParagraphProperties); foreach (var run in paragraph.Texts) { docParagraph.AddRun(run); } _docBody.AppendChild(docParagraph); } } public override void CreateTable(ITableWordModel model) { if (model is WordTable table) { var docTable = new Table(); docTable.AddTableProperties(table.TableProperties); docTable.AddGridColumn(table.TableGrid); foreach (var row in table.Rows) { docTable.AddTableRow(row); } _docBody.AppendChild(docTable); } } public override Stream SaveDocument(ISaveWordModel info) { if (info is WordSaveDocument document) { _docBody.AddSectionProperties(document); } _wordDocument.MainDocumentPart.Document.Save(); _wordDocument.Close(); return _memoryStream; } } }