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) ||
|
2024-11-02 18:51:07 +04:00
|
|
|
|
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();
|
2024-11-02 18:51:07 +04:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
// столбцы
|
2024-11-02 18:51:07 +04:00
|
|
|
|
foreach (var (_, width, _, _) in settings.Columns)
|
2024-10-16 14:03:22 +04:00
|
|
|
|
{
|
2024-11-02 18:51:07 +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);
|
|
|
|
|
|
2024-11-02 18:51:07 +04:00
|
|
|
|
for (int i = 0; i < settings.Columns.Count; i++)
|
2024-10-16 14:03:22 +04:00
|
|
|
|
{
|
2024-11-02 18:51:07 +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);
|
|
|
|
|
|
2024-11-02 18:51:07 +04:00
|
|
|
|
for (int columnIndex = 0; columnIndex < settings.Columns.Count; columnIndex++)
|
2024-10-16 14:03:22 +04:00
|
|
|
|
{
|
2024-11-02 18:51:07 +04:00
|
|
|
|
var (_, _, propertyName, _) = settings.Columns[columnIndex];
|
2024-10-16 14:03:22 +04:00
|
|
|
|
|
2024-11-02 18:51:07 +04:00
|
|
|
|
PropertyInfo propertyInfo = typeof(T).GetProperty(propertyName);
|
2024-10-16 14:03:22 +04:00
|
|
|
|
if (propertyInfo == null)
|
2024-11-02 18:51:07 +04:00
|
|
|
|
throw new ArgumentException($"Свойство {propertyName} не найдено в классе {typeof(T).Name}.");
|
2024-10-16 14:03:22 +04:00
|
|
|
|
|
|
|
|
|
object value = propertyInfo.GetValue(dataItem);
|
2024-11-02 18:51:07 +04:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|