2022-12-17 23:44:29 +04:00
|
|
|
|
using DocumentFormat.OpenXml;
|
|
|
|
|
using DocumentFormat.OpenXml.Packaging;
|
|
|
|
|
using DocumentFormat.OpenXml.Wordprocessing;
|
|
|
|
|
using System.IO;
|
2022-12-19 11:10:05 +04:00
|
|
|
|
using ToolsOffice.Implements.WordOpenXML.Extensions;
|
2022-12-17 23:44:29 +04:00
|
|
|
|
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;
|
|
|
|
|
|
2022-12-19 11:10:05 +04:00
|
|
|
|
public override void CreateDocument(ModelWordDocument model)
|
2022-12-17 23:44:29 +04:00
|
|
|
|
{
|
2022-12-19 11:10:05 +04:00
|
|
|
|
var doc = WordCreateDocument.Create(model);
|
|
|
|
|
if (doc == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-12-17 23:44:29 +04:00
|
|
|
|
_memoryStream = new MemoryStream();
|
|
|
|
|
_wordDocument = WordprocessingDocument.Create(_memoryStream, WordprocessingDocumentType.Document);
|
|
|
|
|
var mainPart = _wordDocument.AddMainDocumentPart();
|
|
|
|
|
mainPart.Document = new Document();
|
|
|
|
|
_docBody = mainPart.Document.AppendChild(new Body());
|
2022-12-19 11:10:05 +04:00
|
|
|
|
if (doc != null)
|
2022-12-17 23:44:29 +04:00
|
|
|
|
{
|
2022-12-19 11:10:05 +04:00
|
|
|
|
mainPart.AddParts(doc.WordDocumentParts);
|
2022-12-17 23:44:29 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-19 11:10:05 +04:00
|
|
|
|
public override void CreateParagraph(ModelWordParagraph model)
|
2022-12-17 23:44:29 +04:00
|
|
|
|
{
|
2022-12-19 11:10:05 +04:00
|
|
|
|
_docBody.AddParagraph(model);
|
2022-12-17 23:44:29 +04:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-19 11:10:05 +04:00
|
|
|
|
public override void CreateTable(ModelWordTable model)
|
2022-12-17 23:44:29 +04:00
|
|
|
|
{
|
2022-12-19 11:10:05 +04:00
|
|
|
|
_docBody.AddTable(model);
|
2022-12-17 23:44:29 +04:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-19 11:10:05 +04:00
|
|
|
|
public override Stream SaveDocument(ModelWordDocument info)
|
2022-12-17 23:44:29 +04:00
|
|
|
|
{
|
2022-12-19 11:10:05 +04:00
|
|
|
|
_docBody.AddSectionProperties(info);
|
2022-12-17 23:44:29 +04:00
|
|
|
|
_wordDocument.MainDocumentPart.Document.Save();
|
|
|
|
|
_wordDocument.Close();
|
|
|
|
|
return _memoryStream;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|