2024-09-29 23:00:02 +04:00
|
|
|
|
using PdfSharp.Drawing;
|
|
|
|
|
using PdfSharp.Pdf;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Library14Petrushin
|
|
|
|
|
{
|
|
|
|
|
public partial class PdfColGroupTable : Component
|
|
|
|
|
{
|
|
|
|
|
public void GeneratePdf<T>(
|
2024-09-30 00:02:59 +04:00
|
|
|
|
string fileName,
|
|
|
|
|
string documentTitle,
|
|
|
|
|
List<(int startRow, int endRow, int startColumn, int endColumn)> mergeCells,
|
|
|
|
|
List<double> rowHeights,
|
|
|
|
|
List<string> headers,
|
2024-09-30 10:56:46 +04:00
|
|
|
|
List<string> commonHeaders,
|
2024-09-30 00:02:59 +04:00
|
|
|
|
List<string> propertyNames,
|
|
|
|
|
List<T> data)
|
2024-09-29 23:00:02 +04:00
|
|
|
|
{
|
|
|
|
|
// Проверка на заполненность входных данных
|
|
|
|
|
if (string.IsNullOrEmpty(fileName) || string.IsNullOrEmpty(documentTitle) ||
|
|
|
|
|
mergeCells == null || rowHeights == null || headers == null ||
|
|
|
|
|
propertyNames == null || data == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Все входные данные должны быть заполнены.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Проверка на соответствие количества заголовков и свойств
|
|
|
|
|
if (headers.Count != propertyNames.Count)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Количество заголовков должно совпадать с количеством свойств.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Проверка на соответствие количества строк и высот строк
|
|
|
|
|
if (rowHeights.Count != data.Count)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Количество высот строк должно совпадать с количеством строк данных.");
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 10:26:35 +04:00
|
|
|
|
|
2024-09-29 23:00:02 +04:00
|
|
|
|
PdfDocument document = new PdfDocument();
|
|
|
|
|
PdfPage page = document.AddPage();
|
|
|
|
|
XGraphics gfx = XGraphics.FromPdfPage(page);
|
|
|
|
|
XFont font = new XFont("Verdana", 10);
|
|
|
|
|
|
2024-10-01 10:26:35 +04:00
|
|
|
|
|
2024-09-29 23:00:02 +04:00
|
|
|
|
gfx.DrawString(documentTitle, new XFont("Verdana", 16, XFontStyleEx.Bold), XBrushes.Black, new XRect(0, 0, page.Width, 50), XStringFormats.Center);
|
|
|
|
|
|
|
|
|
|
// Отрисовка таблицы
|
2024-09-30 00:02:59 +04:00
|
|
|
|
double margin = 50; // Отступ от краев страницы
|
|
|
|
|
double x = margin;
|
2024-09-29 23:00:02 +04:00
|
|
|
|
double y = 70;
|
2024-09-30 00:02:59 +04:00
|
|
|
|
double rowHeight = 20; // Фиксированная высота строки
|
|
|
|
|
double columnWidth = (page.Width - 2 * margin) / (headers.Count + 1); // Ширина столбца
|
2024-09-29 23:00:02 +04:00
|
|
|
|
|
2024-09-30 00:02:59 +04:00
|
|
|
|
// Отрисовка шапки
|
2024-09-30 10:56:46 +04:00
|
|
|
|
|
|
|
|
|
for (int col = 0; col < commonHeaders.Count; col++)
|
|
|
|
|
{
|
|
|
|
|
gfx.DrawString(commonHeaders[col], font, XBrushes.Black, new XRect(x, y + col * rowHeight, columnWidth, rowHeight), XStringFormats.CenterLeft);
|
|
|
|
|
gfx.DrawRectangle(XPens.Black, new XRect(x, y + col * rowHeight, columnWidth, rowHeight)); // Добавляем рамку
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
x += columnWidth;
|
|
|
|
|
|
2024-09-30 00:02:59 +04:00
|
|
|
|
for (int col = 0; col < headers.Count; col++)
|
2024-09-29 23:00:02 +04:00
|
|
|
|
{
|
2024-09-30 00:02:59 +04:00
|
|
|
|
gfx.DrawString(headers[col], font, XBrushes.Black, new XRect(x, y + col * rowHeight, columnWidth, rowHeight), XStringFormats.CenterLeft);
|
|
|
|
|
gfx.DrawRectangle(XPens.Black, new XRect(x, y + col * rowHeight, columnWidth, rowHeight)); // Добавляем рамку
|
2024-09-29 23:00:02 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-30 00:02:59 +04:00
|
|
|
|
x += columnWidth;
|
2024-09-29 23:00:02 +04:00
|
|
|
|
|
|
|
|
|
// Отрисовка данных
|
2024-09-30 00:41:41 +04:00
|
|
|
|
for (int col = 0; col < data.Count; col++)
|
2024-09-29 23:00:02 +04:00
|
|
|
|
{
|
2024-09-30 00:41:41 +04:00
|
|
|
|
for (int row = 0; row < propertyNames.Count; row++)
|
2024-09-29 23:00:02 +04:00
|
|
|
|
{
|
2024-09-30 00:41:41 +04:00
|
|
|
|
var item = data[col];
|
|
|
|
|
var property = item.GetType().GetProperty(propertyNames[row]);
|
2024-09-29 23:00:02 +04:00
|
|
|
|
if (property != null)
|
|
|
|
|
{
|
|
|
|
|
var value = property.GetValue(item)?.ToString() ?? string.Empty;
|
2024-09-30 00:02:59 +04:00
|
|
|
|
gfx.DrawString(value, font, XBrushes.Black, new XRect(x + col * columnWidth, y + row * rowHeight, columnWidth, rowHeight), XStringFormats.CenterLeft);
|
|
|
|
|
gfx.DrawRectangle(XPens.Black, new XRect(x + col * columnWidth, y + row * rowHeight, columnWidth, rowHeight)); // Добавляем рамку
|
2024-09-29 23:00:02 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Объединение ячеек
|
|
|
|
|
foreach (var (startRow, endRow, startColumn, endColumn) in mergeCells)
|
|
|
|
|
{
|
2024-09-30 00:02:59 +04:00
|
|
|
|
double mergeX = margin + (startColumn * columnWidth);
|
2024-09-29 23:00:02 +04:00
|
|
|
|
double mergeWidth = (endColumn - startColumn + 1) * columnWidth;
|
2024-09-30 00:02:59 +04:00
|
|
|
|
double mergeY = 70 + (startRow * rowHeight);
|
|
|
|
|
double mergeHeight = (endRow - startRow + 1) * rowHeight;
|
2024-09-29 23:00:02 +04:00
|
|
|
|
|
|
|
|
|
gfx.DrawRectangle(XPens.Black, XBrushes.White, new XRect(mergeX, mergeY, mergeWidth, mergeHeight));
|
2024-10-01 11:11:57 +04:00
|
|
|
|
if (endColumn == 0)
|
|
|
|
|
{
|
|
|
|
|
gfx.DrawString(commonHeaders[startRow], font, XBrushes.Black, new XRect(mergeX, mergeY, mergeWidth, mergeHeight), XStringFormats.Center);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
gfx.DrawString(headers[startRow], font, XBrushes.Black, new XRect(mergeX, mergeY, mergeWidth, mergeHeight), XStringFormats.Center);
|
|
|
|
|
}
|
2024-09-29 23:00:02 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
document.Save(fileName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|