Click here to Skip to main content
15,896,118 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 220.4K   5.2K   106  
Create Rich Text Format documents programatically.
using System;
using System.Collections.Generic;
using System.Text;

namespace ESCommon.Rtf
{
    public abstract class RtfParagraphBase : RtfDocumentContentBase
    {
        protected bool isPartOfATable = false;
        protected RtfParagraphContentsCollection contents;
        protected RtfParagraphCollection paragraphs;


        internal override RtfDocument DocumentInternal
        {
            get
            {
                return base.DocumentInternal;
            }
            set
            {
                base.DocumentInternal = value;

                foreach (RtfParagraph paragraph in paragraphs)
                {
                    paragraph.DocumentInternal = value;
                }
            }
        }


        /// <summary>
        /// Initializes a new instance of ESCommon.Rtf.RtfParagraph class.
        /// </summary>
        public RtfParagraphBase()
        {
            contents = new RtfParagraphContentsCollection(this);
            paragraphs = new RtfParagraphCollection(this);
        }

        /// <param name="text">Text to add to paragraph contents.</param>
        public RtfParagraphBase(string text) : this()
        {
            AppendText(text);
        }

        /// <param name="text">Text to add to paragraph contents.</param>
        public RtfParagraphBase(RtfParagraphContentBase text) : this()
        {
            AppendText(text);
        }


        /// <summary>
        /// Add text to paragraph contents
        /// </summary>
        public void AppendText(string text)
        {
            AppendText(new RtfText(text));
        }

        /// <summary>
        /// Add text to paragraph contents
        /// </summary>
        public void AppendText(RtfParagraphContentBase text)
        {
            if (paragraphs.Count == 0)
            {
                contents.Add(text);
            }
            else
            {
                paragraphs[paragraphs.Count - 1].AppendText(text);
            }
        }

        /// <summary>
        /// Add an empty paragraph with inherited formatting
        /// </summary>
        public void AppendParagraph()
        {
            paragraphs.Add(new RtfParagraph());
        }

        /// <summary>
        /// Add a new paragraph with inherited formatting
        /// </summary>
        public void AppendParagraph(string text)
        {
            this.AppendParagraph(new RtfParagraph(text));
        }

        /// <summary>
        /// Add a new paragraph with inherited formatting
        /// </summary>
        public void AppendParagraph(RtfParagraphContentBase text)
        {
            this.AppendParagraph(new RtfParagraph(text));
        }

        /// <summary>
        /// Add a new paragraph with inherited formatting
        /// </summary>
        public void AppendParagraph(RtfParagraph paragraph)
        {
            paragraphs.Add(paragraph);
        }


        public abstract RtfParagraphFormatting GetFormatting();
    }
}

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