/// <summary> /// 设置段落样式 /// </summary> /// <param name="newStyle"></param> /// <returns></returns> public DomElementList SetParagraphStyle(DocumentContentStyle newStyle) { DomDocument document = this._DocumentContent.OwnerDocument; Dictionary <DomElement, int> styleIndexs = new Dictionary <DomElement, int>(); if (this.Document.Options.SecurityOptions.EnablePermission) { newStyle.DisableDefaultValue = true; newStyle.CreatorIndex = this.Document.UserHistories.CurrentIndex; newStyle.DeleterIndex = -1; } else { newStyle.DisableDefaultValue = true; newStyle.CreatorIndex = -1; newStyle.DeleterIndex = -1; } foreach (DomParagraphFlagElement p in this.ParagraphsEOFs) { if (document.DocumentControler.CanModify(p)) { DocumentContentStyle rs = (DocumentContentStyle)p.RuntimeStyle.Clone(); if (XDependencyObject.MergeValues(newStyle, rs, true) > 0) { rs.DefaultValuePropertyNames = newStyle.GetDefaultValuePropertyNames(); int newStyleIndex = document.ContentStyles.GetStyleIndex(rs); if (newStyleIndex != p.StyleIndex) { styleIndexs[p] = newStyleIndex; } } } }//foreach if (styleIndexs.Count > 0) { DomElementList result = document.EditorSetParagraphStyle(styleIndexs, true); return(result); } else { return(null); } }
/// <summary> /// 获得运行时的样式 /// </summary> /// <param name="styleIndex"></param> /// <returns></returns> public ContentStyle GetRuntimeStyle(int styleIndex) { ContentStyle style = GetStyle(styleIndex); if (style == _Default) { return(style); } else { if (runtimeStyles == null) { runtimeStyles = new Dictionary <ContentStyle, ContentStyle>(); } if (runtimeStyles.ContainsKey(style)) { return(runtimeStyles[style]); } else { ContentStyle rs = (ContentStyle)style.Clone(); //rs.Merge(this.Default); XDependencyObject.MergeValues(this.Default, rs, false); if (string.IsNullOrEmpty(rs.DefaultValuePropertyNames) == false) { foreach (string name in rs.DefaultValuePropertyNames.Split(',')) { XDependencyObject.RemoveProperty(rs, name); }//foreach } runtimeStyles[style] = rs; return(rs); } } }
/// <summary> /// 设置文档元素的样式 /// </summary> /// <param name="newStyle">新样式</param> /// <returns>是否修改了文档内容</returns> public bool SetElementStyle(DocumentContentStyle newStyle) { Dictionary <DomElement, int> newStyleIndexs = new Dictionary <DomElement, int>(); foreach (DomElement element in this) { DocumentContentStyle rs = (DocumentContentStyle)element.RuntimeStyle.Clone(); if (XDependencyObject.MergeValues(newStyle, rs, true) > 0) { int styleIndex = this.Document.ContentStyles.GetStyleIndex(rs); if (styleIndex != element.StyleIndex) { newStyleIndexs[element] = styleIndex; } } }//foreach if (newStyleIndexs.Count > 0) { this.Document.EditorSetElementStyle(newStyleIndexs, true); return(true); } return(false); }
/// <summary> /// 设置段落样式 /// </summary> /// <param name="newStyle"></param> /// <returns></returns> public DomElementList SetParagraphStyle(DocumentContentStyle newStyle) { CheckState(); Dictionary <DomElement, int> styleIndexs = new Dictionary <DomElement, int>(); foreach (DomParagraphFlagElement p in this.ParagraphsEOFs) { DocumentContentStyle rs = (DocumentContentStyle)p.RuntimeStyle.Clone(); if (XDependencyObject.MergeValues(newStyle, rs, true) > 0) { int newStyleIndex = this.Document.ContentStyles.GetStyleIndex(rs); if (newStyleIndex != p.StyleIndex) { styleIndexs[p] = newStyleIndex; } } }//foreach if (styleIndexs.Count > 0) { this.Document.EditorSetParagraphStyle(styleIndexs, true); } return(this.ParagraphsEOFs); }
//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()); } }
/// <summary> /// 设置多个元素的样式 /// </summary> /// <param name="newStyle">新样式</param> /// <param name="document">文档对象</param> /// <param name="elements">要设置的元素列表</param> /// <returns>操作是否成功</returns> internal static bool SetElementStyle( DocumentContentStyle newStyle, DomDocument document, System.Collections.IEnumerable elements) { if (newStyle == null) { throw new ArgumentNullException("newStyle"); } if (document == null) { throw new ArgumentNullException("document"); } if (elements == null) { throw new ArgumentNullException("elements"); } if (document.Options.SecurityOptions.EnablePermission) { // 运行授权控制,向样式信息添加用户信息 newStyle.DisableDefaultValue = false; newStyle.CreatorIndex = document.UserHistories.CurrentIndex; newStyle.DeleterIndex = -1; } else { // 去除授权控制相关信息 newStyle.DisableDefaultValue = false; newStyle.CreatorIndex = -1; newStyle.DeleterIndex = -1; } Dictionary <DomElement, int> newStyleIndexs = new Dictionary <DomElement, int>(); DomElementList parents = new DomElementList(); DomElement lastParent = null; foreach (DomElement element in elements) { DomElement parent = element.Parent; //if (parent != lastParent) { // 记录所有涉及到的父元素 lastParent = parent; bool addParent = false; //if (element is XTextFieldBorderElement) //{ // addParent = true; //} //else if (addParent) { if (parents.Contains(element.Parent) == false) { parents.Add(element.Parent); } } }//if DocumentContentStyle rs = (DocumentContentStyle)element.RuntimeStyle.Clone(); if (XDependencyObject.MergeValues(newStyle, rs, true) > 0) { rs.DefaultValuePropertyNames = newStyle.GetDefaultValuePropertyNames(); int styleIndex = document.ContentStyles.GetStyleIndex(rs); if (styleIndex != element.StyleIndex) { newStyleIndexs[element] = styleIndex; } if (element.ShadowElement != null && styleIndex != element.ShadowElement.StyleIndex) { newStyleIndexs[element.ShadowElement] = styleIndex; } } }//foreach if (parents.Count > 0) { // 对涉及到的父元素设置样式 foreach (DomElement element in parents) { DocumentContentStyle rs = (DocumentContentStyle)element.RuntimeStyle.Clone(); if (XDependencyObject.MergeValues(newStyle, rs, true) > 0) { rs.DefaultValuePropertyNames = newStyle.GetDefaultValuePropertyNames(); int styleIndex = document.ContentStyles.GetStyleIndex(rs); if (styleIndex != element.StyleIndex) { newStyleIndexs[element] = styleIndex; } } } } if (newStyleIndexs.Count > 0) { DomElementList result = document.EditorSetElementStyle(newStyleIndexs, true); if (result != null && result.Count > 0) { return(true); } } return(false); }
private void CreateElements( HTMLElement rootHtmlElement, DomElement rootDomElement, DocumentContentStyle currentStyle) { if (rootHtmlElement.ChildNodes == null) { return; } foreach (HTMLElement element in rootHtmlElement.ChildNodes) { if (element is HTMLCommentElement) { // 忽略注释 continue; } string tagName = element.FixTagName; switch (tagName) { case "div": { CreateElements(element, rootDomElement, currentStyle); DomElement p = this.DomDocument.CreateElement(typeof(DomParagraphFlagElement)); p.StyleIndex = this.DomDocument.ContentStyles.GetStyleIndex(currentStyle); rootDomElement.Elements.Add(p); } break; case "ul": // 原点式列表 case "ol": // 数值式列表 foreach (HTMLElement li in element.ChildNodes) { if (li.FixTagName == "li") { CreateElements(li, rootDomElement, currentStyle); DomParagraphFlagElement flag = (DomParagraphFlagElement)this.DomDocument.CreateElement(typeof(DomParagraphFlagElement)); DocumentContentStyle s = (DocumentContentStyle)currentStyle.Clone(); if (tagName == "ul") { s.BulletedList = true; } else { s.NumberedList = true; } flag.StyleIndex = this.DomDocument.ContentStyles.GetStyleIndex(s); rootDomElement.Elements.Add(flag); } //if } //foreach break; case "pre": { // 预览文本 DocumentContentStyle s = CreateStyle(element, currentStyle); DomElementList list = this.DomDocument.CreateTextElements(element.InnerText, null, s); rootDomElement.Elements.AddRange(list); DomElement p = this.DomDocument.CreateElement(typeof(DomParagraphFlagElement)); p.StyleIndex = this.DomDocument.ContentStyles.GetStyleIndex(currentStyle); rootDomElement.Elements.Add(p); break; } case "img": { // 图片 DocumentContentStyle s = CreateStyle(element, currentStyle); s.BorderColor = Color.Black; s.BorderLeft = true; s.BorderTop = true; s.BorderRight = true; s.BorderBottom = true; s.BorderWidth = 0; if (element.HasAttribute("border")) { s.BorderWidth = ToInt32(element.GetAttribute("border")); } DomImageElement img = (DomImageElement)this.DomDocument.CreateElement(typeof(DomImageElement)); img.StyleIndex = this.DomDocument.ContentStyles.GetStyleIndex(s); if (element.HasAttribute("width")) { img.Width = (float )ToLength(element.GetAttribute("width")); } if (element.HasAttribute("height")) { img.Height = ( float )ToLength(element.GetAttribute("height")); } if (element.HasAttribute("id")) { img.ID = element.GetAttribute("id"); } if (element.HasAttribute("alt")) { img.Alt = element.GetAttribute("alt"); } if (element.HasAttribute("title")) { img.Title = element.GetAttribute("title"); } if (element.HasAttribute("src")) { XImageValue v = new XImageValue(); string url = this.HtmlDocument.GetAbsoluteURL(element.GetAttribute("src")); try { string msg = string.Format(WriterStrings.Downloading_URL, url); if (this.DomDocument.EditorControl != null) { this.DomDocument.EditorControl.SetStatusText(msg); } if (this.DomDocument.Options.BehaviorOptions.DebugMode) { System.Diagnostics.Debug.Write(msg); } int len = v.Load(url); if (this.DomDocument.Options.BehaviorOptions.DebugMode) { System.Diagnostics.Debug.WriteLine(WriterUtils.FormatByteSize(len)); } } catch (Exception ext) { img.Alt = url + ":" + ext.Message; if (this.DomDocument.Options.BehaviorOptions.DebugMode) { System.Diagnostics.Debug.WriteLine(WriterStrings.Fail); } } if (this.DomDocument.EditorControl != null) { this.DomDocument.EditorControl.SetStatusText(null); } if (v.HasContent == false) { img.Alt = url; if (img.Width == 0) { img.Width = 300; } if (img.Height == 0) { img.Height = 150; } } img.Image = v; } if (img.Width == 0 || img.Height == 0) { img.UpdateSize(); } rootDomElement.Elements.Add(img); } break; case "#text": { // 纯文本片段 string text = DCSoft.Common.StringFormatHelper.NormalizeSpace(element.Text); text = System.Web.HttpUtility.HtmlDecode(text); if (string.IsNullOrEmpty(text) == false) { DomElementList cs = this._DomDocument.CreateTextElements( text, currentStyle, currentStyle); if (cs != null && cs.Count > 0) { rootDomElement.Elements.AddRange(cs); } } } break; case "p": { // 段落 DocumentContentStyle ps = CreateStyle(element, currentStyle); CreateElements(element, rootDomElement, ps); DomParagraphFlagElement flag = ( DomParagraphFlagElement )_DomDocument.CreateElement(typeof(DomParagraphFlagElement)); flag.StyleIndex = _DomDocument.ContentStyles.GetStyleIndex(ps); rootDomElement.Elements.Add(flag); } break; case "br": { // 软回车 DomLineBreakElement lb = (DomLineBreakElement)_DomDocument.CreateElement(typeof(DomLineBreakElement)); rootDomElement.Elements.Add(lb); } break; case "sup": { // 上标 DocumentContentStyle ss = ( DocumentContentStyle )currentStyle.Clone(); ss.Superscript = true; CreateElements(element, rootDomElement, ss); } break; case "sub": { // 下标 DocumentContentStyle ss = (DocumentContentStyle)currentStyle.Clone(); ss.Subscript = true; CreateElements(element, rootDomElement, ss); } break; case "strong": case "b": { // 粗体 DocumentContentStyle ss = (DocumentContentStyle)currentStyle.Clone(); ss.Bold = true; CreateElements(element, rootDomElement, ss); } break; case "i": { // 斜体 DocumentContentStyle ss = (DocumentContentStyle)currentStyle.Clone(); ss.Italic = true; CreateElements(element, rootDomElement, ss); } break; case "strike": { // 删除线 DocumentContentStyle ss = (DocumentContentStyle)currentStyle.Clone(); ss.Strikeout = true; CreateElements(element, rootDomElement, ss); } break; case "a": { // 超链接 DocumentContentStyle ss = (DocumentContentStyle)currentStyle.Clone(); ss.Color = Color.Blue; CreateElements(element, rootDomElement, ss); } break; case "font": { // 字体 DocumentContentStyle ss = CreateStyle(element, currentStyle); if (element.HasAttribute("color")) { // 文字颜色 ss.Color = ToColor(element.GetAttribute("color"), Color.Black); } if (element.HasAttribute("face")) { // 字体名称 ss.FontName = GetFontName(element.GetAttribute("face")); } if (element.HasAttribute("size")) { // 文字大小 int size = ToInt32(element.GetAttribute("size")); switch (size) { case 1: ss.FontSize = 7; break; case 2: ss.FontSize = 10; break; case 3: ss.FontSize = 12; break; case 4: ss.FontSize = 14; break; case 5: ss.FontSize = 18; break; case 6: ss.FontSize = 24; break; case 7: ss.FontSize = 35; break; } } CreateElements(element, rootDomElement, ss); } break; case "h1": case "h2": case "h3": case "h4": case "h5": case "h6": { // 标题 rootDomElement.Elements.Add(this.DomDocument.CreateElement(typeof(DomParagraphFlagElement))); float fz = 9; switch (tagName) { case "h1": fz = 24; break; case "h2": fz = 18; break; case "h3": fz = 13; break; case "h4": fz = 12; break; case "h5": fz = 10; break; case "h6": fz = 8; break; } DocumentContentStyle ss = CreateStyle(element, currentStyle); if (XDependencyObject.HasPropertyValue(ss, "FontSize")) { ss.FontSize = fz; } CreateElements(element, rootDomElement, ss); rootDomElement.Elements.Add(this.DomDocument.CreateElement(typeof(DomParagraphFlagElement))); } break; default: { DocumentContentStyle ds = CreateStyle(element, (DocumentContentStyle)currentStyle); CreateElements(element, rootDomElement, ds); } break; } //switch } //foreach }
private void SetStyleProperty( object sender, WriterCommandEventArgs args, string commandName) { if (args.Mode == WriterCommandEventMode.QueryState) { DocumentContentStyle style = GetCurrentStyle(args.Document); args.Enabled = args.DocumentControler != null && args.DocumentControler.Snapshot.CanModifySelection; switch (commandName) { case StandardCommandNames.Bold: args.Checked = style.Bold; break; case StandardCommandNames.BorderBottom: args.Checked = style.BorderBottom; break; case StandardCommandNames.BorderLeft: args.Checked = style.BorderLeft; break; case StandardCommandNames.BorderRight: args.Checked = style.BorderRight; break; case StandardCommandNames.BorderTop: args.Checked = style.BorderTop; break; case StandardCommandNames.Italic: args.Checked = style.Italic; break; case StandardCommandNames.Strikeout: args.Checked = style.Strikeout; break; case StandardCommandNames.Subscript: args.Checked = style.Subscript; break; case StandardCommandNames.Superscript: args.Checked = style.Superscript; break; case StandardCommandNames.Underline: args.Checked = style.Underline; break; default: args.Enabled = false; return; } } else if (args.Mode == WriterCommandEventMode.Invoke) { DocumentContentStyle cs = GetCurrentStyle(args.Document); DocumentContentStyle ns = args.Document.CreateDocumentContentStyle(); ns.DisableDefaultValue = true; switch (commandName) { case StandardCommandNames.Bold: if (args.Parameter is bool) { ns.Bold = (bool)args.Parameter; } else { ns.Bold = !cs.Bold; } break; case StandardCommandNames.BorderBottom: if (args.Parameter is bool) { ns.BorderBottom = (bool)args.Parameter; } else { ns.BorderBottom = !cs.BorderBottom; } break; case StandardCommandNames.BorderLeft: if (args.Parameter is bool) { ns.BorderLeft = (bool)args.Parameter; } else { ns.BorderLeft = !cs.BorderLeft; } break; case StandardCommandNames.BorderRight: if (args.Parameter is bool) { ns.BorderRight = (bool)args.Parameter; } else { ns.BorderRight = !cs.BorderRight; } break; case StandardCommandNames.BorderTop: if (args.Parameter is bool) { ns.BorderTop = (bool)args.Parameter; } else { ns.BorderTop = !cs.BorderTop; } break; case StandardCommandNames.Italic: if (args.Parameter is bool) { ns.Italic = (bool)args.Parameter; } else { ns.Italic = !cs.Italic; } break; case StandardCommandNames.Strikeout: if (args.Parameter is bool) { ns.Strikeout = (bool)args.Parameter; } else { ns.Strikeout = !cs.Strikeout; } break; case StandardCommandNames.Subscript: if (args.Parameter is bool) { ns.Subscript = (bool)args.Parameter; } else { ns.Subscript = !cs.Subscript; } ns.Superscript = false; break; case StandardCommandNames.Superscript: ns.Subscript = false; if (args.Parameter is bool) { ns.Superscript = (bool)args.Parameter; } else { ns.Superscript = !cs.Superscript; } break; case StandardCommandNames.Underline: if (args.Parameter is bool) { ns.Underline = (bool)args.Parameter; } else { ns.Underline = !cs.Underline; } break; case StandardCommandNames.Color: if (args.Parameter is Color) { ns.Color = (Color)args.Parameter; } break; case StandardCommandNames.BackColor: if (args.Parameter is Color) { ns.BackgroundColor = (Color)args.Parameter; } break; case StandardCommandNames.Font: if (args.Parameter is Font) { ns.Font = new XFontValue((Font)args.Parameter); } else if (args.Parameter is XFontValue) { ns.Font = ((XFontValue)args.Parameter).Clone(); } break; case StandardCommandNames.FontName: if (args.Parameter is string) { ns.FontName = (string)args.Parameter; args.Document.EditorCurrentStyle.FontName = ns.FontName; //if (args.EditorControl != null) //{ // args.EditorControl.Focus(); //} } break; case StandardCommandNames.FontSize: if (args.Parameter is string) { ns.FontSize = FontSizeInfo.GetFontSize((string)args.Parameter, args.Document.DefaultStyle.FontSize); } else if (args.Parameter is float || args.Parameter is double || args.Parameter is int) { ns.FontSize = Convert.ToSingle(args.Parameter); } args.Document.EditorCurrentStyle.FontSize = ns.FontSize; //if (args.EditorControl != null) //{ // args.EditorControl.Focus(); //} break; default: throw new NotSupportedException(commandName); }//switch XDependencyObject.MergeValues(ns, args.Document.EditorCurrentStyle, true); if (args.Document.Selection.Length != 0) { args.Document.BeginLogUndo(); args.Document.Selection.SetElementStyle(ns); args.Document.EndLogUndo(); args.Document.OnSelectionChanged(); args.Document.OnDocumentContentChanged(); } //args.Document.CurrentStyle.Underline = v; } }