using System; using System.Collections.Generic; using System.Text; using C1.Win.C1FlexGrid; using System.IO; using Itenso.Rtf; using Itenso.Rtf.Parser; using Itenso.Rtf.Interpreter; using Itenso.Rtf.Support; using Volian.Print.Library; using Volian.Controls.Library; namespace Volian.Print.Library { public class vlnTable { #region Properties private float[] _RowTop; public float[] RowTop { get { return _RowTop; } set { _RowTop = value; } } private float[] _ColLeft; public float[] ColLeft { get { return _ColLeft; } set { _ColLeft = value; } } private VlnFlexGrid _MyFlexGrid; public VlnFlexGrid MyFlexGrid { get { return _MyFlexGrid; } set { _MyFlexGrid = value; } } private iTextSharp.text.pdf.PdfContentByte _MyContentByte; public iTextSharp.text.pdf.PdfContentByte MyContentByte { get { return _MyContentByte; } set { _MyContentByte = value; } } private vlnCells _MyCells; public vlnCells MyCells { get { return _MyCells; } set { _MyCells = value; } } public float Height { get { return RowTop[MyFlexGrid.Rows.Count]; } } public float Width { get { return ColLeft[MyFlexGrid.Cols.Count]; } } #endregion #region Constructors public vlnTable(VlnFlexGrid myFlexGrid, iTextSharp.text.pdf.PdfContentByte myContentByte) { MyFlexGrid = myFlexGrid; MyContentByte = myContentByte; InitializeSizes(); MyCells = new vlnCells(this, MyFlexGrid, MyContentByte); } #endregion #region Private Methods private void InitializeSizes() { RowTop = new float[MyFlexGrid.Rows.Count + 1]; RowTop[0] = 0; for (int r = 0; r < MyFlexGrid.Rows.Count; r++) RowTop[r + 1] = RowTop[r] + 72 * (MyFlexGrid.Rows[r].Height == -1 ? MyFlexGrid.Rows.DefaultSize : MyFlexGrid.Rows[r].Height) / (float)MyFlexGrid.DPI; ColLeft = new float[MyFlexGrid.Cols.Count + 1]; ColLeft[0] = 0; for (int c = 0; c < MyFlexGrid.Cols.Count; c++) ColLeft[c + 1] = ColLeft[c] + 72 * (MyFlexGrid.Cols[c].Width == -1 ? MyFlexGrid.Cols.DefaultSize : MyFlexGrid.Cols[c].Width) / (float)MyFlexGrid.DPI; } #endregion #region Public Methods public void AdjustRowTop(int row, float hNew) { float hAdjust = hNew - (RowTop[row + 1] - RowTop[row]); for (int r = row; r < MyFlexGrid.Rows.Count; r++) RowTop[r + 1] += hAdjust; } public void ToPdf(iTextSharp.text.pdf.ColumnText myColumnText, float left, float top) { MyCells.ToPdf(myColumnText, left, top); } #endregion } public class vlnCells : List { #region Properties private VlnFlexGrid _MyFlexGrid; public VlnFlexGrid MyFlexGrid { get { return _MyFlexGrid; } set { _MyFlexGrid = value; } } private vlnTable _MyTable; public vlnTable MyTable { get { return _MyTable; } set { _MyTable = value; } } private iTextSharp.text.pdf.PdfContentByte _MyContentByte; public iTextSharp.text.pdf.PdfContentByte MyContentByte { get { return _MyContentByte; } set { _MyContentByte = value; } } #endregion #region Constructors public vlnCells(vlnTable myTable, VlnFlexGrid myFlexGrid, iTextSharp.text.pdf.PdfContentByte myContentByte) { MyTable = myTable; MyFlexGrid = myFlexGrid; MyContentByte = myContentByte; SetupCells(); } #endregion #region Private Methods private void SetupCells() { // Create a ColumnText to determine the cell heights iTextSharp.text.pdf.ColumnText myColumnText1 = new iTextSharp.text.pdf.ColumnText(MyContentByte); // Walk through for (int r = 0; r < MyFlexGrid.Rows.Count; r++) { for (int c = 0; c < MyFlexGrid.Cols.Count; c++) { CellRange cr = MyFlexGrid.GetMergedRange(r, c); if (cr.r1 == r && cr.c1 == c) { float w = MyTable.ColLeft[cr.c2 + 1] - MyTable.ColLeft[cr.c1]; float h = MyTable.RowTop[cr.r2 + 1] - MyTable.RowTop[cr.r1]; string str = MyFlexGrid.GetCellRTFString(r, c)??string.Empty; using (StepRTB myRTB = new StepRTB()) { myRTB.Width = (int)w; myRTB.Font = MyFlexGrid.Font; if (str.StartsWith(@"{\rtf")) myRTB.Rtf = str.Replace(@"\~", @"\u160?"); else myRTB.Text = str; //myRTB.ForeColor = System.Drawing.Color.Red; str = myRTB.Rtf; } iTextSharp.text.Paragraph myPara = RtfToParagraph(str); myColumnText1.SetSimpleColumn(0, 0, w - 4, MyContentByte.PdfDocument.PageSize.Top); // Padding = 4 myPara.MultipliedLeading = 1.2F; myColumnText1.AddElement(myPara); //myColumnText1.Canvas.SetColorFill(iTextSharp.text.BaseColor.RED); float posBefore = myColumnText1.YLine; int status = myColumnText1.Go(true); float posAfter = myColumnText1.YLine; float hContent = 4 + posBefore - posAfter; if (hContent > h) MyTable.AdjustRowTop(cr.r2, hContent); Add(new vlnCell(cr.r1, cr.c1, cr.r2, cr.c2, MyTable, myPara, hContent)); } } } } public static iTextSharp.text.Paragraph RtfToParagraph(string rtf) { IRtfDocument rtfDoc = RtfInterpreterTool.BuildDoc(rtf); Rtf2iTextSharp rtf2IText = new Rtf2iTextSharp(rtfDoc); iTextSharp.text.Paragraph para = rtf2IText.Convert(); para.SetLeading(_SixLinesPerInch, 0); return para; } private static float _SixLinesPerInch = 12; // twips #endregion #region Public Methods public void ToPdf(iTextSharp.text.pdf.ColumnText myColumnText, float left, float top) { foreach (vlnCell myCell in this) myCell.ToPdf(myColumnText, left, top); } #endregion } public class vlnCell { #region Properties private vlnTable _MyTable; public vlnTable MyTable { get { return _MyTable; } set { _MyTable = value; } } private int _r1; public int r1 { get { return _r1; } set { _r1 = value; } } private int _c1; public int c1 { get { return _c1; } set { _c1 = value; } } private int _r2; public int r2 { get { return _r2; } set { _r2 = value; } } private int _c2; public int c2 { get { return _c2; } set { _c2 = value; } } private iTextSharp.text.Paragraph _MyPara; public iTextSharp.text.Paragraph MyPara { get { return _MyPara; } set { _MyPara = value; } } private float _HContent; public float HContent { get { return _HContent; } set { _HContent = value; } } #endregion #region Constructors public vlnCell(int r1, int c1, int r2, int c2, vlnTable myTable, iTextSharp.text.Paragraph myPara, float hContent) { this.r1 = r1; this.c1 = c1; this.r2 = r2; this.c2 = c2; MyTable = myTable; MyPara = myPara; HContent = hContent; } #endregion #region Public Methods public override string ToString() { return string.Format("{0}:{1}", r1, c1); } public void ToPdf(iTextSharp.text.pdf.ColumnText myColumnText, float left, float top) { myColumnText.Canvas.SaveState(); float x = MyTable.ColLeft[c1]; float w = MyTable.ColLeft[c2 + 1] - x; float y = MyTable.RowTop[r1]; float h = MyTable.RowTop[r2 + 1] - y; CellRange cr = MyTable.MyFlexGrid.GetCellRange(r1, c1, r2, c2); if (cr.Style == null || cr.Style.Border.Style != BorderStyleEnum.None) { myColumnText.Canvas.Rectangle(left + x, top - y, w, -h); myColumnText.Canvas.SetColorStroke(iTextSharp.text.Color.BLACK); myColumnText.Canvas.SetLineWidth(.5F); if (cr.Style != null && cr.Style.Border.Style == BorderStyleEnum.Dotted) { myColumnText.Canvas.SetLineCap(iTextSharp.text.pdf.PdfContentByte.LINE_CAP_ROUND); myColumnText.Canvas.SetLineDash(0, 2, 0); //myColumnText.Canvas.SetLineDash(10, 5, 0); //float[] myDashPattern ={ 1, 2, 3, 4, 5, 6, 7, 8 }; //myColumnText.Canvas.SetLineDash(myDashPattern, 0); } else if (cr.Style != null && cr.Style.Border.Style == BorderStyleEnum.Fillet) myColumnText.Canvas.SetLineDash(4, 4, 0); myColumnText.Canvas.Stroke(); } float hAdjust = 0; if (cr.Style != null) { switch (cr.Style.TextAlign) { case TextAlignEnum.CenterBottom: case TextAlignEnum.GeneralBottom: case TextAlignEnum.LeftBottom: case TextAlignEnum.RightBottom: hAdjust = h - HContent; break; case TextAlignEnum.CenterCenter: case TextAlignEnum.GeneralCenter: case TextAlignEnum.LeftCenter: case TextAlignEnum.RightCenter: hAdjust = (h - HContent) / 2; break; default: break; } } iTextSharp.text.pdf.ColumnText myColumnText1 = new iTextSharp.text.pdf.ColumnText(myColumnText.Canvas); myColumnText1.SetSimpleColumn(2 + left + x, top - y - h, left + x + w - 2, -1 + top - y - hAdjust); // 2 == Default Padding myColumnText1.AddElement(MyPara); myColumnText1.Go(); myColumnText.Canvas.RestoreState(); } #endregion } }