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

namespace ESCommon.Rtf
{
    /// <summary>
    /// Represents a table column.
    /// </summary>
    public class RtfTableColumn
    {
        private bool isWidthSet = false;
        private int _width = 9797;
        private RtfTableCellStyle _defaultCellStyle;

        internal int IndexInternal = -1;
        internal RtfTable TableInternal;

        internal bool IsWidthSet
        {
            get { return isWidthSet; }
        }

        /// <summary>
        /// Gets or sets the default cell style to be applied to the cells in the ESCommon.Rtf.RtfTable if no other cell style properties are set.
        /// </summary>
        public RtfTableCellStyle DefaultCellStyle
        {
            get { return _defaultCellStyle; }
            set
            {
                _defaultCellStyle = value;

                if (TableInternal == null)
                    return;

                foreach (RtfTableRow row in TableInternal.Rows)
                {
                    if (!row[IndexInternal].Definition.HasStyle)
                    {
                        row[IndexInternal].Definition.StyleInternal = _defaultCellStyle;
                        if (row[IndexInternal].IsFormattingIncluded = _defaultCellStyle != null)
                            row[IndexInternal].Formatting = _defaultCellStyle.DefaultParagraphFormatting;
                    }
                }
            }
        }

        /// <summary>
        /// Gets or sets the width of the column in twips.
        /// </summary>
        public int Width
        {
            get { return _width; }
            set
            {
                _width = value;
                isWidthSet = true;

                if (TableInternal == null)
                    return;

                foreach (RtfTableRow row in TableInternal.Rows)
                {
                    row[IndexInternal].Definition.Width = _width;

                    row.ResizeCellsInternal();
                }
            }
        }

        /// <summary>
        /// Gets the owning table.
        /// </summary>
        public RtfTable Table
        {
            get { return TableInternal; }
        }

        /// <summary>
        /// Gets the index of the current column in the table.
        /// </summary>
        public int Index
        {
            get { return IndexInternal; }
        }

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

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

        /// <summary>
        /// Initializes a new instance of ESCommon.Rtf.RtfTableColumn class.
        /// </summary>
        /// <param name="style">Default cell style.</param>
        public RtfTableColumn(RtfTableCellStyle style)
        {
            DefaultCellStyle = style;
        }

        /// <summary>
        /// Initializes a new instance of ESCommon.Rtf.RtfTableColumn class.
        /// </summary>
        /// <param name="width">Width in centimeters.</param>
        /// <param name="style">Default cell style.</param>
        public RtfTableColumn(float width, RtfTableCellStyle style)
        {
            Width = TwipConverter.ToTwip(width, MetricUnit.Centimeter);
            DefaultCellStyle = style;
        }
    }
}

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