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( 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) || 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("Количество высот строк должно совпадать с количеством строк данных."); } // Проверка на наложение объединенных ячеек 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(); XGraphics gfx = XGraphics.FromPdfPage(page); XFont font = new XFont("Verdana", 10); // Отрисовка заголовка документа gfx.DrawString(documentTitle, new XFont("Verdana", 16, XFontStyleEx.Bold), XBrushes.Black, new XRect(0, 0, page.Width, 50), XStringFormats.Center); // Отрисовка таблицы double x = 50; double y = 70; double columnWidth = (page.Width - 100) / headers.Count; // Отрисовка заголовков for (int i = 0; i < headers.Count; i++) { gfx.DrawString(headers[i], font, XBrushes.Black, new XRect(x + i * columnWidth, y, columnWidth, rowHeights[0]), XStringFormats.CenterLeft); } y += rowHeights[0]; // Отрисовка данных for (int row = 0; row < data.Count; row++) { var item = data[row]; for (int col = 0; col < propertyNames.Count; col++) { var property = item.GetType().GetProperty(propertyNames[col]); 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); } } 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 mergeWidth = (endColumn - startColumn + 1) * columnWidth; 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); } // Сохранение документа document.Save(fileName); } } }