Ejemplo n.º 1
0
        public override string Render()
        {
            LinkedList <Token> tokList = buildTokenList();
            StringBuilder      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());
        }
Ejemplo n.º 2
0
        public RtfTable AddTable(int rowCount, int colCount, float fontSize)
        {
            float horizontalWidth;

            if (_orientation == PaperOrientation.Portrait)
            {
                horizontalWidth = RtfUtility.paperWidthInPt(_paper, _orientation)
                                  - _margins[Direction.Left] - _margins[Direction.Right];
            }
            else
            {
                horizontalWidth = RtfUtility.paperHeightInPt(_paper, _orientation)
                                  - _margins[Direction.Left] - _margins[Direction.Right];
            }
            return(base.AddTable(rowCount, colCount, horizontalWidth, fontSize));
        }
Ejemplo n.º 3
0
        protected string extractTokenList(LinkedList <Token> tokList)
        {
            LinkedListNode <Token> node;
            StringBuilder          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());
        }
Ejemplo n.º 4
0
        public override string Render()
        {
            StringBuilder rtf = new StringBuilder();

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

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

            // ---------------------------------------------------
            // Insert color table
            // ---------------------------------------------------
            //rtf.AppendLine(@"{\colortbl");
            //rtf.AppendLine(";");
            //for (int i = 1; i < _colorTable.Count; i++) {
            //    RtfColor 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());
        }
Ejemplo n.º 5
0
        internal string renderHead()
        {
            StringBuilder result = new StringBuilder("{");

            if (!string.IsNullOrEmpty(_localHyperlink))
            {
                //result.Append(@"{\field{\*\fldinst HYPERLINK \\l ");
                result.Append(@"{\field{\*\fldinst HYPERLINK ");
                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 (_foregroundColor != null)
            {
                result.Append(@"\cf" + _foregroundColor.Value);
            }

            // Removes underline and hacks foreground
            result.Append(@"\cf1\ulnone");

            if (_backgroundColor != null)
            {
                result.Append(@"\chshdng0\chcbpat" + _backgroundColor.Value + @"\cb" + _backgroundColor.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());
        }
Ejemplo n.º 6
0
        public override string Render()
        {
            StringBuilder 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());
        }
Ejemplo n.º 7
0
        public override string Render()
        {
            StringBuilder 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 (int i = 0; i < _rowCount; i++)
                {
                    for (int 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);
                        }
                    }
                }
            }

            float 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 (int 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 (int j = 0; j < _colCount; j++)
                {
                    if (_cells[i][j].IsMerged && !_cells[i][j].IsBeginOfColSpan)
                    {
                        continue;
                    }
                    float nextCellLeftBorderClearance = j < _colCount - 1 ? cell(i, j + 1).OuterLeftBorderClearance : 0;
                    colAcc += RtfUtility.pt2Twip(cell(i, j).Width);
                    int colRightPos = colAcc;
                    if (nextCellLeftBorderClearance < 0)
                    {
                        colRightPos += RtfUtility.pt2Twip(nextCellLeftBorderClearance);
                        colRightPos  = colRightPos == 0 ? 1 : colRightPos;
                    }

                    // Borders
                    for (Direction d = Direction.Top; d <= Direction.Left; d++)
                    {
                        Border 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 colour
                    if (cell(i, j).BackgroundColour != null)
                    {
                        result.Append(string.Format(@"\clcbpat{0}", cell(i, j).BackgroundColour.Value));                                      // cell.BackGroundColor overrides others
                    }
                    else if (i == 0 && HeaderBackgroundColour != null)
                    {
                        result.Append(string.Format(@"\clcbpat{0}", HeaderBackgroundColour.Value));                                                // header
                    }
                    else if (RowBackgroundColour != null && (RowAltBackgroundColour == null || i % 2 == 0))
                    {
                        result.Append(string.Format(@"\clcbpat{0}", RowBackgroundColour.Value));                                                                                     // row colour
                    }
                    else if (RowBackgroundColour != null && RowAltBackgroundColour != null && i % 2 != 0)
                    {
                        result.Append(string.Format(@"\clcbpat{0}", RowAltBackgroundColour.Value));                                                                                   // alt row colour
                    }
                    if (_cells[i][j].IsMerged && _cells[i][j].MergeInfo.RowSpan > 1)
                    {
                        if (_cells[i][j].IsBeginOfRowSpan)
                        {
                            result.Append(@"\clvmgf");
                        }
                        else
                        {
                            result.Append(@"\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 (int 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());
        }