From 8b9b4146cca5dd5269c7e9dbc856e4a6fa199be1 Mon Sep 17 00:00:00 2001 From: VictoriaPresnyakova Date: Mon, 20 Mar 2023 14:04:05 +0400 Subject: [PATCH] pdf --- .../JewelryStoreBusinessLogic.csproj | 1 + .../OfficePackage/AbstractSaveToPdf.cs | 3 +- .../OfficePackage/Implements/SaveToPdf.cs | 107 ++++++++++++++++++ 3 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 JewelryStoreBusinessLogic/OfficePackage/Implements/SaveToPdf.cs diff --git a/JewelryStoreBusinessLogic/JewelryStoreBusinessLogic.csproj b/JewelryStoreBusinessLogic/JewelryStoreBusinessLogic.csproj index 3735375..f071426 100644 --- a/JewelryStoreBusinessLogic/JewelryStoreBusinessLogic.csproj +++ b/JewelryStoreBusinessLogic/JewelryStoreBusinessLogic.csproj @@ -17,6 +17,7 @@ + diff --git a/JewelryStoreBusinessLogic/OfficePackage/AbstractSaveToPdf.cs b/JewelryStoreBusinessLogic/OfficePackage/AbstractSaveToPdf.cs index 8e42900..4a4beba 100644 --- a/JewelryStoreBusinessLogic/OfficePackage/AbstractSaveToPdf.cs +++ b/JewelryStoreBusinessLogic/OfficePackage/AbstractSaveToPdf.cs @@ -1,5 +1,4 @@ -using DocumentFormat.OpenXml.Drawing; -using JewelryStoreBusinessLogic.OfficePackage.HelperEnums; +using JewelryStoreBusinessLogic.OfficePackage.HelperEnums; using JewelryStoreBusinessLogic.OfficePackage.HelperModels; using System; using System.Collections.Generic; diff --git a/JewelryStoreBusinessLogic/OfficePackage/Implements/SaveToPdf.cs b/JewelryStoreBusinessLogic/OfficePackage/Implements/SaveToPdf.cs new file mode 100644 index 0000000..e93e65f --- /dev/null +++ b/JewelryStoreBusinessLogic/OfficePackage/Implements/SaveToPdf.cs @@ -0,0 +1,107 @@ +using JewelryStoreBusinessLogic.OfficePackage.HelperEnums; +using JewelryStoreBusinessLogic.OfficePackage.HelperModels; +using MigraDoc.DocumentObjectModel; +using MigraDoc.DocumentObjectModel.Tables; +using MigraDoc.Rendering; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreBusinessLogic.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); + } + + } +}