From 311ef7245c7e827d6f76e22664b54c1edf61c6ad Mon Sep 17 00:00:00 2001 From: ekallin Date: Sun, 24 Mar 2024 00:59:16 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D1=8B=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D0=BF=D0=B4=D1=84,=20=D0=B2=D0=BE=D1=80=D0=B4?= =?UTF-8?q?=20(=D0=BF=D0=BE=D1=84=D0=B8=D0=BA=D1=81=D0=B8=D0=BB=D0=B0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../OfficePackage/AbstractSaveToPdf.cs | 83 ++++++++++++ .../HelperEnums/PdfParagraphAlignmentType.cs | 15 +++ .../OfficePackage/HelperModels/PdfInfo.cs | 19 +++ .../HelperModels/PdfParagraph.cs | 17 +++ .../HelperModels/PdfRowParameters.cs | 16 +++ .../HelperModels/WordParagraph.cs | 8 +- .../HelperModels/WordTextProperties.cs | 3 +- .../OfficePackage/Implements/SaveToPdf.cs | 99 ++++++++++++++ .../OfficePackage/Implements/SaveToWord.cs | 121 ++++++++++++++++++ 9 files changed, 379 insertions(+), 2 deletions(-) create mode 100644 SushiBarBusinessLogic/OfficePackage/AbstractSaveToPdf.cs create mode 100644 SushiBarBusinessLogic/OfficePackage/HelperEnums/PdfParagraphAlignmentType.cs create mode 100644 SushiBarBusinessLogic/OfficePackage/HelperModels/PdfInfo.cs create mode 100644 SushiBarBusinessLogic/OfficePackage/HelperModels/PdfParagraph.cs create mode 100644 SushiBarBusinessLogic/OfficePackage/HelperModels/PdfRowParameters.cs create mode 100644 SushiBarBusinessLogic/OfficePackage/Implements/SaveToPdf.cs create mode 100644 SushiBarBusinessLogic/OfficePackage/Implements/SaveToWord.cs diff --git a/SushiBarBusinessLogic/OfficePackage/AbstractSaveToPdf.cs b/SushiBarBusinessLogic/OfficePackage/AbstractSaveToPdf.cs new file mode 100644 index 0000000..cba331a --- /dev/null +++ b/SushiBarBusinessLogic/OfficePackage/AbstractSaveToPdf.cs @@ -0,0 +1,83 @@ +using SushiBarBusinessLogic.OfficePackage.HelperEnums; +using SushiBarBusinessLogic.OfficePackage.HelperModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SushiBarBusinessLogic.OfficePackage +{ + public abstract class AbstractSaveToPdf + { + public void CreateDoc(PdfInfo info) + { + CreatePdf(info); + CreateParagraph(new PdfParagraph + { + Text = info.Title, + Style = + "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + CreateParagraph(new PdfParagraph + { + Text = $"с { info.DateFrom.ToShortDateString() } по { info.DateTo.ToShortDateString() }", Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + CreateTable(new List { "2cm", "3cm", "6cm", "3cm" }); + CreateRow(new PdfRowParameters + { + Texts = new List { "Номер", "Дата заказа", "Изделие", "Сумма" }, + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + foreach (var order in info.Orders) + { + CreateRow(new PdfRowParameters + { + Texts = new List { order.Id.ToString(), + order.DateCreate.ToShortDateString(), + order.SushiName, + order.Sum.ToString() }, + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Left + }); + } + CreateParagraph(new PdfParagraph + { + Text = $"Итого: {info.Orders.Sum(x => x.Sum)}\t", + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Rigth + }); + SavePdf(info); + } + /// + /// Создание doc-файла + /// + /// + protected abstract void CreatePdf(PdfInfo info); + /// + /// Создание параграфа с текстом + /// + /// + /// + protected abstract void CreateParagraph(PdfParagraph paragraph); + /// + /// Создание таблицы + /// + /// + /// + protected abstract void CreateTable(List columns); + /// + /// Создание и заполнение строки + /// + /// + protected abstract void CreateRow(PdfRowParameters rowParameters); + /// + /// Сохранение файла + /// + /// + protected abstract void SavePdf(PdfInfo info); + } +} diff --git a/SushiBarBusinessLogic/OfficePackage/HelperEnums/PdfParagraphAlignmentType.cs b/SushiBarBusinessLogic/OfficePackage/HelperEnums/PdfParagraphAlignmentType.cs new file mode 100644 index 0000000..375e74d --- /dev/null +++ b/SushiBarBusinessLogic/OfficePackage/HelperEnums/PdfParagraphAlignmentType.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SushiBarBusinessLogic.OfficePackage.HelperEnums +{ + public enum PdfParagraphAlignmentType + { + Center, + Left, + Rigth + } +} \ No newline at end of file diff --git a/SushiBarBusinessLogic/OfficePackage/HelperModels/PdfInfo.cs b/SushiBarBusinessLogic/OfficePackage/HelperModels/PdfInfo.cs new file mode 100644 index 0000000..b59261e --- /dev/null +++ b/SushiBarBusinessLogic/OfficePackage/HelperModels/PdfInfo.cs @@ -0,0 +1,19 @@ +using SushiBarContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SushiBarBusinessLogic.OfficePackage.HelperModels +{ + public class PdfInfo + { + public string FileName { get; set; } = string.Empty; + public string Title { get; set; } = string.Empty; + public DateTime DateFrom { get; set; } + public DateTime DateTo { get; set; } + public List Orders { get; set; } = new(); + + } +} diff --git a/SushiBarBusinessLogic/OfficePackage/HelperModels/PdfParagraph.cs b/SushiBarBusinessLogic/OfficePackage/HelperModels/PdfParagraph.cs new file mode 100644 index 0000000..4b588c7 --- /dev/null +++ b/SushiBarBusinessLogic/OfficePackage/HelperModels/PdfParagraph.cs @@ -0,0 +1,17 @@ +using SushiBarBusinessLogic.OfficePackage.HelperEnums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SushiBarBusinessLogic.OfficePackage.HelperModels +{ + public class PdfParagraph + { + public string Text { get; set; } = string.Empty; + public string Style { get; set; } = string.Empty; + public PdfParagraphAlignmentType ParagraphAlignment { get; set; } + + } +} diff --git a/SushiBarBusinessLogic/OfficePackage/HelperModels/PdfRowParameters.cs b/SushiBarBusinessLogic/OfficePackage/HelperModels/PdfRowParameters.cs new file mode 100644 index 0000000..ddf051f --- /dev/null +++ b/SushiBarBusinessLogic/OfficePackage/HelperModels/PdfRowParameters.cs @@ -0,0 +1,16 @@ +using SushiBarBusinessLogic.OfficePackage.HelperEnums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SushiBarBusinessLogic.OfficePackage.HelperModels +{ + public class PdfRowParameters + { + public List Texts { get; set; } = new(); + public string Style { get; set; } = string.Empty; + public PdfParagraphAlignmentType ParagraphAlignment { get; set; } + } +} diff --git a/SushiBarBusinessLogic/OfficePackage/HelperModels/WordParagraph.cs b/SushiBarBusinessLogic/OfficePackage/HelperModels/WordParagraph.cs index 8b322ab..e6e48c1 100644 --- a/SushiBarBusinessLogic/OfficePackage/HelperModels/WordParagraph.cs +++ b/SushiBarBusinessLogic/OfficePackage/HelperModels/WordParagraph.cs @@ -1,4 +1,10 @@ -namespace SushiBarBusinessLogic.OfficePackage.HelperModels +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SushiBarBusinessLogic.OfficePackage.HelperModels { public class WordParagraph { diff --git a/SushiBarBusinessLogic/OfficePackage/HelperModels/WordTextProperties.cs b/SushiBarBusinessLogic/OfficePackage/HelperModels/WordTextProperties.cs index 06e58ef..ed56373 100644 --- a/SushiBarBusinessLogic/OfficePackage/HelperModels/WordTextProperties.cs +++ b/SushiBarBusinessLogic/OfficePackage/HelperModels/WordTextProperties.cs @@ -2,10 +2,11 @@ namespace SushiBarBusinessLogic.OfficePackage.HelperModels { - public class WotdTextProperties + public class WordTextProperties { public string Size { get; set; } = string.Empty; public bool Bold { get; set; } public WordJustificationType JustificationType { get; set; } + } } diff --git a/SushiBarBusinessLogic/OfficePackage/Implements/SaveToPdf.cs b/SushiBarBusinessLogic/OfficePackage/Implements/SaveToPdf.cs new file mode 100644 index 0000000..634e453 --- /dev/null +++ b/SushiBarBusinessLogic/OfficePackage/Implements/SaveToPdf.cs @@ -0,0 +1,99 @@ +using MigraDoc.DocumentObjectModel; +using MigraDoc.Rendering; +using SushiBarBusinessLogic.OfficePackage.HelperEnums; +using SushiBarBusinessLogic.OfficePackage.HelperModels; +using MigraDoc.DocumentObjectModel.Tables; + +namespace SushiBarBusinessLogic.OfficePackage.Implements +{ + public class SaveToPdf : AbstractSaveToPdf + { + private Document? _document; + private Section? _section; + private Table? _table; + private static ParagraphAlignment + GetParagraphAlignment(PdfParagraphAlignmentType type) + { + return type switch + { + PdfParagraphAlignmentType.Center => ParagraphAlignment.Center, + PdfParagraphAlignmentType.Left => ParagraphAlignment.Left, + PdfParagraphAlignmentType.Rigth => ParagraphAlignment.Right, + _ => ParagraphAlignment.Justify, + }; + } + /// + /// Создание стилей для документа + /// + /// + private static void DefineStyles(Document document) + { + var style = document.Styles["Normal"]; + style.Font.Name = "Times New Roman"; + style.Font.Size = 14; + style = document.Styles.AddStyle("NormalTitle", "Normal"); + style.Font.Bold = true; + } + protected override void CreatePdf(PdfInfo info) + { + _document = new Document(); + DefineStyles(_document); + _section = _document.AddSection(); + } + protected override void CreateParagraph(PdfParagraph pdfParagraph) + { + if (_section == null) + { + return; + } + var paragraph = _section.AddParagraph(pdfParagraph.Text); + paragraph.Format.SpaceAfter = "1cm"; + paragraph.Format.Alignment = GetParagraphAlignment(pdfParagraph.ParagraphAlignment); + paragraph.Style = pdfParagraph.Style; + } + protected override void CreateTable(List columns) + { + if (_document == null) + { + return; + } + _table = _document.LastSection.AddTable(); + foreach (var elem in columns) + { + _table.AddColumn(elem); + } + } + protected override void CreateRow(PdfRowParameters rowParameters) + { + if (_table == null) + { + return; + } + var row = _table.AddRow(); + for (int i = 0; i < rowParameters.Texts.Count; ++i) + { + row.Cells[i].AddParagraph(rowParameters.Texts[i]); + if (!string.IsNullOrEmpty(rowParameters.Style)) + { + row.Cells[i].Style = rowParameters.Style; + } + Unit borderWidth = 0.5; + row.Cells[i].Borders.Left.Width = borderWidth; + row.Cells[i].Borders.Right.Width = borderWidth; + row.Cells[i].Borders.Top.Width = borderWidth; + row.Cells[i].Borders.Bottom.Width = borderWidth; + row.Cells[i].Format.Alignment = GetParagraphAlignment(rowParameters.ParagraphAlignment); + row.Cells[i].VerticalAlignment = VerticalAlignment.Center; + } + } + protected override void SavePdf(PdfInfo info) + { + var renderer = new PdfDocumentRenderer(true) + { + Document = _document + }; + renderer.RenderDocument(); + renderer.PdfDocument.Save(info.FileName); + } + } +} diff --git a/SushiBarBusinessLogic/OfficePackage/Implements/SaveToWord.cs b/SushiBarBusinessLogic/OfficePackage/Implements/SaveToWord.cs new file mode 100644 index 0000000..6c1fc93 --- /dev/null +++ b/SushiBarBusinessLogic/OfficePackage/Implements/SaveToWord.cs @@ -0,0 +1,121 @@ +using SushiBarBusinessLogic.OfficePackage.HelperEnums; +using SushiBarBusinessLogic.OfficePackage.HelperModels; +using DocumentFormat.OpenXml; +using DocumentFormat.OpenXml.Packaging; +using DocumentFormat.OpenXml.Wordprocessing; + + +namespace SushiBarBusinessLogic.OfficePackage.Implements +{ + public class SaveToWord : AbstractSaveToWord + { + private WordprocessingDocument? _wordDocument; + private Body? _docBody; + /// + /// Получение типа выравнивания + /// + /// + /// + private static JustificationValues + GetJustificationValues(WordJustificationType type) + { + return type switch + { + WordJustificationType.Both => JustificationValues.Both, + WordJustificationType.Center => JustificationValues.Center, + _ => JustificationValues.Left, + }; + } + /// + /// Настройки страницы + /// + /// + private static SectionProperties CreateSectionProperties() + { + var properties = new SectionProperties(); + var pageSize = new PageSize + { + Orient = PageOrientationValues.Portrait + }; + properties.AppendChild(pageSize); + return properties; + } + /// + /// Задание форматирования для абзаца + /// + /// + /// + private static ParagraphProperties? CreateParagraphProperties(WordTextProperties? paragraphProperties) + { + if (paragraphProperties == null) + { + return null; + } + var properties = new ParagraphProperties(); + properties.AppendChild(new Justification() + { + Val = GetJustificationValues(paragraphProperties.JustificationType) + }); + properties.AppendChild(new SpacingBetweenLines + { + LineRule = LineSpacingRuleValues.Auto + }); + properties.AppendChild(new Indentation()); + var paragraphMarkRunProperties = new ParagraphMarkRunProperties(); + if (!string.IsNullOrEmpty(paragraphProperties.Size)) + { + paragraphMarkRunProperties.AppendChild(new FontSize + { + Val = paragraphProperties.Size + }); + } + properties.AppendChild(paragraphMarkRunProperties); + return properties; + } + protected override void CreateWord(WordInfo info) + { + _wordDocument = WordprocessingDocument.Create(info.FileName, WordprocessingDocumentType.Document); + MainDocumentPart mainPart = _wordDocument.AddMainDocumentPart(); + mainPart.Document = new Document(); + _docBody = mainPart.Document.AppendChild(new Body()); + } + protected override void CreateParagraph(WordParagraph paragraph) + { + if (_docBody == null || paragraph == null) + { + return; + } + var docParagraph = new Paragraph(); + + docParagraph.AppendChild(CreateParagraphProperties(paragraph.TextProperties)); + foreach (var run in paragraph.Texts) + { + var docRun = new Run(); + var properties = new RunProperties(); + properties.AppendChild(new FontSize { Val = run.Item2.Size }); + if (run.Item2.Bold) + { + properties.AppendChild(new Bold()); + } + docRun.AppendChild(properties); + docRun.AppendChild(new Text + { + Text = run.Item1, + Space = SpaceProcessingModeValues.Preserve + }); + docParagraph.AppendChild(docRun); + } + _docBody.AppendChild(docParagraph); + } + protected override void SaveWord(WordInfo info) + { + if (_docBody == null || _wordDocument == null) + { + return; + } + _docBody.AppendChild(CreateSectionProperties()); + _wordDocument.MainDocumentPart!.Document.Save(); + _wordDocument.Dispose(); + } + } +}