/// <summary>
 /// Gets the RTF document content base list.
 /// </summary>
 /// <param name="rtfHtmlNodeList">The RTF HTML node list.</param>
 /// <returns></returns>
 private static RtfDocumentContentBase[] GetRtfDocumentContents(List <RtfHtmlNode> rtfHtmlNodeList)
 {
     RtfDocumentContentBase[] rtfDocumentContents = new RtfDocumentContentBase[rtfHtmlNodeList.Count];
     for (int nodeCount = 0; nodeCount < rtfHtmlNodeList.Count; nodeCount++)
     {
         RtfHtmlNode        rtfHtmlNode        = rtfHtmlNodeList[nodeCount];
         RtfParagraphFormat rtfParagraphFormat = new RtfParagraphFormat(FontSize,
                                                                        string.IsNullOrEmpty(rtfHtmlNode.RtfParagraphFormat)
                 ? RtfTextAlign.Justified
                 : GetRtfTextAlign(rtfHtmlNode.RtfParagraphFormat));
         RtfFormattedParagraph rtfFormattedParagraph = new RtfFormattedParagraph(rtfParagraphFormat);
         foreach (var rtfHtmlItem in rtfHtmlNode.RtfHtmlItem)
         {
             //Add text in paragraph
             if (!string.IsNullOrEmpty(rtfHtmlItem.HtmlText))
             {
                 rtfFormattedParagraph.AppendText(new RtfFormattedText(rtfHtmlItem.HtmlText, rtfHtmlItem.RtfFormattedText));
             }
             //Add image in paragraph
             if (rtfHtmlItem.HtmlImage != null)
             {
                 RtfImage rtfImage = new RtfImage(rtfHtmlItem.HtmlImage, RtfImageFormat.Wmf, rtfHtmlItem.ImageHeight, rtfHtmlItem.ImageWidth);
                 rtfFormattedParagraph.AppendText(rtfImage);
             }
         }
         rtfFormattedParagraph.Formatting.SpaceAfter = TwipConverter.ToTwip(10F, MetricUnit.Point);
         rtfDocumentContents[nodeCount] = rtfFormattedParagraph;
     }
     return(rtfDocumentContents);
 }
示例#2
0
        /**
         * Takes an Element subclass and returns the correct IRtfBasicElement
         * subclass, that wraps the Element subclass.
         *
         * @param element The Element to wrap
         * @return A IRtfBasicElement wrapping the Element
         * @throws DocumentException
         */
        public IRtfBasicElement MapElement(IElement element)
        {
            IRtfBasicElement rtfElement = null;

            if (element is IRtfBasicElement)
            {
                rtfElement = (IRtfBasicElement)element;
                rtfElement.SetRtfDocument(this.rtfDoc);
                return(rtfElement);
            }
            switch (element.Type)
            {
            case Element.CHUNK:
                if (((Chunk)element).GetImage() != null)
                {
                    rtfElement = new RtfImage(rtfDoc, ((Chunk)element).GetImage());
                }
                else if (((Chunk)element).HasAttributes() && ((Chunk)element).Attributes.ContainsKey(Chunk.NEWPAGE))
                {
                    rtfElement = new RtfNewPage(rtfDoc);
                }
                else
                {
                    rtfElement = new RtfChunk(rtfDoc, (Chunk)element);
                }
                break;

            case Element.PHRASE:
                rtfElement = new RtfPhrase(rtfDoc, (Phrase)element);
                break;

            case Element.PARAGRAPH:
                rtfElement = new RtfParagraph(rtfDoc, (Paragraph)element);
                break;

            case Element.ANCHOR:
                rtfElement = new RtfAnchor(rtfDoc, (Anchor)element);
                break;

            case Element.ANNOTATION:
                rtfElement = new RtfAnnotation(rtfDoc, (Annotation)element);
                break;

            case Element.IMGRAW:
            case Element.IMGTEMPLATE:
            case Element.JPEG:
                rtfElement = new RtfImage(rtfDoc, (Image)element);
                break;

            case Element.AUTHOR:
            case Element.SUBJECT:
            case Element.KEYWORDS:
            case Element.TITLE:
            case Element.PRODUCER:
            case Element.CREATIONDATE:
                rtfElement = new RtfInfoElement(rtfDoc, (Meta)element);
                break;

            case Element.LIST:
                rtfElement = new RtfList(rtfDoc, (List)element);
                break;

            case Element.LISTITEM:
                rtfElement = new RtfListItem(rtfDoc, (ListItem)element);
                break;

            case Element.SECTION:
                rtfElement = new RtfSection(rtfDoc, (Section)element);
                break;

            case Element.CHAPTER:
                rtfElement = new RtfChapter(rtfDoc, (Chapter)element);
                break;

            case Element.TABLE:
                try {
                    rtfElement = new TB.RtfTable(rtfDoc, (Table)element);
                }
                catch (InvalidCastException) {
                    rtfElement = new TB.RtfTable(rtfDoc, ((SimpleTable)element).CreateTable());
                }
                break;
            }

            return(rtfElement);
        }
示例#3
0
        public Report()
        {
            rtf = new RtfDocument();
            rtf.FontTable.Add(new RtfFont("Calibri"));
            rtf.FontTable.Add(new RtfFont("Constantia"));
            rtf.ColorTable.AddRange(new RtfColor[] {
                new RtfColor(Color.Red),
                new RtfColor(0, 0, 255)
            });

            RtfParagraphFormatting LeftAligned12 = new RtfParagraphFormatting(12, RtfTextAlign.Left);
            RtfParagraphFormatting Centered10    = new RtfParagraphFormatting(10, RtfTextAlign.Center);

            RtfFormattedParagraph header = new RtfFormattedParagraph(new RtfParagraphFormatting(16, RtfTextAlign.Center));
            RtfFormattedParagraph p1     = new RtfFormattedParagraph(new RtfParagraphFormatting(12, RtfTextAlign.Left));

            RtfTable t = new RtfTable(RtfTableAlign.Center, 2, 3);

            header.Formatting.SpaceAfter = TwipConverter.ToTwip(12F, MetricUnit.Point);
            header.AppendText("Calibri ");
            header.AppendText(new RtfFormattedText("Bold", RtfCharacterFormatting.Bold));

            t.Width            = TwipConverter.ToTwip(5, MetricUnit.Centimeter);
            t.Columns[1].Width = TwipConverter.ToTwip(2, MetricUnit.Centimeter);

            foreach (RtfTableRow row in t.Rows)
            {
                row.Height = TwipConverter.ToTwip(2, MetricUnit.Centimeter);
            }

            t.MergeCellsVertically(1, 0, 2);

            t.DefaultCellStyle = new RtfTableCellStyle(RtfBorderSetting.None, Centered10);

            t[0, 0].Definition.Style = new RtfTableCellStyle(RtfBorderSetting.None, LeftAligned12, RtfTableCellVerticalAlign.Bottom);
            t[0, 0].AppendText("Bottom");

            t[1, 0].Definition.Style = new RtfTableCellStyle(RtfBorderSetting.Left, Centered10, RtfTableCellVerticalAlign.Center, RtfTableCellTextFlow.BottomToTopLeftToRight);
            t[1, 1].Definition.Style = t[1, 0].Definition.Style;
            t[1, 0].AppendText("Vertical");

            t[0, 1].Formatting = new RtfParagraphFormatting(10, RtfTextAlign.Center);
            t[0, 1].Formatting.TextColorIndex = 1;
            t[0, 1].AppendText(new RtfFormattedText("Black ", 0));
            t[0, 1].AppendText("Red ");
            t[0, 1].AppendText(new RtfFormattedText("Blue", 2));

            t[0, 2].AppendText("Normal");
            t[1, 2].AppendText(new RtfFormattedText("Italic", RtfCharacterFormatting.Caps | RtfCharacterFormatting.Italic));
            t[1, 2].AppendParagraph("+");
            t[1, 2].AppendParagraph(new RtfFormattedText("Caps", RtfCharacterFormatting.Caps | RtfCharacterFormatting.Italic));

            p1.Formatting.FontIndex   = 1;
            p1.Formatting.IndentLeft  = TwipConverter.ToTwip(6.05F, MetricUnit.Centimeter);
            p1.Formatting.SpaceBefore = TwipConverter.ToTwip(6F, MetricUnit.Point);
            p1.AppendText("Constantia ");
            p1.AppendText(new RtfFormattedText("Superscript", RtfCharacterFormatting.Superscript));
            p1.AppendParagraph(new RtfFormattedText("Inline", -1, 8));
            p1.AppendText(new RtfFormattedText(" font size ", -1, 14));
            p1.AppendText(new RtfFormattedText("change", -1, 8));

            RtfImage picture = new RtfImage(Properties.Resources.lemon, RtfImageFormat.Wmf);

            picture.ScaleX = 50;
            picture.ScaleY = 50;

            p1.AppendParagraph(picture);

            RtfFormattedText linkText = new RtfFormattedText("View article", RtfCharacterFormatting.Underline, 2);

            linkText.BackgroundColorIndex = 1;
            p1.AppendParagraph(new RtfHyperlink("http://www.codeproject.com/KB/cs/RtfConstructor.aspx", linkText));

            RtfFormattedParagraph p2 = new RtfFormattedParagraph();

            p2.Formatting = new RtfParagraphFormatting(10);

            p2.Tabs.Add(new RtfTab(TwipConverter.ToTwip(2.5F, MetricUnit.Centimeter), RtfTabKind.Decimal));
            p2.Tabs.Add(new RtfTab(TwipConverter.ToTwip(5F, MetricUnit.Centimeter), RtfTabKind.FlushRight, RtfTabLead.Dots));
            p2.Tabs.Add(new RtfTab(TwipConverter.ToTwip(7.5F, MetricUnit.Centimeter)));
            p2.Tabs.Add(new RtfTab(TwipConverter.ToTwip(10F, MetricUnit.Centimeter), RtfTabKind.Centered));
            p2.Tabs.Add(new RtfTab(TwipConverter.ToTwip(15F, MetricUnit.Centimeter), RtfTabLead.Hyphens));

            p2.Tabs[2].Bar = true;

            p2.AppendText("One");
            p2.AppendText(new RtfTabCharacter());
            p2.AppendText("Two");
            p2.AppendText(new RtfTabCharacter());
            p2.AppendText("Three");
            p2.AppendText(new RtfTabCharacter());
            p2.AppendText("Five");
            p2.AppendText(new RtfTabCharacter());
            p2.AppendText("Six");

            rtf.Contents.AddRange(new RtfDocumentContentBase[] {
                header,
                t,
                p1,
                p2,
            });
        }