This commit is contained in:
parent
8924845fed
commit
68b08407ce
@ -12,6 +12,7 @@
|
|||||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
|
||||||
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.1" />
|
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.1" />
|
||||||
|
<PackageReference Include="PdfSharp.MigraDoc.Standard" Version="1.51.15" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -0,0 +1,80 @@
|
|||||||
|
using FurnitureAssemblyBusinessLogic.OfficePackage.HelperEnums;
|
||||||
|
using FurnitureAssemblyBusinessLogic.OfficePackage.HelperModels;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace FurnitureAssemblyBusinessLogic.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<string> { "2cm", "3cm", "6cm", "3cm" });
|
||||||
|
CreateRow(new PdfRowParameters
|
||||||
|
{
|
||||||
|
Texts = new List<string> { "Номер", "Дата заказа", "Изделие", "Сумма" },
|
||||||
|
Style = "NormalTitle",
|
||||||
|
ParagraphAlignment = PdfParagraphAlignmentType.Center
|
||||||
|
});
|
||||||
|
foreach (var order in info.Orders)
|
||||||
|
{
|
||||||
|
CreateRow(new PdfRowParameters
|
||||||
|
{
|
||||||
|
Texts = new List<string> { order.Id.ToString(), order.DateCreate.ToShortDateString(), order.FurnitureName, 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);
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Создание doc-файла
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="info"></param>
|
||||||
|
protected abstract void CreatePdf(PdfInfo info);
|
||||||
|
/// <summary>
|
||||||
|
/// Создание параграфа с текстом
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="title"></param>
|
||||||
|
/// <param name="style"></param>
|
||||||
|
protected abstract void CreateParagraph(PdfParagraph paragraph);
|
||||||
|
/// <summary>
|
||||||
|
/// Создание таблицы
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="title"></param>
|
||||||
|
/// <param name="style"></param>
|
||||||
|
protected abstract void CreateTable(List<string> columns);
|
||||||
|
/// <summary>
|
||||||
|
/// Создание и заполнение строки
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="rowParameters"></param>
|
||||||
|
protected abstract void CreateRow(PdfRowParameters rowParameters);
|
||||||
|
/// <summary>
|
||||||
|
/// Сохранение файла
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="info"></param>
|
||||||
|
protected abstract void SavePdf(PdfInfo info);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace FurnitureAssemblyBusinessLogic.OfficePackage.HelperEnums
|
||||||
|
{
|
||||||
|
public enum PdfParagraphAlignmentType
|
||||||
|
{
|
||||||
|
Center,
|
||||||
|
Left,
|
||||||
|
Right
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
using FurnitureAssemblyContracts.ViewModels;
|
||||||
|
|
||||||
|
|
||||||
|
namespace FurnitureAssemblyBusinessLogic.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<ReportOrdersViewModel> Orders { get; set; } = new();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
using FurnitureAssemblyBusinessLogic.OfficePackage.HelperEnums;
|
||||||
|
|
||||||
|
namespace FurnitureAssemblyBusinessLogic.OfficePackage.HelperModels
|
||||||
|
{
|
||||||
|
public class PdfParagraph
|
||||||
|
{
|
||||||
|
public string Text { get; set; } = string.Empty;
|
||||||
|
public string Style { get; set; } = string.Empty;
|
||||||
|
public PdfParagraphAlignmentType ParagraphAlignment { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
using FurnitureAssemblyBusinessLogic.OfficePackage.HelperEnums;
|
||||||
|
|
||||||
|
namespace FurnitureAssemblyBusinessLogic.OfficePackage.HelperModels
|
||||||
|
{
|
||||||
|
public class PdfRowParameters
|
||||||
|
{
|
||||||
|
public List<string> Texts { get; set; } = new();
|
||||||
|
public string Style { get; set; } = string.Empty;
|
||||||
|
public PdfParagraphAlignmentType ParagraphAlignment { get; set; }
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -277,8 +277,7 @@ namespace FurnitureAssemblyBusinessLogic.OfficePackage.Implements
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Все ячейки должны быть последовательно друг за другом
|
// Все ячейки должны быть последовательно друг за другом расположены
|
||||||
расположены
|
|
||||||
// нужно определить, после какой вставлять
|
// нужно определить, после какой вставлять
|
||||||
Cell? refCell = null;
|
Cell? refCell = null;
|
||||||
foreach (Cell rowCell in row.Elements<Cell>())
|
foreach (Cell rowCell in row.Elements<Cell>())
|
||||||
|
@ -0,0 +1,100 @@
|
|||||||
|
using FurnitureAssemblyBusinessLogic.OfficePackage.HelperEnums;
|
||||||
|
using FurnitureAssemblyBusinessLogic.OfficePackage.HelperModels;
|
||||||
|
using MigraDoc.DocumentObjectModel;
|
||||||
|
using MigraDoc.DocumentObjectModel.Tables;
|
||||||
|
using MigraDoc.Rendering;
|
||||||
|
|
||||||
|
namespace FurnitureAssemblyBusinessLogic.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.Right => ParagraphAlignment.Right,
|
||||||
|
_ => ParagraphAlignment.Justify,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Создание стилей для документа
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="document"></param>
|
||||||
|
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<string> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user