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

namespace ESCommon.Rtf
{
    /// <summary>
    /// Specifies border setting.
    /// </summary>
    [Flags]
    public enum RtfBorderSetting { 
        None = 0,
        Top = 1,
        Left = 2,
        Bottom = 4,
        Right = 8,
        All = Top | Left | Bottom | Right
    }
    
    /// <summary>
    /// Represents a table cell.
    /// </summary>
    [RtfControlWord("pard"), RtfControlWordDenotingEnd("cell")]
    public class RtfTableCell : RtfFormattedParagraph
    {
        private RtfTableCellDefinition _definition;

        internal RtfTableColumn ColumnInternal;
        internal RtfTableRow RowInternal;
        internal int ColumnIndexInternal = -1;
        internal int RowIndexInternal = -1;

        /// <summary>
        /// Gets cell definition containing information about cell style and width.
        /// </summary>
        [RtfIgnore]
        public RtfTableCellDefinition Definition
        {
            get { return _definition; }
        }

        /// <summary>
        /// Gets owning table.
        /// </summary>
        [RtfIgnore]
        public RtfTable Table
        {
            get { return RowInternal.Table; }
        }

        /// <summary>
        /// Gets owning column.
        /// </summary>
        [RtfIgnore]
        public RtfTableColumn OwningColumn
        {
            get { return ColumnInternal; }
        }

        /// <summary>
        /// Gets owning row.
        /// </summary>
        [RtfIgnore]
        public RtfTableRow OwningRow
        {
            get { return RowInternal; }
        }

        /// <summary>
        /// Gets the index of the owning column in a table.
        /// </summary>
        public int ColumnIndex
        {
            get { return ColumnIndexInternal; }
        }

        /// <summary>
        /// Gets the index of the owning row in a table.
        /// </summary>
        public int RowIndex
        {
            get { return RowIndexInternal; }
        }

        /// <summary>
        /// Initializes a new instance of ESCommon.Rtf.RtfTableCell class.
        /// </summary>
        public RtfTableCell()
        {
            Initialize();
        }

        /// <summary>
        /// Initializes a new instance of ESCommon.Rtf.RtfTableCell class.
        /// </summary>
        /// <param name="width">Cell width in centimeters.</param>
        public RtfTableCell(float width)
        {
            Initialize();
            
            _definition.Width = TwipConverter.ToTwip(width, MetricUnit.Centimeter);
        }

        /// <summary>
        /// Initializes a new instance of ESCommon.Rtf.RtfTableCell class.
        /// </summary>
        /// <param name="text">Text displayed in the cell.</param>
        public RtfTableCell(string text) : base(text)
        {
            Initialize();
        }

        /// <summary>
        /// Initializes a new instance of ESCommon.Rtf.RtfTableCell class.
        /// </summary>
        /// <param name="text">Text displayed in the cell.</param>
        public RtfTableCell(RtfParagraphContentBase text) : base(text)
        {
            Initialize();
        }

        /// <summary>
        /// Initializes a new instance of ESCommon.Rtf.RtfTableCell class.
        /// </summary>
        /// <param name="style">Style applied to the cell.</param>
        public RtfTableCell(RtfTableCellStyle style) : base(style.DefaultParagraphFormatting)
        {
            Initialize(style);
        }

        /// <summary>
        /// Initializes a new instance of ESCommon.Rtf.RtfTableCell class.
        /// </summary>
        /// <param name="width">Cell width in centimeters.</param>
        /// <param name="text">Text displayed in the cell.</param>
        public RtfTableCell(float width, string text) : base(text)
        {
            Initialize();
            _definition.Width = TwipConverter.ToTwip(width, MetricUnit.Centimeter);
        }

        /// <summary>
        /// Initializes a new instance of ESCommon.Rtf.RtfTableCell class.
        /// </summary>
        /// <param name="width">Cell width in centimeters.</param>
        /// <param name="text">Text displayed in the cell.</param>
        public RtfTableCell(float width, RtfParagraphContentBase text) : base(text)
        {
            Initialize();
            _definition.Width = TwipConverter.ToTwip(width, MetricUnit.Centimeter);
        }

        /// <summary>
        /// Initializes a new instance of ESCommon.Rtf.RtfTableCell class.
        /// </summary>
        /// <param name="width">Cell width in centimeters.</param>
        /// <param name="style">Style applied to the cell.</param>
        public RtfTableCell(float width, RtfTableCellStyle style) : base(style.DefaultParagraphFormatting)
        {
            Initialize(style);
            _definition.Width = TwipConverter.ToTwip(width, MetricUnit.Centimeter);
        }

        /// <summary>
        /// Initializes a new instance of ESCommon.Rtf.RtfTableCell class.
        /// </summary>
        /// <param name="text">Text displayed in the cell.</param>
        /// <param name="style">Style applied to the cell.</param>
        public RtfTableCell(string text, RtfTableCellStyle style) : base(text, style.DefaultParagraphFormatting)
        {
            Initialize(style);
        }

        /// <summary>
        /// Initializes a new instance of ESCommon.Rtf.RtfTableCell class.
        /// </summary>
        /// <param name="text">Text displayed in the cell.</param>
        /// <param name="style">Style applied to the cell.</param>
        public RtfTableCell(RtfParagraphContentBase text, RtfTableCellStyle style) : base(text, style.DefaultParagraphFormatting)
        {
            Initialize(style);
        }

        /// <summary>
        /// Initializes a new instance of ESCommon.Rtf.RtfTableCell class.
        /// </summary>
        /// <param name="width">Cell width in centimeters.</param>
        /// <param name="text">Text displayed in the cell.</param>
        /// <param name="style">Style applied to the cell.</param>
        public RtfTableCell(float width, string text, RtfTableCellStyle style) : base(text, style.DefaultParagraphFormatting)
        {
            Initialize(style);

            _definition.Width = TwipConverter.ToTwip(width, MetricUnit.Centimeter);
        }

        /// <summary>
        /// Initializes a new instance of ESCommon.Rtf.RtfTableCell class.
        /// </summary>
        /// <param name="width">Cell width in centimeters.</param>
        /// <param name="text">Text displayed in the cell.</param>
        /// <param name="style">Style applied to the cell.</param>
        public RtfTableCell(float width, RtfParagraphContentBase text, RtfTableCellStyle style) : base(text, style.DefaultParagraphFormatting)
        {
            Initialize(style);

            _definition.Width = TwipConverter.ToTwip(width, MetricUnit.Centimeter);
        }


        private void Initialize()
        {
            Initialize(null);
        }

        private void Initialize(RtfTableCellStyle style)
        {
            _definition = new RtfTableCellDefinition(this);
            _definition.Style = style;

            IsPartOfATable = true;
            IsFormattingIncluded = style != null && style.DefaultParagraphFormatting != null;
        }
    }
}

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