2024-05-29 14:53:56 +04:00
|
|
|
|
using MigraDoc.DocumentObjectModel;
|
|
|
|
|
using MigraDoc.DocumentObjectModel.Tables;
|
|
|
|
|
using MigraDoc.Rendering;
|
|
|
|
|
using PolyclinicBusinessLogic.OfficePackage.HelperEnums;
|
|
|
|
|
using PolyclinicBusinessLogic.OfficePackage.HelperModels.PDF;
|
|
|
|
|
|
|
|
|
|
namespace PolyclinicBusinessLogic.OfficePackage.Implements
|
|
|
|
|
{
|
2024-05-29 21:16:38 +04:00
|
|
|
|
public class SaveToPdfProcedures : AbstractSaveToPdfProcedures
|
2024-05-29 14:53:56 +04:00
|
|
|
|
{
|
|
|
|
|
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,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Создание стилей для документа
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="document"></param>
|
|
|
|
|
private static void DefineStyles(Document document)
|
|
|
|
|
{
|
|
|
|
|
var style = document.Styles["Normal"];
|
|
|
|
|
style.Font.Name = "Times New Roman";
|
2024-05-29 21:55:24 +04:00
|
|
|
|
style.Font.Size = 11;
|
2024-05-29 14:53:56 +04:00
|
|
|
|
style = document.Styles.AddStyle("NormalTitle", "Normal");
|
|
|
|
|
style.Font.Bold = true;
|
|
|
|
|
}
|
|
|
|
|
protected override void CreatePdf(PdfProceduresByMedicamentsAndSymptomsInfo 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();
|
2024-05-29 21:55:24 +04:00
|
|
|
|
// Получаем доступную ширину страницы, чтобы таблица не вылезала за края
|
|
|
|
|
var pageWidth = _document.DefaultPageSetup.PageWidth - _document.DefaultPageSetup.LeftMargin - _document.DefaultPageSetup.RightMargin;
|
|
|
|
|
var totalWidth = columns.Sum(col => Unit.FromCentimeter(double.Parse(col.Replace("cm", ""))));
|
|
|
|
|
|
|
|
|
|
// Если ширина таблицы превышает доступную ширину страницы, уменьшаем ширину колонок пропорционально
|
|
|
|
|
if (totalWidth > pageWidth)
|
|
|
|
|
{
|
|
|
|
|
var scale = pageWidth / totalWidth;
|
|
|
|
|
for (int i = 0; i < columns.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
columns[i] = $"{Unit.FromCentimeter(double.Parse(columns[i].Replace("cm", "")) * scale).Centimeter}cm";
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-29 14:53:56 +04:00
|
|
|
|
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(PdfProceduresByMedicamentsAndSymptomsInfo info)
|
|
|
|
|
{
|
|
|
|
|
var renderer = new PdfDocumentRenderer(true)
|
|
|
|
|
{
|
|
|
|
|
Document = _document
|
|
|
|
|
};
|
|
|
|
|
renderer.RenderDocument();
|
|
|
|
|
renderer.PdfDocument.Save(info.FileName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|