более приятный на вид док

This commit is contained in:
the 2024-06-25 09:00:46 +04:00
parent 17403f4ce9
commit a1575b150d
3 changed files with 43 additions and 30 deletions

View File

@ -62,7 +62,7 @@ namespace BusinessLogic.BusinessLogic
_saveToPdf.CreateDoc(new PdfInfo
{
FileName = model.FileName,
Title = "Отчёт о поставке",
Title = "Накладная",
Date = model.Date!.Value,
Supply = _supplyStorage.GetElement(new SupplySearchModel()
{

View File

@ -11,30 +11,55 @@ namespace BusinessLogic.OfficePackage
{
public abstract class AbstractSaveToPdf
{
public BarcodeLogic BarcodeLogic { get; set; } = new BarcodeLogic();
public void CreateDoc(PdfInfo info)
{
CreatePdf(info);
CreateParagraph(new PdfParagraph
{
Text = info.Title,
Text = $"{info.Title}\nОт {DateTime.UtcNow}",
Style = "NormalTitle",
ParagraphAlignment = PdfParagraphAlignmentType.Center
});
CreateTable(new List<string> { "2cm", "3cm", "6cm", "4cm" });
CreateParagraph(new PdfParagraph
{
Text = $"Поставщик: {info.Supply.SupplierName}\nДата создания поставки: {info.Supply.Date}\nСтатус: {info.Supply.Status}",
Style = "NormalTitle",
ParagraphAlignment = PdfParagraphAlignmentType.Right
});
CreateTable(new List<string> { "5cm", "10cm"});
CreateRow(new PdfRowParameters
{
Texts = new List<string> { "Идентификатор", "Дата поставки", "Информация", "Статус" },
Texts = new List<string> { "Id", "Информация" },
Style = "NormalTitle",
ParagraphAlignment = PdfParagraphAlignmentType.Center
});
CreateRow(new PdfRowParameters
{
Texts = new List<string> { info.Supply.Id.ToString(), info.Supply.Date.ToShortDateString(), info.Supply.Name, info.Supply.Status.ToString() },
Texts = new List<string> { info.Supply.Id.ToString(), info.Supply.Name, },
Style = "Normal",
ParagraphAlignment = PdfParagraphAlignmentType.Left
});
CreateParagraph(new PdfParagraph
{
Text = "Товары",
Style = "NormalTitle",
ParagraphAlignment = PdfParagraphAlignmentType.Center
});
CreateTable(new List<string> { "5cm", "5cm", "3cm", "2cm" });
CreateRow(new PdfRowParameters
{
Texts = new List<string> { "Id", "Название", "Цена", "Кол-во" },
Style = "NormalTitle",
ParagraphAlignment = PdfParagraphAlignmentType.Center
});
foreach (var product in info.Supply.Products.Values)
{
CreateRow(new PdfRowParameters
{
Texts = new List<string> { product.Item1.Id.ToString(), product.Item1.Name, product.Item1.Price.ToString(), product.Item2.ToString() },
});
}
CreateParagraph(new PdfParagraph
{
Text = $"Итого: {info.Supply.Price}",
Style = "Normal",
@ -42,32 +67,11 @@ namespace BusinessLogic.OfficePackage
});
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 CreateImage(string path);
protected abstract void SavePdf(PdfInfo info);
}
}

View File

@ -1,6 +1,7 @@
using BusinessLogic.OfficePackage.HelperEnums;
using BusinessLogic.OfficePackage.HelperModels;
using MigraDoc.DocumentObjectModel;
using MigraDoc.DocumentObjectModel.Shapes;
using MigraDoc.DocumentObjectModel.Tables;
using MigraDoc.Rendering;
using System.Text;
@ -12,6 +13,7 @@ namespace BusinessLogic.OfficePackage.Implements
private Document? _document;
private Section? _section;
private Table? _table;
private Image _image;
private static ParagraphAlignment
GetParagraphAlignment(PdfParagraphAlignmentType type)
{
@ -49,8 +51,7 @@ namespace BusinessLogic.OfficePackage.Implements
}
var paragraph = _section.AddParagraph(pdfParagraph.Text);
paragraph.Format.SpaceAfter = "1cm";
paragraph.Format.Alignment =
GetParagraphAlignment(pdfParagraph.ParagraphAlignment);
paragraph.Format.Alignment = GetParagraphAlignment(pdfParagraph.ParagraphAlignment);
paragraph.Style = pdfParagraph.Style;
}
protected override void CreateTable(List<string> columns)
@ -89,6 +90,14 @@ namespace BusinessLogic.OfficePackage.Implements
row.Cells[i].VerticalAlignment = VerticalAlignment.Center;
}
}
protected override void CreateImage(string path)
{
if (_document == null)
{
return;
}
_document.LastSection.AddImage(path);
}
protected override void SavePdf(PdfInfo info)
{
var renderer = new PdfDocumentRenderer(true)