From 31b37da456983df5c04e3f7e9fffdb345b9377a4 Mon Sep 17 00:00:00 2001 From: GokaPek Date: Mon, 30 Sep 2024 00:02:59 +0400 Subject: [PATCH] =?UTF-8?q?=D1=88=D0=B0=D0=BF=D0=BA=D0=B0=20=D0=B2=D0=B5?= =?UTF-8?q?=D1=80=D1=82=D0=B8=D0=BA=D0=B0=D0=BB=D1=8C=D0=BD=D0=B0=D1=8F,?= =?UTF-8?q?=20=D0=B0=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D0=B5=20=D0=BD=D0=B5?= =?UTF-8?q?=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Library14Petrushin/PdfColGroupTable.cs | 58 ++++++++++---------------- 1 file changed, 22 insertions(+), 36 deletions(-) diff --git a/Library14Petrushin/PdfColGroupTable.cs b/Library14Petrushin/PdfColGroupTable.cs index 0818285..5e42452 100644 --- a/Library14Petrushin/PdfColGroupTable.cs +++ b/Library14Petrushin/PdfColGroupTable.cs @@ -13,13 +13,13 @@ namespace Library14Petrushin public partial class PdfColGroupTable : Component { public void GeneratePdf( - string fileName, - string documentTitle, - List<(int startRow, int endRow, int startColumn, int endColumn)> mergeCells, - List rowHeights, - List headers, - List propertyNames, - List data) + string fileName, + string documentTitle, + List<(int startRow, int endRow, int startColumn, int endColumn)> mergeCells, + List rowHeights, + List headers, + List propertyNames, + List data) { // Проверка на заполненность входных данных if (string.IsNullOrEmpty(fileName) || string.IsNullOrEmpty(documentTitle) || @@ -41,23 +41,6 @@ namespace Library14Petrushin throw new ArgumentException("Количество высот строк должно совпадать с количеством строк данных."); } - // Проверка на наложение объединенных ячеек - foreach (var (startRow, endRow, startColumn, endColumn) in mergeCells) - { - foreach (var (otherStartRow, otherEndRow, otherStartColumn, otherEndColumn) in mergeCells) - { - if (startRow != otherStartRow || endRow != otherEndRow || - startColumn != otherStartColumn || endColumn != otherEndColumn) - { - if (startRow <= otherEndRow && endRow >= otherStartRow && - startColumn <= otherEndColumn && endColumn >= otherStartColumn) - { - throw new ArgumentException("Объединенные ячейки не должны накладываться друг на друга."); - } - } - } - } - // Создание документа PdfDocument document = new PdfDocument(); PdfPage page = document.AddPage(); @@ -68,17 +51,20 @@ namespace Library14Petrushin gfx.DrawString(documentTitle, new XFont("Verdana", 16, XFontStyleEx.Bold), XBrushes.Black, new XRect(0, 0, page.Width, 50), XStringFormats.Center); // Отрисовка таблицы - double x = 50; + double margin = 50; // Отступ от краев страницы + double x = margin; double y = 70; - double columnWidth = (page.Width - 100) / headers.Count; + double rowHeight = 20; // Фиксированная высота строки + double columnWidth = (page.Width - 2 * margin) / (headers.Count + 1); // Ширина столбца - // Отрисовка заголовков - for (int i = 0; i < headers.Count; i++) + // Отрисовка шапки + for (int col = 0; col < headers.Count; col++) { - gfx.DrawString(headers[i], font, XBrushes.Black, new XRect(x + i * columnWidth, y, columnWidth, rowHeights[0]), XStringFormats.CenterLeft); + 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)); // Добавляем рамку } - y += rowHeights[0]; + x += columnWidth; // Отрисовка данных for (int row = 0; row < data.Count; row++) @@ -90,22 +76,22 @@ namespace Library14Petrushin if (property != null) { var value = property.GetValue(item)?.ToString() ?? string.Empty; - gfx.DrawString(value, font, XBrushes.Black, new XRect(x + col * columnWidth, y, columnWidth, rowHeights[row]), XStringFormats.CenterLeft); + 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)); // Добавляем рамку } } - y += rowHeights[row]; } // Объединение ячеек foreach (var (startRow, endRow, startColumn, endColumn) in mergeCells) { - double mergeY = 70 + (startRow * rowHeights[startRow]); - double mergeHeight = (endRow - startRow + 1) * rowHeights[startRow]; - double mergeX = 50 + (startColumn * columnWidth); + double mergeX = margin + (startColumn * columnWidth); double mergeWidth = (endColumn - startColumn + 1) * columnWidth; + double mergeY = 70 + (startRow * rowHeight); + double mergeHeight = (endRow - startRow + 1) * rowHeight; gfx.DrawRectangle(XPens.Black, XBrushes.White, new XRect(mergeX, mergeY, mergeWidth, mergeHeight)); - gfx.DrawString(headers[startColumn], font, XBrushes.Black, new XRect(mergeX, mergeY, mergeWidth, mergeHeight), XStringFormats.Center); + gfx.DrawString(headers[startRow], font, XBrushes.Black, new XRect(mergeX, mergeY, mergeWidth, mergeHeight), XStringFormats.Center); } // Сохранение документа