2024-10-22 16:40:02 +04:00

151 lines
4.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using BarsukovComponents.NotVisualComponents.Configs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using MigraDoc;
using MigraDoc.DocumentObjectModel;
using PdfSharp.Pdf;
using MigraDoc.Rendering;
using BarsukovComponents.Exceptions;
namespace BarsukovComponents.NotVisualComponents
{
public partial class PdfTable : Component
{
public PdfTable()
{
InitializeComponent();
}
public PdfTable(IContainer container)
{
container.Add(this);
InitializeComponent();
}
public void CreateTable<T>(string docPath, string title, List<MergeCells>? mergeCells, List<ColumnInfo> colInfo, List<T> data) where T : class, new()
{
if (string.IsNullOrEmpty(docPath))
{
throw new ArgumentNullException("Введите путь до файла!");
}
if (string.IsNullOrEmpty(title))
{
throw new ArgumentNullException("Введите заголовок");
}
if (colInfo == null)
{
throw new ArgumentNullException("Введите все заголовки");
}
if (data == null)
{
throw new ArgumentNullException("Нету информации для вывода");
}
Document _document = new Document();
var style = _document.Styles["Normal"];
style.Font.Name = "Times New Roman";
style.Font.Size = 14;
Section _section = _document.AddSection();
//Заголовок
var paragraph = _section.AddParagraph(title);
paragraph.Format.SpaceAfter = "0.3cm";
paragraph.Format.Font.Size = "16";
paragraph.Format.Font.Bold = true;
paragraph.Format.Alignment = ParagraphAlignment.Center;
//Создание таблицы
var table = _section.AddTable();
table.Borders.Visible = true;
//Создание колонок
for (int i = 0; i < colInfo.Count; i++)
{
table.AddColumn(colInfo[i].Width);
}
//Создание строк
if (mergeCells != null)
{
table.AddRow();
}
var row = table.AddRow();
for (int i = 0; i < colInfo.Count; i++)
{
row[i].AddParagraph(colInfo[i].Header);
}
List<int> MergeColls = new List<int>();
//Объединение ячеек в строке
if (mergeCells != null)
{
foreach (var cell in mergeCells)
{
MergeColls.AddRange(cell.Indexes[0..]);
table.Rows[0].Cells[cell.Indexes[0]].MergeRight = cell.Indexes[1..].Length;
table.Rows[0].Cells[cell.Indexes[0]].AddParagraph(cell.Header);
}
}
int cellsCount = table.Rows[1].Cells.Count;
//Объединение ячеек в столбце
if (MergeColls.Count != 0)
{
for (int i = 0; i < cellsCount; i++)
{
var cell = table.Rows[0].Cells[i];
if (!MergeColls.Contains(i))
{
cell.MergeDown = 1;
cell.AddParagraph(colInfo[i].Header);
}
}
}
//Вывод данных
int rowData = 2;
foreach (var item in data)
{
var properties = item.GetType().GetProperties();
//if (properties.Count() != cellsCount)
//{
// throw new Exception("Кол-во полей объекта не совпадает с кол-вом колонок");
//}
for (int i = 0; i < cellsCount; i++)
{
var property = properties.FirstOrDefault(p => p.Name == colInfo[i].PropertyName);
if (property != null)
{
var propValue = property.GetValue(item);
if (propValue == null)
{
throw new Exception("Пустое поле");
}
if (table.Rows.Count <= rowData)
{
table.AddRow();
}
table.Rows[rowData].Cells[i].AddParagraph(propValue.ToString());
}
}
rowData++;
}
var renderer = new PdfDocumentRenderer(true);
renderer.Document = _document;
renderer.RenderDocument();
renderer.PdfDocument.Save(docPath);
}
}
}