첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ RTFTable.cs

using System.Linq;
using System.Text;

namespace TestProject
{
    /// <summary>
    /// RTF 테이블
    /// </summary>
    public class RTFTable
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region Field

        /// <summary>
        /// 내부 마진
        /// </summary>
        public int InternalMargin = 180;

        /// <summary>
        /// 행 카운트
        /// </summary>
        public int RowCount;
        
        /// <summary>
        /// 열 카운트
        /// </summary>
        public int ColumnCount;

        /// <summary>
        /// 열 너비 배열
        /// </summary>
        public int[] ColumnWidthArray = null;

        /// <summary>
        /// 컨텐트 배열
        /// </summary>
        public string[,] ContentArray = null;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - RTFTable(rowCount, colunmnCount, internalMargin)

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="rowCount">행 카운트</param>
        /// <param name="colunmnCount">열 카운트</param>
        /// <param name="internalMargin">내부 마진</param>
        public RTFTable(int rowCount, int colunmnCount, int internalMargin)
        {
            RowCount         = rowCount;
            ColumnCount      = colunmnCount;
            InternalMargin   = internalMargin;
            ColumnWidthArray = Enumerable.Repeat(1440, ColumnCount).ToArray();

            ContentArray = new string[RowCount, ColumnCount];

            for(int row = 0; row < RowCount; row++)
            {
                for(int column = 0; column < ColumnCount; column++)
                {
                    ContentArray[row, column] = string.Empty;
                }
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 열 너비 배열 설정하기 - SetColumnWidthArray(int[] columnWidthArray)

        /// <summary>
        /// 열 너비 배열 설정하기
        /// </summary>
        /// <param name="columnWidthArray">열 너비 배열</param>
        public void SetColumnWidthArray(params int[] columnWidthArray)
        {
            for(int column = 0; column < ColumnCount; column++)
            {
                ColumnWidthArray[column] = columnWidthArray[column];
            }
        }

        #endregion
        #region 문자열 구하기 - ToString()

        /// <summary>
        /// 문자열 구하기
        /// </summary>
        /// <returns>문자열</returns>
        public override string ToString()
        {
            StringBuilder stringBuilder = new StringBuilder();

            string columnWidthString = GetColumnWidthString();

            for(int row = 0; row < RowCount; row++)
            {
                stringBuilder.Append(@"\trowd");
                stringBuilder.Append(@"\trgaph" + InternalMargin.ToString());

                stringBuilder.Append(columnWidthString);

                for(int c = 0; c < ColumnCount; c++)
                {
                    stringBuilder.Append(@"\pard\intbl{" + ContentArray[row, c].Replace(@"\", @"\\") + @"}\cell");
                }

                stringBuilder.Append(@"\row");
            }

            return stringBuilder.ToString();
        }

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 열 너비 문자열 구하기 - GetColumnWidthString()

        /// <summary>
        /// 열 너비 문자열 구하기
        /// </summary>
        /// <returns>열 너비 문자열</returns>
        private string GetColumnWidthString()
        {
            StringBuilder stringBuilder = new StringBuilder();

            int totalWidth = 0;

            for(int column = 0; column < ColumnCount; column++)
            {
                totalWidth += ColumnWidthArray[column];

                stringBuilder.Append(@"\cellx" + totalWidth.ToString());
            }

            return stringBuilder.ToString();
        }

        #endregion
    }
}

 

728x90

 

▶ MainForm.cs

using System;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            this.insertButton.Click += insertButton_Click;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 테이블 추가 버튼 클릭시 처리하기 - insertButton_Click(sender, e)

        /// <summary>
        /// 테이블 추가 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void insertButton_Click(object sender, EventArgs e)
        {
            RTFTable table = new RTFTable(4, 3, 100);

            table.SetColumnWidthArray(900, 900, 900);

            for(int row = 0; row < 4; row++)
            {
                for(int column = 0; column < 3; column++)
                {
                    table.ContentArray[row, column] = "(" + row.ToString() + ", " + column.ToString() + ")";
                }
            }

            this.richTextBox.Rtf = this.richTextBox.Rtf.Replace("@@@", table.ToString());
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요