93 lines
3.6 KiB
C#
93 lines
3.6 KiB
C#
using FormLibrary.HelperClasses;
|
||
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;
|
||
|
||
namespace FormLibrary
|
||
{
|
||
public partial class PDFTableCustom : Component
|
||
{
|
||
public PDFTableCustom()
|
||
{
|
||
InitializeComponent();
|
||
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
||
}
|
||
|
||
public PDFTableCustom(IContainer container)
|
||
{
|
||
container.Add(this);
|
||
|
||
InitializeComponent();
|
||
}
|
||
public void GeneratePDFWithHead<T>(PDFTableSettings<T> settings)
|
||
{
|
||
if (settings == null ||
|
||
string.IsNullOrEmpty(settings.FilePath) ||
|
||
string.IsNullOrEmpty(settings.DocumentTitle) ||
|
||
settings.HeaderTitles == null || settings.HeaderTitles.Count == 0 ||
|
||
settings.ColumnWidths == null || settings.DataList == null ||
|
||
settings.ColumnPropertyMappings == null)
|
||
throw new ArgumentException("Заполнены не все необходимые данные для генерации документа.");
|
||
|
||
if (settings.HeaderTitles.Count != settings.ColumnWidths.Count)
|
||
throw new ArgumentException("Количество заголовков должно совпадать с количеством ширин столбцов.");
|
||
|
||
|
||
Document document = new Document();
|
||
Section section = document.AddSection();
|
||
section.AddParagraph(settings.DocumentTitle, "Heading1");
|
||
|
||
Table table = new Table();
|
||
table.Borders.Width = 0.75;
|
||
|
||
// столбцы
|
||
for (int i = 0; i < settings.ColumnWidths.Count; i++)
|
||
{
|
||
Column column = table.AddColumn(Unit.FromCentimeter(settings.ColumnWidths[i]));
|
||
column.Format.Alignment = ParagraphAlignment.Center;
|
||
}
|
||
|
||
// заголовки
|
||
Row headerRow = table.AddRow();
|
||
headerRow.Height = Unit.FromCentimeter(settings.HeaderRowHeight);
|
||
for (int i = 0; i < settings.HeaderTitles.Count; i++)
|
||
{
|
||
headerRow.Cells[i].AddParagraph(settings.HeaderTitles[i]);
|
||
headerRow.Cells[i].Format.Font.Bold = true;
|
||
headerRow.Cells[i].Format.Alignment = ParagraphAlignment.Center;
|
||
}
|
||
|
||
// данные
|
||
foreach (var dataItem in settings.DataList)
|
||
{
|
||
Row row = table.AddRow();
|
||
row.Height = Unit.FromCentimeter(settings.DataRowHeight);
|
||
|
||
foreach (var columnMapping in settings.ColumnPropertyMappings)
|
||
{
|
||
PropertyInfo propertyInfo = typeof(T).GetProperty(columnMapping.Value);
|
||
if (propertyInfo == null)
|
||
throw new ArgumentException($"Свойство {columnMapping.Value} не найдено в классе {typeof(T).Name}.");
|
||
|
||
object value = propertyInfo.GetValue(dataItem);
|
||
row.Cells[columnMapping.Key].AddParagraph(value != null ? value.ToString() : "");
|
||
}
|
||
}
|
||
|
||
section.Add(table);
|
||
|
||
PdfDocumentRenderer renderer = new PdfDocumentRenderer(true) { Document = document };
|
||
renderer.RenderDocument();
|
||
renderer.Save(settings.FilePath);
|
||
}
|
||
}
|
||
}
|