예제 #1
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());
        }
예제 #2
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());
        }