CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/Implements/SaveToPdfClient.cs
Илья Федотов 93e0fe9524 Pdf Report Client
2024-07-23 20:29:10 +04:00

99 lines
3.2 KiB
C#

using DocumentFormat.OpenXml.Bibliography;
using ElectronicsShopBusinessLogic.OfficePackage.HelperEnums;
using ElectronicsShopBusinessLogic.OfficePackage.HelperModels;
using MigraDoc.DocumentObjectModel;
using MigraDoc.DocumentObjectModel.Tables;
using MigraDoc.Rendering;
using PdfSharp.Pdf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ElectronicsShopBusinessLogic.OfficePackage.Implements {
public class SaveToPdfClient : AbstractSaveToPdfClient {
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 CreateParagraph(PdfParagraph pdfParagraph) {
if (_section == null) {
return;
}
var paragraph = _section.AddParagraph(pdfParagraph.Text);
paragraph.Format.SpaceAfter = "1cm";
paragraph.Format.Alignment = GetParagraphAlignment(pdfParagraph.alignmentType);
paragraph.Style = pdfParagraph.Style;
}
protected override void CreateRow(PdfRowParameters rowParameters) {
if (_table == null) {
return;
}
var row = _table.AddRow();
for (int i = 0; i < rowParameters.Text.Count; ++i) {
// заполнение ячейки (добавление параграфа в ячейку)
row.Cells[i].AddParagraph(rowParameters.Text[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.alignmentType);
row.Cells[i].VerticalAlignment = VerticalAlignment.Center;
}
}
protected override void CreateTable(List<string> columns) {
if (_document == null) {
return;
}
_table = _document.LastSection.AddTable();
foreach (var elem in columns) {
_table.AddColumn(elem);
}
}
protected override void CretePdf(PdfInfoClient info) {
_document = new Document();
DefineStyles(_document);
// Ссылка нп первую секцию
_section = _document.AddSection();
}
protected override PdfDocument SavePdf(PdfInfoClient info) {
var renderer = new PdfDocumentRenderer(true) {
Document = _document,
};
renderer.RenderDocument();
renderer.PdfDocument.Save(info.FileName);
return renderer.PdfDocument;
}
}
}