#region MigraDoc - Creating Documents on the Fly // // Authors: // Stefan Lange // Klaus Potzesny // David Stephensen // // Copyright (c) 2001-2017 empira Software GmbH, Cologne Area (Germany) // // http://www.pdfsharp.com // http://www.migradoc.com // http://sourceforge.net/projects/pdfsharp // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. #endregion using System; using MigraDoc.DocumentObjectModel.publics; using MigraDoc.DocumentObjectModel.Visitors; namespace MigraDoc.DocumentObjectModel.Tables { /// /// Represents a table in a document. /// public class Table : DocumentObject, IVisitable { /// /// Initializes a new instance of the Table class. /// public Table() { } /// /// Initializes a new instance of the Table class with the specified parent. /// public Table(DocumentObject parent) : base(parent) { } #region Methods /// /// Creates a deep copy of this object. /// public new Table Clone() { return (Table)DeepCopy(); } /// /// Implements the deep copy of the object. /// protected override object DeepCopy() { Table table = (Table)base.DeepCopy(); if (table._columns != null) { table._columns = table._columns.Clone(); table._columns._parent = table; } if (table._rows != null) { table._rows = table._rows.Clone(); table._rows._parent = table; } if (table._format != null) { table._format = table._format.Clone(); table._format._parent = table; } if (table._borders != null) { table._borders = table._borders.Clone(); table._borders._parent = table; } if (table._shading != null) { table._shading = table._shading.Clone(); table._shading._parent = table; } return table; } /// /// Adds a new column to the table. Allowed only before any row was added. /// public Column AddColumn() { return Columns.AddColumn(); } /// /// Adds a new column of the specified width to the table. Allowed only before any row was added. /// public Column AddColumn(Unit width) { Column clm = Columns.AddColumn(); clm.Width = width; return clm; } /// /// Adds a new row to the table. Allowed only if at least one column was added. /// public Row AddRow() { return _rows.AddRow(); } /// /// Returns true if no cell exists in the table. /// public bool IsEmpty { get { return Rows.Count == 0 || Columns.Count == 0; } } /// /// Sets a shading of the specified Color in the specified Tablerange. /// public void SetShading(int clm, int row, int clms, int rows, Color clr) { int rowsCount = _rows.Count; int clmsCount = _columns.Count; if (row < 0 || row >= rowsCount) throw new ArgumentOutOfRangeException("row", "Invalid row index."); if (clm < 0 || clm >= clmsCount) throw new ArgumentOutOfRangeException("clm", "Invalid column index."); if (rows <= 0 || row + rows > rowsCount) throw new ArgumentOutOfRangeException("rows", "Invalid row count."); if (clms <= 0 || clm + clms > clmsCount) throw new ArgumentOutOfRangeException("clms", "Invalid column count."); int maxRow = row + rows - 1; int maxClm = clm + clms - 1; for (int r = row; r <= maxRow; r++) { Row currentRow = _rows[r]; for (int c = clm; c <= maxClm; c++) currentRow[c].Shading.Color = clr; } } /// /// Sets the borders surrounding the specified range of the table. /// public void SetEdge(int clm, int row, int clms, int rows, Edge edge, BorderStyle style, Unit width, Color clr) { Border border; int maxRow = row + rows - 1; int maxClm = clm + clms - 1; for (int r = row; r <= maxRow; r++) { Row currentRow = _rows[r]; for (int c = clm; c <= maxClm; c++) { Cell currentCell = currentRow[c]; if ((edge & Edge.Top) == Edge.Top && r == row) { border = currentCell.Borders.Top; border.Style = style; border.Width = width; if (clr != Color.Empty) border.Color = clr; } if ((edge & Edge.Left) == Edge.Left && c == clm) { border = currentCell.Borders.Left; border.Style = style; border.Width = width; if (clr != Color.Empty) border.Color = clr; } if ((edge & Edge.Bottom) == Edge.Bottom && r == maxRow) { border = currentCell.Borders.Bottom; border.Style = style; border.Width = width; if (clr != Color.Empty) border.Color = clr; } if ((edge & Edge.Right) == Edge.Right && c == maxClm) { border = currentCell.Borders.Right; border.Style = style; border.Width = width; if (clr != Color.Empty) border.Color = clr; } if ((edge & Edge.Horizontal) == Edge.Horizontal && r < maxRow) { border = currentCell.Borders.Bottom; border.Style = style; border.Width = width; if (clr != Color.Empty) border.Color = clr; } if ((edge & Edge.Vertical) == Edge.Vertical && c < maxClm) { border = currentCell.Borders.Right; border.Style = style; border.Width = width; if (clr != Color.Empty) border.Color = clr; } if ((edge & Edge.DiagonalDown) == Edge.DiagonalDown) { border = currentCell.Borders.DiagonalDown; border.Style = style; border.Width = width; if (clr != Color.Empty) border.Color = clr; } if ((edge & Edge.DiagonalUp) == Edge.DiagonalUp) { border = currentCell.Borders.DiagonalUp; border.Style = style; border.Width = width; if (clr != Color.Empty) border.Color = clr; } } } } /// /// Sets the borders surrounding the specified range of the table. /// public void SetEdge(int clm, int row, int clms, int rows, Edge edge, BorderStyle style, Unit width) { SetEdge(clm, row, clms, rows, edge, style, width, Color.Empty); } #endregion #region Properties /// /// Gets or sets the Columns collection of the table. /// public Columns Columns { get { return _columns ?? (_columns = new Columns(this)); } set { SetParent(value); _columns = value; } } [DV] public Columns _columns; /// /// Gets the Rows collection of the table. /// public Rows Rows { get { return _rows ?? (_rows = new Rows(this)); } set { SetParent(value); _rows = value; } } [DV] public Rows _rows; /// /// Sets or gets the default style name for all rows and columns of the table. /// public string Style { get { return _style.Value; } set { _style.Value = value; } } [DV] public NString _style = NString.NullValue; /// /// Gets the default ParagraphFormat for all rows and columns of the table. /// public ParagraphFormat Format { get { return _format ?? (_format = new ParagraphFormat(this)); } set { SetParent(value); _format = value; } } [DV] public ParagraphFormat _format; /// /// Gets or sets the default top padding for all cells of the table. /// public Unit TopPadding { get { return _topPadding; } set { _topPadding = value; } } [DV] public Unit _topPadding = Unit.NullValue; /// /// Gets or sets the default bottom padding for all cells of the table. /// public Unit BottomPadding { get { return _bottomPadding; } set { _bottomPadding = value; } } [DV] public Unit _bottomPadding = Unit.NullValue; /// /// Gets or sets the default left padding for all cells of the table. /// public Unit LeftPadding { get { return _leftPadding; } set { _leftPadding = value; } } [DV] public Unit _leftPadding = Unit.NullValue; /// /// Gets or sets the default right padding for all cells of the table. /// public Unit RightPadding { get { return _rightPadding; } set { _rightPadding = value; } } [DV] public Unit _rightPadding = Unit.NullValue; /// /// Gets the default Borders object for all cells of the column. /// public Borders Borders { get { return _borders ?? (_borders = new Borders(this)); } set { SetParent(value); _borders = value; } } [DV] public Borders _borders; /// /// Gets the default Shading object for all cells of the column. /// public Shading Shading { get { return _shading ?? (_shading = new Shading(this)); } set { SetParent(value); _shading = value; } } [DV] public Shading _shading; /// /// Gets or sets a value indicating whether /// to keep all the table rows on the same page. /// public bool KeepTogether { get { return _keepTogether.Value; } set { _keepTogether.Value = value; } } [DV] public NBool _keepTogether = NBool.NullValue; /// /// Gets or sets a comment associated with this object. /// public string Comment { get { return _comment.Value; } set { _comment.Value = value; } } [DV] public NString _comment = NString.NullValue; #endregion #region public /// /// Converts Table into DDL. /// public override void Serialize(Serializer serializer) { serializer.WriteComment(_comment.Value); serializer.WriteLine("\\table"); int pos = serializer.BeginAttributes(); if (_style.Value != String.Empty) serializer.WriteSimpleAttribute("Style", Style); if (!IsNull("Format")) _format.Serialize(serializer, "Format", null); if (!_topPadding.IsNull) serializer.WriteSimpleAttribute("TopPadding", TopPadding); if (!_leftPadding.IsNull) serializer.WriteSimpleAttribute("LeftPadding", LeftPadding); if (!_rightPadding.IsNull) serializer.WriteSimpleAttribute("RightPadding", RightPadding); if (!_bottomPadding.IsNull) serializer.WriteSimpleAttribute("BottomPadding", BottomPadding); if (!IsNull("Borders")) _borders.Serialize(serializer, null); if (!IsNull("Shading")) _shading.Serialize(serializer); serializer.EndAttributes(pos); serializer.BeginContent(); Columns.Serialize(serializer); Rows.Serialize(serializer); serializer.EndContent(); } /// /// Allows the visitor object to visit the document object and its child objects. /// void IVisitable.AcceptVisitor(DocumentObjectVisitor visitor, bool visitChildren) { visitor.VisitTable(this); ((IVisitable)_columns).AcceptVisitor(visitor, visitChildren); ((IVisitable)_rows).AcceptVisitor(visitor, visitChildren); } /// /// Gets the cell with the given row and column indices. /// public Cell this[int rwIdx, int clmIdx] { get { return Rows[rwIdx].Cells[clmIdx]; } } /// /// Returns the meta object of this instance. /// public override Meta Meta { get { return _meta ?? (_meta = new Meta(typeof(Table))); } } static Meta _meta; #endregion } }