PIbd-22-Stroev-V.M.-Plumbin.../PlumbingRepair/PlumbingRepairBusinessLogic/OfficePackage/Implements/SaveToPdf.cs

114 lines
3.7 KiB
C#
Raw Normal View History

2024-03-27 11:04:28 +04:00
using PlumbingRepairBusinessLogic.OfficePackage.HelperEnum;
using PlumbingRepairBusinessLogic.OfficePackage.HelperModels;
using MigraDoc.DocumentObjectModel;
using MigraDoc.DocumentObjectModel.Tables;
using MigraDoc.Rendering;
namespace PlumbingRepairBusinessLogic.OfficePackage.Implements
{
public class SaveToPdf : AbstractSaveToPdf
{
private Document? _document;
2024-04-10 01:01:51 +04:00
2024-03-27 11:04:28 +04:00
private Section? _section;
2024-04-10 01:01:51 +04:00
2024-03-27 11:04:28 +04:00
private Table? _table;
2024-04-10 01:01:51 +04:00
2024-03-27 11:04:28 +04:00
private static ParagraphAlignment GetParagraphAlignment(PdfParagraphAlignmentType type)
{
return type switch
{
PdfParagraphAlignmentType.Center => ParagraphAlignment.Center,
PdfParagraphAlignmentType.Left => ParagraphAlignment.Left,
2024-04-10 01:01:51 +04:00
PdfParagraphAlignmentType.Right => ParagraphAlignment.Right,
2024-03-27 11:04:28 +04:00
_ => ParagraphAlignment.Justify,
};
}
2024-04-10 01:01:51 +04:00
/// <summary>
/// Создание стилей для документа
/// </summary>
/// <param name="document"></param>
2024-03-27 11:04:28 +04:00
private static void DefineStyles(Document document)
{
var style = document.Styles["Normal"];
style.Font.Name = "Times New Roman";
style.Font.Size = 14;
2024-04-10 01:01:51 +04:00
2024-03-27 11:04:28 +04:00
style = document.Styles.AddStyle("NormalTitle", "Normal");
style.Font.Bold = true;
}
2024-04-10 01:01:51 +04:00
2024-03-27 11:04:28 +04:00
protected override void CreatePdf(PdfInfo info)
{
_document = new Document();
DefineStyles(_document);
2024-04-10 01:01:51 +04:00
2024-03-27 11:04:28 +04:00
_section = _document.AddSection();
}
2024-04-10 01:01:51 +04:00
2024-03-27 11:04:28 +04:00
protected override void CreateParagraph(PdfParagraph pdfParagraph)
{
if (_section == null)
{
return;
}
var paragraph = _section.AddParagraph(pdfParagraph.Text);
paragraph.Format.SpaceAfter = "1cm";
2024-04-10 01:01:51 +04:00
paragraph.Format.Alignment = GetParagraphAlignment(pdfParagraph.ParagraphAlignment);
2024-03-27 11:04:28 +04:00
paragraph.Style = pdfParagraph.Style;
}
2024-04-10 01:01:51 +04:00
2024-03-27 11:04:28 +04:00
protected override void CreateTable(List<string> columns)
{
if (_document == null)
{
return;
}
_table = _document.LastSection.AddTable();
2024-04-10 01:01:51 +04:00
2024-03-27 11:04:28 +04:00
foreach (var elem in columns)
{
_table.AddColumn(elem);
}
}
2024-04-10 01:01:51 +04:00
2024-03-27 11:04:28 +04:00
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]);
2024-04-10 01:01:51 +04:00
2024-03-27 11:04:28 +04:00
if (!string.IsNullOrEmpty(rowParameters.Style))
{
row.Cells[i].Style = rowParameters.Style;
}
2024-04-10 01:01:51 +04:00
2024-03-27 11:04:28 +04:00
Unit borderWidth = 0.5;
2024-04-10 01:01:51 +04:00
2024-03-27 11:04:28 +04:00
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;
2024-04-10 01:01:51 +04:00
row.Cells[i].Format.Alignment = GetParagraphAlignment(rowParameters.ParagraphAlignment);
2024-03-27 11:04:28 +04:00
row.Cells[i].VerticalAlignment = VerticalAlignment.Center;
}
}
2024-04-10 01:01:51 +04:00
2024-03-27 11:04:28 +04:00
protected override void SavePdf(PdfInfo info)
{
var renderer = new PdfDocumentRenderer(true)
{
Document = _document
};
renderer.RenderDocument();
renderer.PdfDocument.Save(info.FileName);
}
}
2024-04-10 01:01:51 +04:00
}