2024-10-02 18:26:36 +04:00
|
|
|
|
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) ||
|
2024-10-03 15:34:22 +04:00
|
|
|
|
settings.Columns == null || settings.Columns.Count == 0 ||
|
|
|
|
|
settings.DataList == null)
|
2024-10-02 18:26:36 +04:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
// столбцы
|
2024-10-03 15:34:22 +04:00
|
|
|
|
foreach (var (_, width, _, _) in settings.Columns)
|
2024-10-02 18:26:36 +04:00
|
|
|
|
{
|
2024-10-03 15:34:22 +04:00
|
|
|
|
Column column = table.AddColumn(Unit.FromCentimeter(width));
|
2024-10-02 18:26:36 +04:00
|
|
|
|
column.Format.Alignment = ParagraphAlignment.Center;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// заголовки
|
|
|
|
|
Row headerRow = table.AddRow();
|
|
|
|
|
headerRow.Height = Unit.FromCentimeter(settings.HeaderRowHeight);
|
2024-10-03 15:34:22 +04:00
|
|
|
|
|
|
|
|
|
for (int columnIndex = 0; columnIndex < settings.Columns.Count; columnIndex++)
|
2024-10-02 18:26:36 +04:00
|
|
|
|
{
|
2024-10-03 15:34:22 +04:00
|
|
|
|
var (headerTitle, _, _, _) = settings.Columns[columnIndex];
|
|
|
|
|
headerRow.Cells[columnIndex].AddParagraph(headerTitle);
|
|
|
|
|
headerRow.Cells[columnIndex].Format.Font.Bold = true;
|
|
|
|
|
headerRow.Cells[columnIndex].Format.Alignment = ParagraphAlignment.Center;
|
2024-10-02 18:26:36 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// данные
|
|
|
|
|
foreach (var dataItem in settings.DataList)
|
|
|
|
|
{
|
|
|
|
|
Row row = table.AddRow();
|
|
|
|
|
row.Height = Unit.FromCentimeter(settings.DataRowHeight);
|
|
|
|
|
|
2024-10-03 15:34:22 +04:00
|
|
|
|
for (int columnIndex = 0; columnIndex < settings.Columns.Count; columnIndex++)
|
2024-10-02 18:26:36 +04:00
|
|
|
|
{
|
2024-10-03 15:34:22 +04:00
|
|
|
|
var (_, _, propertyName, _) = settings.Columns[columnIndex];
|
|
|
|
|
|
|
|
|
|
PropertyInfo propertyInfo = typeof(T).GetProperty(propertyName);
|
2024-10-02 18:26:36 +04:00
|
|
|
|
if (propertyInfo == null)
|
2024-10-03 15:34:22 +04:00
|
|
|
|
throw new ArgumentException($"Свойство {propertyName} не найдено в классе {typeof(T).Name}.");
|
2024-10-02 18:26:36 +04:00
|
|
|
|
|
|
|
|
|
object value = propertyInfo.GetValue(dataItem);
|
2024-10-03 15:34:22 +04:00
|
|
|
|
if (columnIndex == 0)
|
|
|
|
|
{
|
|
|
|
|
row.Cells[columnIndex].Format.Font.Bold = true;
|
|
|
|
|
}
|
|
|
|
|
row.Cells[columnIndex].AddParagraph(value != null ? value.ToString() : "");
|
2024-10-02 18:26:36 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
section.Add(table);
|
|
|
|
|
|
|
|
|
|
PdfDocumentRenderer renderer = new PdfDocumentRenderer(true) { Document = document };
|
|
|
|
|
renderer.RenderDocument();
|
|
|
|
|
renderer.Save(settings.FilePath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|