Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

RTF Document Constructor Library

, 16 Aug 2010
Create Rich Text Format documents programatically.
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;

namespace ESCommon.Rtf
{
    /// <summary>
    /// Represents a collection of rows in a ESCommon.Rtf.RtfTable
    /// </summary>
    public class RtfTableRowCollection : ICollection<RtfTableRow>, IEnumerable<RtfTableRow>
    {
        private RtfTable Table;
        private List<RtfTableRow> list;

        /// <summary>
        /// Gets ESCommon.Rtf.RtfTableRow at specified location.
        /// </summary>
        /// <param name="index">A zero-based index of ESCommon.Rtf.RtfTableRow to get.</param>
        public RtfTableRow this[int index]
        {
            get { return list[index]; }
        }

        /// <summary>
        /// Gets the number of rows in the collection.
        /// </summary>
        public int Count
        {
            get { return list.Count; }
        }


        /// <summary>
        /// Initializes a new instance of ESCommon.Rtf.RtfTableRowCollection class.
        /// </summary>
        /// <param name="table">Owning table.</param>
        internal RtfTableRowCollection(RtfTable table)
        {
            list = new List<RtfTableRow>();
            Table = table;
        }


        /// <summary>
        /// Adds a new ESCommon.Rtf.RtfTableRow to the collection.
        /// </summary>
        public void Add()
        {
            Add(new RtfTableRow(Table.ColumnCount));
        }

        /// <summary>
        /// Adds the specified ESCommon.Rtf.RtfTableRow to the collection.
        /// </summary>
        public void Add(RtfTableRow rtfTableRow)
        {
            OnAddingRow(rtfTableRow);

            int num = list.Count;
            
            list.Add(rtfTableRow);

            rtfTableRow.IndexInternal = num;

            foreach (RtfTableCell cell in rtfTableRow.Cells)
                cell.RowIndexInternal = num;
        }

        /// <summary>
        /// Adds the specified ESCommon.Rtf.RtfTableRow objects to the collection.
        /// </summary>
        public void AddRange(RtfTableRow[] rtfTableRows)
        {
            foreach (RtfTableRow row in rtfTableRows)
            {
                if (row != null)
                    this.Add(row);
                else
                    this.Add();
            }
        }

        /// <summary>
        /// Determines if an element is in the collection.
        /// </summary>
        /// <param name="cell">An instance of ESCommon.Rtf.RtfTableRow to locate in the collection.</param>
        public bool Contains(RtfTableRow rtfTableRow)
        {
            return list.Contains(rtfTableRow);
        }

        /// <summary>
        /// Copies the entire collection to a compatible one-dimensional array, starting at the specified index of the target array.
        /// </summary>
        /// <param name="array">The one-dimensional System.Array that is the destination of the elements.</param>
        /// <param name="arrayIndex">The zero-based index in array at which copying begins.</param>
        public void CopyTo(RtfTableRow[] array, int arrayIndex)
        {
            list.CopyTo(array, arrayIndex);
        }


        private void OnAddingRow(RtfTableRow rtfTableRow)
        {
            if (rtfTableRow == null)
                throw (new ArgumentNullException("RtfTableRow cannot be null"));

            if (rtfTableRow.TableInternal != null)
                throw (new InvalidOperationException("RtfTableRow already belongs to a RtfTable"));

            rtfTableRow.TableInternal = this.Table;
            rtfTableRow.DocumentInternal = Table.DocumentInternal;

            rtfTableRow.Width = this.Table.Width;
            rtfTableRow.AlignInternal = this.Table.Align;

            foreach (RtfTableCell cell in rtfTableRow.Cells)
            {
                if (Table.ColumnCount > cell.ColumnIndexInternal)
                    cell.ColumnInternal = Table.Columns[cell.ColumnIndexInternal];
                
                if (!cell.Definition.HasStyle)
                {
                    if (cell.ColumnInternal != null && cell.ColumnInternal.DefaultCellStyle != null)
                        Table.Columns[cell.ColumnIndexInternal].DefaultCellStyle.CopyTo(cell.Definition.StyleInternal);
                    else if (Table.DefaultCellStyle != null)
                        Table.DefaultCellStyle.CopyTo(cell.Definition.StyleInternal);
                }

                if (!cell.Definition.IsWidthSet)
                {
                    if (cell.ColumnInternal != null && cell.ColumnInternal.IsWidthSet)
                        cell.Definition.Width = cell.ColumnInternal.Width;
                }
            }
        }
        

        void ICollection<RtfTableRow>.Clear()
        {

        }

        bool ICollection<RtfTableRow>.IsReadOnly
        {
            get { return false; }
        }

        bool ICollection<RtfTableRow>.Remove(RtfTableRow item)
        {
            return false;
        }

        IEnumerator<RtfTableRow> IEnumerable<RtfTableRow>.GetEnumerator()
        {
            return list.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return list.GetEnumerator();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Dima Popov
Software Developer Energoservice
Russian Federation Russian Federation
Dmitry lives in Arkhangelsk, Russia. He has developed C# applications since 2007.

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 16 Aug 2010
Article Copyright 2010 by Dima Popov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid