/// <summary>
        /// 执行插入超链接命令
        /// </summary>
        public static void InsertHyperlick(this HtmlDocument document, HyperlinkObject hyperlink)
        {
            var url = hyperlink.URL.HtmlEncoding();
            var txt = hyperlink.Text.HtmlEncoding();

            if (string.IsNullOrEmpty(txt))
            {
                txt = url;
            }
            string tx = string.Format("<a href=\"{0}\">{1}</a>", url, txt);

            document.InsertHTML(tx);
        }
        /// <summary>
        /// 执行插入表格命令
        /// </summary>
        public static void InsertTable(this HtmlDocument document, TableObject table)
        {
            int    rows    = table.Rows;
            int    cols    = table.Columns;
            string width   = string.Format(" width=\"{0}{1}\"", table.Width, table.WidthUnit.Value);
            string height  = string.Format(" height=\"{0}{1}\"", table.Height, table.HeightUnit.Value);
            string spacing = string.Format(" cellspacing=\"{0}{1}\"", table.Spacing, (table.Spacing > 0 ? table.SpacingUnit.Value : string.Empty));
            string padding = string.Format(" cellspacing=\"{0}{1}\"", table.Padding, (table.Padding > 0 ? table.PaddingUnit.Value : string.Empty));
            string border  = string.Format(" border=\"{0}\"", table.Border);
            string title   = (string.IsNullOrEmpty(table.Title) != false ? string.Format(" title=\"{0}\"", table.Title.HtmlEncoding()) : string.Empty);
            string align   = (table.Alignment != TableAlignment.Default ? string.Format(" align=\"{0}\"", table.Alignment.Value) : string.Empty);

            StringBuilder bx = new StringBuilder();

            bx.AppendFormat("<table{0}{1}{2}{3}{4}{5}{6}>", width, height, spacing, padding, border, align, title);
            for (int i = 0; i < rows; i++)
            {
                bx.Append("<tr>");
                if (i == 0 && (table.HeaderOption == TableHeaderOption.FirstRow || table.HeaderOption == TableHeaderOption.FirstRowAndColumn))
                {
                    for (int j = 0; j < cols; j++)
                    {
                        bx.Append("<th></th>");
                    }
                }
                else
                {
                    for (int j = 0; j < cols; j++)
                    {
                        if (i == 0 && (table.HeaderOption == TableHeaderOption.FirstColumn || table.HeaderOption == TableHeaderOption.FirstRowAndColumn))
                        {
                            bx.Append("<th></th>");
                        }
                        else
                        {
                            bx.Append("<td></td>");
                        }
                    }
                }
                bx.Append("</tr>");
            }
            bx.Append("</table>");
            document.InsertHTML(bx.ToString());
        }
        /// <summary>
        /// 执行插入图像命令
        /// </summary>
        public static void InsertImage(this HtmlDocument document, ImageObject image)
        {
            string hspace = (image.HorizontalSpace > 0 ? "hspace=\"" + image.HorizontalSpace + "\" " : string.Empty);
            string vspace = (image.VerticalSpace > 0 ? "vspace=\"" + image.VerticalSpace + "\" " : string.Empty);
            string border = (image.BorderSize > 0 ? "border=\"" + image.BorderSize + "\" " : string.Empty);
            string align  = (image.Alignment != ImageAlignment.Default ? "align=\"" + image.Alignment.Value + "\" " : string.Empty);
            string title  = (string.IsNullOrEmpty(image.TitleText) == false ? "title=\"" + image.TitleText + "\" " : string.Empty);
            string tx     = string.Empty;

            if (string.IsNullOrEmpty(image.LinkUrl))
            {
                tx = string.Format("<img src=\"{0}\" alt=\"{1}\" width=\"{2}\" height=\"{3}\" {4}{5}{6}{7}{8} />",
                                   image.ImageUrl, image.AltText, image.Width, image.Height, title, hspace, vspace, border, align);
            }
            else
            {
                string url = image.ImageUrl.HtmlEncoding();
                tx = string.Format("<a href=\"{0}\"><img src=\"{1}\" alt=\"{2}\" width=\"{3}\" height=\"{4}\" {5}{6}{7}{8}{9} /></a>",
                                   image.LinkUrl, image.ImageUrl, image.AltText, image.Width, image.Height, title, hspace, vspace, border, align);
            }
            document.InsertHTML(tx);
        }
Example #4
0
 public void InsertText(string text)
 {
     htmldoc.InsertHTML(text);
 }