Click here to Skip to main content
15,886,518 members
Articles / Programming Languages / C#

RTF Document Constructor Library

Rate me:
Please Sign up or sign in to vote.
4.95/5 (57 votes)
16 Aug 2010CPOL4 min read 213.4K   5.2K   106  
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 ESCommon.Rtf.RtfParagraph within a paragraph.
    /// </summary>
    public class RtfParagraphCollection: ICollection<RtfParagraph>, IEnumerable<RtfParagraph>
    {
        private RtfParagraphBase Parent;
        private List<RtfParagraph> list;

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

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


        /// <summary>
        /// Initializes a new instance of ESCommon.Rtf.RtfParagraphCollection class.
        /// </summary>
        /// <param name="paragraph">Parent paragraph.</param>
        internal RtfParagraphCollection(RtfParagraphBase parent)
        {
            list = new List<RtfParagraph>();
            
            Parent = parent;
        }


        /// <summary>
        /// Adds the specified ESCommon.Rtf.RtfParagraph to the collection.
        /// </summary>
        public void Add(RtfParagraph rtfParagraph)
        {
            OnAddingParagraph(rtfParagraph);

            list.Add(rtfParagraph);
        }

        /// <summary>
        /// Adds the specified ESCommon.Rtf.RtfParagraph objects to the collection.
        /// </summary>
        public void AddRange(RtfParagraph[] rtfParagraphs)
        {
            foreach (RtfParagraph paragraph in rtfParagraphs)
            {
                Add(paragraph);
            }
        }

        /// <summary>
        /// Clears all the contents of the collection.
        /// </summary>
        public void Clear()
        {
            list.Clear();
        }

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

        /// <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(RtfParagraph[] array, int arrayIndex)
        {
            list.CopyTo(array, arrayIndex);
        }



        private void OnAddingParagraph(RtfParagraph paragraph)
        {
            if (paragraph == null)
                throw (new ArgumentNullException("RtfParagraph cannot be null"));

            if (paragraph.ParentInternal != null)
                throw (new InvalidOperationException("RtfParagraph already belongs to a paragraph"));

            paragraph.ParentInternal = Parent;
            paragraph.DocumentInternal = Parent.DocumentInternal;
        }



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

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

        public IEnumerator<RtfParagraph> 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)


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

Comments and Discussions