namespace QWERTYkez.WordProcessor; internal sealed class TableBuilder : ParagraphBuilderBase, ITable, IRow, ICell { private readonly Table _table = new(); internal TableBuilder(FontProps? baseFont) : base(baseFont) { // Получаем или создаем свойства таблицы var tblProps = _table.GetFirstChild(); if (tblProps == null) { tblProps = new TableProperties(); _table.PrependChild(tblProps); } // --- ОТКЛЮЧАЕМ АВТОПОДБОР --- // Устанавливаем фиксированный макет таблицы. // Именно это свойство соответствует отключению "Автоподбор по содержимому". tblProps.TableLayout = new() { Type = TableLayoutValues.Fixed // <-- Ключевое изменение }; tblProps.TableCellMarginDefault = new([new LeftMargin() { Type = TableWidthUnitValues.Dxa, Width = "0" }, new RightMargin() { Type = TableWidthUnitValues.Dxa, Width = "0" }]); } internal static TableBuilder Create(FontProps? baseFont = null) => new(baseFont); // Метод завершения сборки public Table Build() => _table; // Публичные методы настройки таблицы public ITable Properties( uint borderWidth = 8, BorderValues? borderValues = null, TableRowAlignmentValues? tableAlignment = null) { var borderType = borderValues ?? BorderValues.Single; var alignment = tableAlignment ?? TableRowAlignmentValues.Center; _table.AppendChild(new TableProperties( new TableBorders( new TopBorder() { Val = borderType, Size = borderWidth }, new BottomBorder() { Val = borderType, Size = borderWidth }, new LeftBorder() { Val = borderType, Size = borderWidth }, new RightBorder() { Val = borderType, Size = borderWidth }, new InsideHorizontalBorder() { Val = borderType, Size = borderWidth }, new InsideVerticalBorder() { Val = borderType, Size = borderWidth } ), new TableJustification { Val = alignment }, new TableLayout() { Type = TableLayoutValues.Autofit } )); return this; } // Добавление строк с использованием IRowBuilder public ITable Row(Action configure) { var IRowBuilder = RowBuilder; configure(this); _table.AppendChild(IRowBuilder.Build()); return this; } public ITable Row(double height, Action configure) { var IRowBuilder = RowBuilder.SetHeight(height); configure(this); _table.AppendChild(IRowBuilder.Build()); return this; } // Быстрые методы для добавления строк с ячейками public ITable RowWithCells(params string[] cellTexts) { Row(row => { foreach (var text in cellTexts) { row.Cell(text); } }); return this; } public ITable RowWithCells(FontProps font, params string[] cellTexts) { Row(row => { foreach (var text in cellTexts) { row.Cell(text, font); } }); return this; } private TableRow _row = null!; internal IRow RowBuilder { get { _row = new(); return this; } } TableRow IRow.Build() => _row; // Настройка высоты строки (исправленный метод) public IRow SetHeight(double heightInCm) { var height = new TableRowHeight { Val = (UInt32Value)(uint)(heightInCm * 567), // 1 см = 567 DXA HeightType = HeightRuleValues.AtLeast }; // Убедимся, что TableRowProperties существует if (_row.TableRowProperties is null) { _row.TableRowProperties = new TableRowProperties(); } else { // Удалим существующий TableRowHeight, если он есть var existingHeight = _row.TableRowProperties.Elements().FirstOrDefault(); existingHeight?.Remove(); } // Добавляем TableRowHeight как дочерний элемент _row.TableRowProperties.AppendChild(height); return this; } public IRow SetExactHeight(double heightInCm) { var height = new TableRowHeight { Val = (UInt32Value)(uint)(heightInCm * 567), HeightType = HeightRuleValues.Exact }; // Убедимся, что TableRowProperties существует if (_row.TableRowProperties is null) { _row.TableRowProperties = new TableRowProperties(); } else { // Удалим существующий TableRowHeight, если он есть var existingHeight = _row.TableRowProperties.Elements().FirstOrDefault(); existingHeight?.Remove(); } // Добавляем TableRowHeight как дочерний элемент _row.TableRowProperties.AppendChild(height); return this; } // Добавление ячеек public IRow Cell(Action configure) { var cellBuilder = CellBuilder; configure(this); _row.AppendChild(cellBuilder.Build()); return this; } public IRow Cell(string text) { var cellBuilder = CellBuilder; cellBuilder.Paragraph(p => p.Run(text)); _row.AppendChild(cellBuilder.Build()); return this; } public IRow Cell(string text, FontProps font) { var cellBuilder = CellBuilder; if (font is not null) cellBuilder.Paragraph(p => p.Run(text, font)); else cellBuilder.Paragraph(p => p.Run(text)); _row.AppendChild(cellBuilder.Build()); return this; } public IRow Cell(CellProps cellProps, string text, FontProps font) { var cellBuilder = CellBuilder.SetCellProps(cellProps); if (font is not null) cellBuilder.Paragraph(p => p.Run(text, font)); else cellBuilder.Paragraph(p => p.Run(text)); _row.AppendChild(cellBuilder.Build()); return this; } public IRow Cell(CellProps cellProps, string text) { var cellBuilder = CellBuilder.SetCellProps(cellProps); cellBuilder.Paragraph(p => p.Run(text)); _row.AppendChild(cellBuilder.Build()); return this; } public IRow Cell(CellProps cellProps) { var cellBuilder = CellBuilder.SetCellProps(cellProps); cellBuilder.Paragraph(p => p.Run("")); _row.AppendChild(cellBuilder.Build()); return this; } public IRow Cell() { var cellBuilder = CellBuilder; cellBuilder.Paragraph(p => p.Run("")); _row.AppendChild(cellBuilder.Build()); return this; } // Метод для установки произвольных свойств строки public IRow SetProperties(Action configure) { _row.TableRowProperties ??= new TableRowProperties(); configure(_row.TableRowProperties); return this; } public IRow CellWithPara(Action configure) { var cellBuilder = CellBuilder; cellBuilder.Paragraph(configure); _row.AppendChild(cellBuilder.Build()); return this; } public IRow CellWithPara(CellProps cellProps, Action configure) { var cellBuilder = CellBuilder.SetCellProps(cellProps); cellBuilder.Paragraph(configure); _row.AppendChild(cellBuilder.Build()); return this; } private TableCell _cell = null!; private List _paragraphs = null!; internal ICell CellBuilder { get { _cell = new(); _paragraphs = []; return this; } } TableCell ICell.Build() { foreach (var paragraph in _paragraphs) { _cell.AppendChild(paragraph); } return _cell; } // Установка свойств ячейки public ICell SetCellProps(CellProps props) { if (props.TryExtract(out var elements)) { _cell.TableCellProperties = new TableCellProperties(elements); } return this; } // Добавление параграфов public ICell Paragraph(Action configure) { var paraBuilder = ParagraphBuilder; configure(paraBuilder); _paragraphs.Add(paraBuilder.Build()); return this; } public ICell Paragraph(string text, FontProps? font = null) { var paraBuilder = ParagraphBuilder; if (font is not null) { paraBuilder.Run(text, font); } else { paraBuilder.Run(text); } _paragraphs.Add(paraBuilder.Build()); return this; } public ICell Paragraph(params string[] lines) { var paraBuilder = ParagraphBuilder; if (lines.Length > 0) { paraBuilder.Run(lines[0]); for (int i = 1; i < lines.Length; i++) { paraBuilder.Break(); paraBuilder.Run(lines[i]); } } _paragraphs.Add(paraBuilder.Build()); return this; } // Метод Formula для ICellBuilder ICell ICell.Formula(Action configure) { var paraBuilder = ParagraphBuilder; paraBuilder.Formula(configure); _paragraphs.Add(paraBuilder.Build()); return this; } }