Example #1
0
        public RtfTable AddTable(int rowCount, int colCount, float fontSize)
        {
            var horizontalWidth = RtfUtility.PaperWidthInPt(_paper, _orientation)
                                  - Margins[Direction.Left] - Margins[Direction.Right];

            return(AddTable(rowCount, colCount, horizontalWidth, fontSize));
        }
        public override string Render()
        {
            var tokList = BuildTokenList();
            var result  = new StringBuilder(_blockHead);

            if (_startNewPage)
            {
                result.Append(@"\pagebb");
            }

            if (LineSpacing >= 0)
            {
                result.Append(@"\sl-" + RtfUtility.Pt2Twip(LineSpacing) + @"\slmult0");
            }
            if (_margins[Direction.Top] > 0)
            {
                result.Append(@"\sb" + RtfUtility.Pt2Twip(_margins[Direction.Top]));
            }
            if (_margins[Direction.Bottom] > 0)
            {
                result.Append(@"\sa" + RtfUtility.Pt2Twip(_margins[Direction.Bottom]));
            }
            if (_margins[Direction.Left] > 0)
            {
                result.Append(@"\li" + RtfUtility.Pt2Twip(_margins[Direction.Left]));
            }
            if (_margins[Direction.Right] > 0)
            {
                result.Append(@"\ri" + RtfUtility.Pt2Twip(_margins[Direction.Right]));
            }
            //if (_firstLineIndent != 0) {
            result.Append(@"\fi" + RtfUtility.Pt2Twip(FirstLineIndent));
            //}
            result.Append(AlignmentCode());
            result.AppendLine();

            // insert default char format intto the 1st position of _charFormats
            if (_defaultCharFormat != null)
            {
                result.AppendLine(_defaultCharFormat.RenderHead());
            }
            result.AppendLine(ExtractTokenList(tokList));
            if (_defaultCharFormat != null)
            {
                result.Append(_defaultCharFormat.RenderTail());
            }

            result.AppendLine(_blockTail);
            return(result.ToString());
        }
        protected string ExtractTokenList(LinkedList <Token> tokList)
        {
            LinkedListNode <Token> node;
            var result = new StringBuilder();

            node = tokList.First;
            while (node != null)
            {
                if (node.Value.isControl)
                {
                    result.Append(node.Value.text);
                }
                else
                {
                    result.Append(RtfUtility.UnicodeEncode(node.Value.text));
                }
                node = node.Next;
            }
            return(result.ToString());
        }
Example #4
0
        public override string Render()
        {
            var result = new StringBuilder(_blockHead);

            if (_startNewPage)
            {
                result.Append(@"\pagebb");
            }

            if (_margins[Direction.Top] >= 0)
            {
                result.Append(@"\sb" + RtfUtility.Pt2Twip(_margins[Direction.Top]));
            }
            if (_margins[Direction.Bottom] >= 0)
            {
                result.Append(@"\sa" + RtfUtility.Pt2Twip(_margins[Direction.Bottom]));
            }
            if (_margins[Direction.Left] >= 0)
            {
                result.Append(@"\li" + RtfUtility.Pt2Twip(_margins[Direction.Left]));
            }
            if (_margins[Direction.Right] >= 0)
            {
                result.Append(@"\ri" + RtfUtility.Pt2Twip(_margins[Direction.Right]));
            }
            switch (_alignment)
            {
            case Align.Left:
                result.Append(@"\ql");
                break;

            case Align.Right:
                result.Append(@"\qr");
                break;

            case Align.Center:
                result.Append(@"\qc");
                break;
            }
            result.AppendLine();

            result.Append(@"{\*\shppict{\pict");
            if (_imgType == ImageFileType.Jpg)
            {
                result.Append(@"\jpegblip");
            }
            else if (_imgType == ImageFileType.Png || _imgType == ImageFileType.Gif)
            {
                result.Append(@"\pngblip");
            }
            else
            {
                throw new Exception("Image type not supported.");
            }
            if (_height > 0)
            {
                result.Append(@"\pichgoal" + RtfUtility.Pt2Twip(_height));
            }
            if (_width > 0)
            {
                result.Append(@"\picwgoal" + RtfUtility.Pt2Twip(_width));
            }
            result.AppendLine();

            result.AppendLine(ExtractImage());
            result.AppendLine("}}");
            if (StartNewPara)
            {
                result.Append(@"\par");
            }
            result.AppendLine(_blockTail);
            return(result.ToString());
        }
Example #5
0
        public override string Render()
        {
            var result = new StringBuilder();

            // validate borders for each cell.
            // (borders may be changed because of cell merging)
            ValidateAllMergedCellBorders();
            // set default char format for each cell.
            if (_defaultCharFormat != null)
            {
                for (var i = 0; i < RowCount; i++)
                {
                    for (var j = 0; j < ColCount; j++)
                    {
                        if (_cells[i][j].IsMerged &&
                            _cells[i][j].MergeInfo.Representative != _cells[i][j])
                        {
                            continue;
                        }
                        if (_cells[i][j].DefaultCharFormat != null)
                        {
                            _cells[i][j].DefaultCharFormat.CopyFrom(_defaultCharFormat);
                        }
                    }
                }
            }

            var topMargin = _margins[Direction.Top] - _fontSize;

            if (_startNewPage || topMargin > 0)
            {
                result.Append(@"{\pard");
                if (_startNewPage)
                {
                    result.Append(@"\pagebb");
                }
                if (_margins[Direction.Top] >= 0)
                {
                    result.Append(@"\sl-" + RtfUtility.Pt2Twip(topMargin));
                }
                else
                {
                    result.Append(@"\sl-1");
                }
                result.AppendLine(@"\slmult0\par}");
            }

            int colAcc;

            for (var i = 0; i < RowCount; i++)
            {
                colAcc = 0;
                result.Append(@"{\trowd\trgaph" +
                              string.Format(@"\trpaddl{0}\trpaddt{1}\trpaddr{2}\trpaddb{3}",
                                            RtfUtility.Pt2Twip(CellPadding[i][Direction.Left]),
                                            RtfUtility.Pt2Twip(CellPadding[i][Direction.Top]),
                                            RtfUtility.Pt2Twip(CellPadding[i][Direction.Right]),
                                            RtfUtility.Pt2Twip(CellPadding[i][Direction.Bottom])));
                switch (_alignment)
                {
                case Align.Left:
                    result.Append(@"\trql");
                    break;

                case Align.Right:
                    result.Append(@"\trqr");
                    break;

                case Align.Center:
                    result.Append(@"\trqc");
                    break;

                case Align.FullyJustify:
                    result.Append(@"\trqj");
                    break;
                }
                result.AppendLine();
                if (_margins[Direction.Left] >= 0)
                {
                    result.AppendLine(@"\trleft" + RtfUtility.Pt2Twip(_margins[Direction.Left]));
                    colAcc = RtfUtility.Pt2Twip(_margins[Direction.Left]);
                }
                if (_rowHeight[i] > 0)
                {
                    result.Append(@"\trrh" + RtfUtility.Pt2Twip(_rowHeight[i]));
                }
                if (_rowKeepInSamePage[i])
                {
                    result.Append(@"\trkeep");
                }
                if (i < TitleRowCount)
                {
                    result.Append(@"\trhdr");
                }
                result.AppendLine();

                for (var j = 0; j < ColCount; j++)
                {
                    if (_cells[i][j].IsMerged && !_cells[i][j].IsBeginOfColSpan)
                    {
                        continue;
                    }
                    var nextCellLeftBorderClearance = j < ColCount - 1 ? Cell(i, j + 1).OuterLeftBorderClearance : 0;
                    colAcc += RtfUtility.Pt2Twip(Cell(i, j).Width);
                    var colRightPos = colAcc;
                    if (nextCellLeftBorderClearance < 0)
                    {
                        colRightPos += RtfUtility.Pt2Twip(nextCellLeftBorderClearance);
                        colRightPos  = colRightPos == 0 ? 1 : colRightPos;
                    }

                    // Borders
                    for (var d = Direction.Top; d <= Direction.Left; d++)
                    {
                        var bdr = Cell(i, j).Borders[d];
                        if (bdr.Style != BorderStyle.None)
                        {
                            result.Append(@"\clbrdr");
                            switch (d)
                            {
                            case Direction.Top:
                                result.Append("t");
                                break;

                            case Direction.Right:
                                result.Append("r");
                                break;

                            case Direction.Bottom:
                                result.Append("b");
                                break;

                            case Direction.Left:
                                result.Append("l");
                                break;
                            }
                            result.Append(@"\brdrw" + RtfUtility.Pt2Twip(bdr.Width));
                            result.Append(@"\brdr");
                            switch (bdr.Style)
                            {
                            case BorderStyle.Single:
                                result.Append("s");
                                break;

                            case BorderStyle.Dotted:
                                result.Append("dot");
                                break;

                            case BorderStyle.Dashed:
                                result.Append("dash");
                                break;

                            case BorderStyle.Double:
                                result.Append("db");
                                break;

                            default:
                                throw new Exception("Unkown border style");
                            }
                            result.Append(@"\brdrcf" + bdr.Color.Value);
                        }
                    }

                    // Cell background color
                    if (Cell(i, j).BackgroundColor != null)
                    {
                        result.Append($@"\clcbpat{Cell(i, j).BackgroundColor.Value}"); // cell.BackGroundColor overrides others
                    }
                    else if (i == 0 && HeaderBackgroundColor != null)
                    {
                        result.Append($@"\clcbpat{HeaderBackgroundColor.Value}"); // header
                    }
                    else if (RowBackgroundColor != null && (RowAltBackgroundColor == null || i % 2 == 0))
                    {
                        result.Append($@"\clcbpat{RowBackgroundColor.Value}"); // row colour
                    }
                    else if (RowBackgroundColor != null && RowAltBackgroundColor != null && i % 2 != 0)
                    {
                        result.Append($@"\clcbpat{RowAltBackgroundColor.Value}"); // alt row colour
                    }
                    if (_cells[i][j].IsMerged && _cells[i][j].MergeInfo.RowSpan > 1)
                    {
                        result.Append(_cells[i][j].IsBeginOfRowSpan ? @"\clvmgf" : @"\clvmrg");
                    }
                    switch (_cells[i][j].AlignmentVertical)
                    {
                    case AlignVertical.Top:
                        result.Append(@"\clvertalt");
                        break;

                    case AlignVertical.Middle:
                        result.Append(@"\clvertalc");
                        break;

                    case AlignVertical.Bottom:
                        result.Append(@"\clvertalb");
                        break;
                    }
                    result.AppendLine(@"\cellx" + colRightPos);
                }

                for (var j = 0; j < ColCount; j++)
                {
                    if (!_cells[i][j].IsMerged || _cells[i][j].IsBeginOfColSpan)
                    {
                        result.Append(_cells[i][j].Render());
                    }
                }

                result.AppendLine(@"\row}");
            }

            if (_margins[Direction.Bottom] >= 0)
            {
                result.Append(@"\sl-" + RtfUtility.Pt2Twip(_margins[Direction.Bottom]) + @"\slmult");
            }

            return(result.ToString());
        }
Example #6
0
        public override string Render()
        {
            var rtf = new StringBuilder();

            // ---------------------------------------------------
            // Prologue
            // ---------------------------------------------------
            rtf.AppendLine(@"{\rtf1\ansi\deff0");
            rtf.AppendLine();

            // ---------------------------------------------------
            // Insert font table
            // ---------------------------------------------------
            rtf.AppendLine(@"{\fonttbl");
            for (var i = 0; i < _fontTable.Count; i++)
            {
                rtf.AppendLine(@"{\f" + i + " " + RtfUtility.UnicodeEncode(_fontTable[i]) + ";}");
            }
            rtf.AppendLine("}");
            rtf.AppendLine();

            // ---------------------------------------------------
            // Insert color table
            // ---------------------------------------------------
            rtf.AppendLine(@"{\colortbl");
            rtf.AppendLine(";");
            for (var i = 1; i < _colorTable.Count; i++)
            {
                var c = _colorTable[i];
                rtf.AppendLine(@"\red" + c.Red + @"\green" + c.Green + @"\blue" + c.Blue + ";");
            }
            rtf.AppendLine("}");
            rtf.AppendLine();

            // ---------------------------------------------------
            // Preliminary
            // ---------------------------------------------------
            rtf.AppendLine(@"\deflang" + (int)_lcid + @"\plain\fs"
                           + RtfUtility.Pt2HalfPt(DefaultValue.FontSize) + @"\widowctrl\hyphauto\ftnbj");
            // page size
            rtf.AppendLine(@"\paperw" + RtfUtility.PaperWidthInTwip(_paper, _orientation)
                           + @"\paperh" + RtfUtility.PaperHeightInTwip(_paper, _orientation));
            // page margin
            rtf.AppendLine(@"\margt" + RtfUtility.Pt2Twip(Margins[Direction.Top]));
            rtf.AppendLine(@"\margr" + RtfUtility.Pt2Twip(Margins[Direction.Right]));
            rtf.AppendLine(@"\margb" + RtfUtility.Pt2Twip(Margins[Direction.Bottom]));
            rtf.AppendLine(@"\margl" + RtfUtility.Pt2Twip(Margins[Direction.Left]));
            // orientation
            if (_orientation == PaperOrientation.Landscape)
            {
                rtf.AppendLine(@"\landscape");
            }
            // header/footer
            if (_header != null)
            {
                rtf.Append(_header.Render());
            }
            if (_footer != null)
            {
                rtf.Append(_footer.Render());
            }
            rtf.AppendLine();

            // ---------------------------------------------------
            // Document body
            // ---------------------------------------------------
            rtf.Append(base.Render());

            // ---------------------------------------------------
            // Ending
            // ---------------------------------------------------
            rtf.AppendLine("}");

            return(rtf.ToString());
        }
Example #7
0
        internal string RenderHead()
        {
            var result = new StringBuilder("{");

            if (!string.IsNullOrEmpty(LocalHyperlink))
            {
                result.Append(@"{\field{\*\fldinst HYPERLINK \\l ");
                result.Append("\"" + LocalHyperlink + "\"");
                if (!string.IsNullOrEmpty(LocalHyperlinkTip))
                {
                    result.Append(" \\\\o \"" + LocalHyperlinkTip + "\"");
                }
                result.Append(@"}{\fldrslt{");
            }


            if (Font != null || AnsiFont != null)
            {
                if (Font == null)
                {
                    result.Append(@"\f" + AnsiFont.Value);
                }
                else if (AnsiFont == null)
                {
                    result.Append(@"\f" + Font.Value);
                }
                else
                {
                    result.Append(@"\loch\af" + AnsiFont.Value + @"\hich\af" + AnsiFont.Value
                                  + @"\dbch\af" + Font.Value);
                }
            }
            if (FontSize > 0)
            {
                result.Append(@"\fs" + RtfUtility.Pt2HalfPt(FontSize));
            }
            if (FgColor != null)
            {
                result.Append(@"\cf" + FgColor.Value);
            }
            if (BgColor != null)
            {
                result.Append(@"\chshdng0\chcbpat" + BgColor.Value + @"\cb" + BgColor.Value);
            }

            foreach (var fontStyle in _fontStyleMap)
            {
                if (FontStyle.ContainsStyleAdd(fontStyle.Key))
                {
                    result.Append(@"\" + fontStyle.Value);
                }
                else if (FontStyle.ContainsStyleRemove(fontStyle.Key))
                {
                    result.Append(@"\" + fontStyle.Value + "0");
                }
            }
            if (TwoInOneStyle != TwoInOneStyle.NotEnabled)
            {
                result.Append(@"\twoinone");
                switch (TwoInOneStyle)
                {
                case TwoInOneStyle.None:
                    result.Append("0");
                    break;

                case TwoInOneStyle.Parentheses:
                    result.Append("1");
                    break;

                case TwoInOneStyle.SquareBrackets:
                    result.Append("2");
                    break;

                case TwoInOneStyle.AngledBrackets:
                    result.Append("3");
                    break;

                case TwoInOneStyle.Braces:
                    result.Append("4");
                    break;
                }
            }

            if (result.ToString().Contains(@"\"))
            {
                result.Append(" ");
            }

            if (!string.IsNullOrEmpty(Bookmark))
            {
                result.Append(@"{\*\bkmkstart " + Bookmark + "}");
            }

            return(result.ToString());
        }