Ejemplo n.º 1
0
        /// <summary>
        /// 输出图片类型的元素
        /// </summary>
        /// <param name="element">文档元素对象</param>
        public void WriteImageElement(DomElement element)
        {
            DocumentContentStyle style = element.RuntimeStyle;
            Image img = element.CreateContentImage();

            if (img != null)
            {
                HtmlAttachFile file = this.AddImage(img, null);
                WriteStartElement("img");
                WriteStartStyle();
                WriteDocumentContentStyle(element.RuntimeStyle, element);
                WriteEndStyle();
                WriteAttributeString("src", file.ReferenceCode);
                WriteAttributeString(
                    "width",
                    GraphicsUnitConvert.ToCSSLength(
                        element.Width,
                        element.OwnerDocument.DocumentGraphicsUnit,
                        CssLengthUnit.Pixels));
                WriteAttributeString(
                    "width",
                    GraphicsUnitConvert.ToCSSLength(
                        element.Height,
                        element.OwnerDocument.DocumentGraphicsUnit,
                        CssLengthUnit.Pixels));
                if (element is DomImageElement)
                {
                    string title = ((DomImageElement)element).Title;
                    if (title != null && title.Length > 0)
                    {
                        WriteAttributeString("title", title);
                    }
                }
                WriteEndElement();
            }
        }
Ejemplo n.º 2
0
        //public override void WriteStartDocument()
        //{
        //    base.WriteStartDocument();
        //    this.WriteStartElement("div");
        //}

        //public override void WriteEndDocument()
        //{
        //    this.WriteEndElement();
        //    base.WriteEndDocument();
        //}
        /// <summary>
        /// 输出文档样式
        /// </summary>
        /// <param name="style">文档样式信息对象</param>
        /// <param name="element">相关的文档元素对象</param>
        public void WriteDocumentContentStyle(DocumentContentStyle style, DomElement element)
        {
            if (style == null)
            {
                throw new ArgumentNullException("style");
            }
            GraphicsUnit documentUnit = GraphicsUnit.Document;

            if (this.MainDocument != null)
            {
                documentUnit = this.MainDocument.DocumentGraphicsUnit;
            }
            // 输出边框线
            base.WriteBorderStyle(
                style.BorderLeft,
                style.BorderTop,
                style.BorderRight,
                style.BorderBottom,
                style.BorderColor,
                (int)style.BorderWidth,
                style.BorderStyle);
            // 输出字体
            base.WriteFontStyle(style.Font.Value);
            // 输出内边距
            base.WritePaddingStyle(
                ToPixel(style.PaddingLeft),
                ToPixel(style.PaddingTop),
                ToPixel(style.PaddingRight),
                ToPixel(style.PaddingBottom));

            // 输出外边距
            base.WriteMarginStyle(
                ToPixel(style.MarginLeft),
                ToPixel(style.MarginTop),
                ToPixel(style.MarginRight),
                ToPixel(style.MarginBottom));

            if (element is DomParagraphFlagElement ||
                element is DomParagraphElement)
            {
                // 文本水平对齐方式
                switch (style.Align)
                {
                case DocumentContentAlignment.Left:
                    base.WriteStyleItem("text-align", "left");
                    break;

                case DocumentContentAlignment.Center:
                    base.WriteStyleItem("text-align", "center");
                    break;

                case DocumentContentAlignment.Right:
                    base.WriteStyleItem("text-align", "right");
                    break;

                case DocumentContentAlignment.Justify:
                    base.WriteStyleItem("text-align", "justify");
                    break;

                default:
                    base.WriteStyleItem("text-align", "left");
                    break;
                }
            }
            if (XDependencyObject.HasPropertyValue(style, ContentStyle.PropertyName_BackgroundColor))
            {
                // 输出背景色
                WriteStyleItem("background-color", ColorToString(style.BackgroundColor));
            }
            if (style.BackgroundImage != null && style.BackgroundImage.HasContent)
            {
                // 输出背景图片
                HtmlAttachFile file = base.AddImage(style.BackgroundImage.Value, null);
                WriteStyleItem("background-image", "url(" + file.ReferenceCode + ")");
            }
            if (XDependencyObject.HasPropertyValue(style, ContentStyle.PropertyName_BackgroundPosition))
            {
                // 背景位置
                string strItem = null;
                switch (style.BackgroundPosition)
                {
                case ContentAlignment.TopLeft:
                    strItem = "top left";
                    break;

                case ContentAlignment.TopCenter:
                    strItem = "top center";
                    break;

                case ContentAlignment.TopRight:
                    strItem = "top right";
                    break;

                case ContentAlignment.MiddleLeft:
                    strItem = "center left";
                    break;

                case ContentAlignment.MiddleCenter:
                    strItem = "center center";
                    break;

                case ContentAlignment.MiddleRight:
                    strItem = "center right";
                    break;

                case ContentAlignment.BottomLeft:
                    strItem = "bottom left";
                    break;

                case ContentAlignment.BottomCenter:
                    strItem = "bottom center";
                    break;

                case ContentAlignment.BottomRight:
                    strItem = "bottom right";
                    break;
                }
                WriteStyleItem("background-position", strItem);
            }
            if (style.BackgroundPositionX != 0)
            {
                WriteStyleItem("background-position-x", GraphicsUnitConvert.ToCSSLength(
                                   ToPixel(style.BackgroundPositionX),
                                   documentUnit,
                                   CssLengthUnit.Pixels));
            }
            if (style.BackgroundPositionY != 0)
            {
                WriteStyleItem("background-position-y", GraphicsUnitConvert.ToCSSLength(
                                   ToPixel(style.BackgroundPositionY),
                                   documentUnit,
                                   CssLengthUnit.Pixels));
            }
            if (style.BackgroundRepeat)
            {
                WriteStyleItem("background-repeat", "repeat");
            }
            if (XDependencyObject.HasPropertyValue(style, ContentStyle.PropertyName_Color))
            {
                WriteStyleItem("color", ColorToString(style.Color));
            }
            // 首行缩进
            if (Math.Abs(style.FirstLineIndent) > 0.05)
            {
                WriteStyleItem("text-indent", GraphicsUnitConvert.ToCSSLength(
                                   style.FirstLineIndent,
                                   documentUnit,
                                   CssLengthUnit.Pixels));
            }

            //case ContentStyle.PropertyName_LineSpacing:
            //    {
            //        // 行间距
            //        float ls = style.LineSpacing;
            //        if (Math.Abs(ls) > 0.05)
            //        {

            //        }
            //    }
            //    break;

            // 后置强制分页
            if (style.PageBreakAfter)
            {
                WriteStyleItem("page-break-after", "always");
            }
            // 前置强制分页
            if (style.PageBreakBefore)
            {
                WriteStyleItem("page-break-before", "always");
            }
            // 从右到左排版
            if (style.RightToLeft)
            {
                WriteStyleItem("direction", "rtl");
            }
            // 字符间距
            if (Math.Abs(style.Spacing) > 0.05)
            {
                WriteStyleItem("letter-spacing", GraphicsUnitConvert.ToCSSLength(
                                   style.Spacing,
                                   documentUnit,
                                   CssLengthUnit.Pixels));
            }
            // 垂直显示文本
            if (style.VertialText)
            {
            }

            // 可见性
            if (style.Visible == false)
            {
                WriteStyleItem("visibility", "hidden");
            }
            if (XDependencyObject.HasPropertyValue(style, ContentStyle.PropertyName_Zoom))
            {
                // 缩放比率
                WriteStyleItem("zoom", style.Zoom.ToString());
            }
        }