From 8212ec648a352cd77645fe6ede90a77b8fd20cb2 Mon Sep 17 00:00:00 2001 From: malimova Date: Thu, 11 Apr 2024 23:54:48 +0400 Subject: [PATCH] Pdf- all done --- .../AbstractSaveToPdf.cs | 79 ++++++++++++++ .../ConfectioneryBusinessLogic.csproj | 3 + .../ConfectioneryBusinessLogic/PdfInfo.cs | 19 ++++ .../PdfParagraph.cs | 18 ++++ .../PdfParagraphAlignmentType.cs | 15 +++ .../PdfRowParameters.cs | 16 +++ .../ConfectioneryBusinessLogic/SaveToPdf.cs | 102 ++++++++++++++++++ 7 files changed, 252 insertions(+) create mode 100644 Confectionery/ConfectioneryBusinessLogic/AbstractSaveToPdf.cs create mode 100644 Confectionery/ConfectioneryBusinessLogic/PdfInfo.cs create mode 100644 Confectionery/ConfectioneryBusinessLogic/PdfParagraph.cs create mode 100644 Confectionery/ConfectioneryBusinessLogic/PdfParagraphAlignmentType.cs create mode 100644 Confectionery/ConfectioneryBusinessLogic/PdfRowParameters.cs create mode 100644 Confectionery/ConfectioneryBusinessLogic/SaveToPdf.cs diff --git a/Confectionery/ConfectioneryBusinessLogic/AbstractSaveToPdf.cs b/Confectionery/ConfectioneryBusinessLogic/AbstractSaveToPdf.cs new file mode 100644 index 0000000..050c2eb --- /dev/null +++ b/Confectionery/ConfectioneryBusinessLogic/AbstractSaveToPdf.cs @@ -0,0 +1,79 @@ +using ConfectioneryBusinessLogic.OfficePackage.HelperEnums; +using ConfectioneryBusinessLogic.OfficePackage.HelperModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryBusinessLogic +{ + 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.PastryName, order.Sum.ToString() }, + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Left + }); + } + CreateParagraph(new PdfParagraph + { + Text = $"Итого: {info.Orders.Sum(x => x.Sum)}\t", + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Right + }); + 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/Confectionery/ConfectioneryBusinessLogic/ConfectioneryBusinessLogic.csproj b/Confectionery/ConfectioneryBusinessLogic/ConfectioneryBusinessLogic.csproj index 50558a1..eec37d3 100644 --- a/Confectionery/ConfectioneryBusinessLogic/ConfectioneryBusinessLogic.csproj +++ b/Confectionery/ConfectioneryBusinessLogic/ConfectioneryBusinessLogic.csproj @@ -9,7 +9,10 @@ + + + diff --git a/Confectionery/ConfectioneryBusinessLogic/PdfInfo.cs b/Confectionery/ConfectioneryBusinessLogic/PdfInfo.cs new file mode 100644 index 0000000..1087926 --- /dev/null +++ b/Confectionery/ConfectioneryBusinessLogic/PdfInfo.cs @@ -0,0 +1,19 @@ +using ConfectioneryContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryBusinessLogic.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/Confectionery/ConfectioneryBusinessLogic/PdfParagraph.cs b/Confectionery/ConfectioneryBusinessLogic/PdfParagraph.cs new file mode 100644 index 0000000..471c064 --- /dev/null +++ b/Confectionery/ConfectioneryBusinessLogic/PdfParagraph.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ConfectioneryBusinessLogic.OfficePackage.HelperEnums; + + +namespace ConfectioneryBusinessLogic.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/Confectionery/ConfectioneryBusinessLogic/PdfParagraphAlignmentType.cs b/Confectionery/ConfectioneryBusinessLogic/PdfParagraphAlignmentType.cs new file mode 100644 index 0000000..f0a8f4f --- /dev/null +++ b/Confectionery/ConfectioneryBusinessLogic/PdfParagraphAlignmentType.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryBusinessLogic.OfficePackage.HelperEnums +{ + public enum PdfParagraphAlignmentType + { + Center, + Left, + Right + } +} diff --git a/Confectionery/ConfectioneryBusinessLogic/PdfRowParameters.cs b/Confectionery/ConfectioneryBusinessLogic/PdfRowParameters.cs new file mode 100644 index 0000000..d0ec0ae --- /dev/null +++ b/Confectionery/ConfectioneryBusinessLogic/PdfRowParameters.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ConfectioneryBusinessLogic.OfficePackage.HelperEnums; + +namespace ConfectioneryBusinessLogic.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/Confectionery/ConfectioneryBusinessLogic/SaveToPdf.cs b/Confectionery/ConfectioneryBusinessLogic/SaveToPdf.cs new file mode 100644 index 0000000..c9a3b3c --- /dev/null +++ b/Confectionery/ConfectioneryBusinessLogic/SaveToPdf.cs @@ -0,0 +1,102 @@ +using MigraDoc.DocumentObjectModel; +using MigraDoc.DocumentObjectModel.Tables; +using MigraDoc.Rendering; +using ConfectioneryBusinessLogic.OfficePackage.HelperEnums; +using ConfectioneryBusinessLogic.OfficePackage.HelperModels; + + +namespace ConfectioneryBusinessLogic +{ + 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.Right => 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); + } + } +}