PIbd-31_Danilov_V_V_COP-30/Components/NonVisual/HeaderedTablePDF.cs

99 lines
3.0 KiB
C#
Raw Normal View History

2024-10-16 14:03:22 +04:00
using MigraDoc.DocumentObjectModel.Tables;
using MigraDoc.DocumentObjectModel;
using MigraDoc.Rendering;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Components.SaveToPdfHelpers;
namespace Components.NonVisual
{
public partial class HeaderedTablePDF : Component
{
public HeaderedTablePDF()
{
InitializeComponent();
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
}
public HeaderedTablePDF(IContainer container)
{
container.Add(this);
2024-10-30 14:26:40 +04:00
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
2024-10-16 14:03:22 +04:00
InitializeComponent();
}
public void GeneratePDFWithHead<T>(PDFTableSettings<T> settings)
{
if (settings == null ||
string.IsNullOrEmpty(settings.FilePath) ||
string.IsNullOrEmpty(settings.DocumentTitle) ||
settings.Columns == null || settings.Columns.Count == 0 ||
settings.DataList == null)
2024-10-16 14:03:22 +04:00
throw new ArgumentException("Заполнены не все необходимые данные для генерации документа.");
Document document = new Document();
Section section = document.AddSection();
Paragraph titleParagraph = section.AddParagraph(settings.DocumentTitle, "Heading1");
titleParagraph.Format.Font.Bold = true;
2024-10-16 14:03:22 +04:00
Table table = new Table();
table.Borders.Width = 0.75;
// столбцы
foreach (var (_, width, _, _) in settings.Columns)
2024-10-16 14:03:22 +04:00
{
Column column = table.AddColumn(Unit.FromCentimeter(width));
2024-10-16 14:03:22 +04:00
column.Format.Alignment = ParagraphAlignment.Center;
}
// заголовки
Row headerRow = table.AddRow();
headerRow.Height = Unit.FromCentimeter(settings.HeaderRowHeight);
for (int i = 0; i < settings.Columns.Count; i++)
2024-10-16 14:03:22 +04:00
{
var (headerTitle, _, _, _) = settings.Columns[i];
headerRow.Cells[i].AddParagraph(headerTitle);
2024-10-30 14:26:40 +04:00
headerRow.Cells[i].Format.Font.Bold = true;
headerRow.Cells[i].Format.Alignment = ParagraphAlignment.Center;
2024-10-16 14:03:22 +04:00
}
// данные
foreach (var dataItem in settings.DataList)
{
Row row = table.AddRow();
row.Height = Unit.FromCentimeter(settings.DataRowHeight);
for (int columnIndex = 0; columnIndex < settings.Columns.Count; columnIndex++)
2024-10-16 14:03:22 +04:00
{
var (_, _, propertyName, _) = settings.Columns[columnIndex];
2024-10-16 14:03:22 +04:00
PropertyInfo propertyInfo = typeof(T).GetProperty(propertyName);
2024-10-16 14:03:22 +04:00
if (propertyInfo == null)
throw new ArgumentException($"Свойство {propertyName} не найдено в классе {typeof(T).Name}.");
2024-10-16 14:03:22 +04:00
object value = propertyInfo.GetValue(dataItem);
if (columnIndex == 0)
{
row.Cells[columnIndex].Format.Font.Bold = true;
}
row.Cells[columnIndex].AddParagraph(value != null ? value.ToString() : "");
2024-10-16 14:03:22 +04:00
}
}
section.Add(table);
PdfDocumentRenderer renderer = new PdfDocumentRenderer(true) { Document = document };
renderer.RenderDocument();
renderer.Save(settings.FilePath);
}
}
}