DepartmentProject/DepartmentPortal/Common/ToolsOffice/Implements/WordOpenXML/BuilderWordDocumentOpenXML.cs

56 lines
1.7 KiB
C#
Raw Normal View History

2022-12-17 23:44:29 +04:00
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System.IO;
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;
public override void CreateDocument(ModelWordDocument model)
2022-12-17 23:44:29 +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());
if (doc != null)
2022-12-17 23:44:29 +04:00
{
mainPart.AddParts(doc.WordDocumentParts);
2022-12-17 23:44:29 +04:00
}
}
public override void CreateParagraph(ModelWordParagraph model)
2022-12-17 23:44:29 +04:00
{
_docBody.AddParagraph(model);
2022-12-17 23:44:29 +04:00
}
public override void CreateTable(ModelWordTable model)
2022-12-17 23:44:29 +04:00
{
_docBody.AddTable(model);
2022-12-17 23:44:29 +04:00
}
public override Stream SaveDocument(ModelWordDocument info)
2022-12-17 23:44:29 +04:00
{
_docBody.AddSectionProperties(info);
2022-12-17 23:44:29 +04:00
_wordDocument.MainDocumentPart.Document.Save();
_wordDocument.Close();
return _memoryStream;
}
}
}