using DocumentFormat.OpenXml.Drawing; using DocumentFormat.OpenXml.Packaging; using HTEXLib.Helpers.ColorHelpers; using HTEXLib.Helpers.ShapeHelpers; using HTEXLib.Models.Inner; using OpenXmlPowerTools; using System; using System.Collections.Generic; using System.Drawing; using System.Globalization; using System.IO; using System.Linq; using System.Text; using System.Xml; using System.Xml.Linq; using System.Xml.XPath; namespace HTEXLib.DOCX.OpenXmlTool { public static class WORDHelper { public static XmlNamespaceManager xmlnsManager; public static IEnumerable GetWordNodeListByPath(this XElement xElement, string expression) { return xElement.XPathSelectElements(expression, xmlnsManager); } public static XElement GetWordNodeByPath(this XElement xElement, string expression) { return xElement.XPathSelectElement(expression, xmlnsManager); } static WORDHelper() { xmlnsManager = new XmlNamespaceManager(new NameTable()); xmlnsManager.AddNamespace("activex", "http://schemas.microsoft.com/office/2006/activeX"); xmlnsManager.AddNamespace("biblio", "http://schemas.microsoft.com/office/word/2004/10/bibliography"); xmlnsManager.AddNamespace("ink", "http://schemas.microsoft.com/ink/2010/main"); xmlnsManager.AddNamespace("wne", "http://schemas.microsoft.com/office/word/2006/wordml"); xmlnsManager.AddNamespace("a", "http://schemas.openxmlformats.org/drawingml/2006/main"); xmlnsManager.AddNamespace("a14", "http://schemas.microsoft.com/office/drawing/2010/main"); xmlnsManager.AddNamespace("c", "http://schemas.openxmlformats.org/drawingml/2006/chart"); xmlnsManager.AddNamespace("cdr", "http://schemas.openxmlformats.org/drawingml/2006/chartDrawing"); xmlnsManager.AddNamespace("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006"); xmlnsManager.AddNamespace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"); xmlnsManager.AddNamespace("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"); xmlnsManager.AddNamespace("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"); xmlnsManager.AddNamespace("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape"); xmlnsManager.AddNamespace("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"); xmlnsManager.AddNamespace("cx", "http://schemas.microsoft.com/office/drawing/2014/chartex"); xmlnsManager.AddNamespace("cx1", "http://schemas.microsoft.com/office/drawing/2015/9/8/chartex"); xmlnsManager.AddNamespace("cx2", "http://schemas.microsoft.com/office/drawing/2015/10/21/chartex"); xmlnsManager.AddNamespace("cx3", "http://schemas.microsoft.com/office/drawing/2016/5/9/chartex"); xmlnsManager.AddNamespace("cx4", "http://schemas.microsoft.com/office/drawing/2016/5/10/chartex"); xmlnsManager.AddNamespace("cx5", "http://schemas.microsoft.com/office/drawing/2016/5/11/chartex"); xmlnsManager.AddNamespace("cx6", "http://schemas.microsoft.com/office/drawing/2016/5/12/chartex"); xmlnsManager.AddNamespace("cx7", "http://schemas.microsoft.com/office/drawing/2016/5/13/chartex"); xmlnsManager.AddNamespace("cx8", "http://schemas.microsoft.com/office/drawing/2016/5/14/chartex"); xmlnsManager.AddNamespace("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006"); xmlnsManager.AddNamespace("aink", "http://schemas.microsoft.com/office/drawing/2016/ink"); xmlnsManager.AddNamespace("am3d", "http://schemas.microsoft.com/office/drawing/2017/model3d"); xmlnsManager.AddNamespace("o", "urn:schemas-microsoft-com:office:office"); xmlnsManager.AddNamespace("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"); xmlnsManager.AddNamespace("m", "http://schemas.openxmlformats.org/officeDocument/2006/math"); xmlnsManager.AddNamespace("v", "urn:schemas-microsoft-com:vml"); xmlnsManager.AddNamespace("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"); xmlnsManager.AddNamespace("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"); xmlnsManager.AddNamespace("w10", "urn:schemas-microsoft-com:office:word"); xmlnsManager.AddNamespace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"); xmlnsManager.AddNamespace("w14", "http://schemas.microsoft.com/office/word/2010/wordml"); xmlnsManager.AddNamespace("w15", "http://schemas.microsoft.com/office/word/2012/wordml"); xmlnsManager.AddNamespace("w16cex", "http://schemas.microsoft.com/office/word/2018/wordml/cex"); xmlnsManager.AddNamespace("w16cid", "http://schemas.microsoft.com/office/word/2016/wordml/cid"); xmlnsManager.AddNamespace("w16", "http://schemas.microsoft.com/office/word/2018/wordml"); xmlnsManager.AddNamespace("w16se", "http://schemas.microsoft.com/office/word/2015/wordml/symex"); xmlnsManager.AddNamespace("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"); xmlnsManager.AddNamespace("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk"); xmlnsManager.AddNamespace("wne", "http://schemas.microsoft.com/office/word/2006/wordml"); xmlnsManager.AddNamespace("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape"); xmlnsManager.AddNamespace("pt14", "http://powertools.codeplex.com/2011"); } /// /// 处理 LineReference 轮廓 a:lnRef /// 处理 FillReference 填充 a:fillRef /// 处理 EffectReference 效果 a:effectRef /// 处理 FontReference 字体 a:fontRef /// /// public static SlideColor DoShapeStyle(DocumentFormat.OpenXml.Office2010.Word.DrawingShape.ShapeStyle shapeStyle, WordprocessingDocument wordDoc) { if (shapeStyle == null) { return null; } string LineColor = DoMatrixReferenceColors(shapeStyle.LineReference, wordDoc); string FillColor = DoMatrixReferenceColors(shapeStyle.FillReference, wordDoc); string EffectColor = DoMatrixReferenceColors(shapeStyle.EffectReference, wordDoc); string FontColor = FontReferenceColors(shapeStyle.FontReference, wordDoc); return new SlideColor { LineColor = LineColor, FillColor = FillColor, EffectColor = EffectColor, FontColor = FontColor }; } public static string DoMatrixReferenceColors(StyleMatrixReferenceType Color, WordprocessingDocument slide) { string FontColor = ""; if (Color == null) { return FontColor; } if (Color.RgbColorModelHex != null) { FontColor = ReadColor(Color.RgbColorModelHex); } else if (Color.RgbColorModelPercentage != null) { FontColor = ReadColor(Color.RgbColorModelPercentage); } else if (Color.SystemColor != null) { FontColor = ReadColor(Color.SystemColor); } if (Color.SchemeColor != null) { FontColor = ReadColor(Color.SchemeColor, slide); } if (Color.HslColor != null) { FontColor = ReadColor(Color.HslColor); } if (Color.PresetColor != null) { FontColor = ReadColor(Color.PresetColor); } return FontColor; } public static string FontReferenceColors(FontReference Color, WordprocessingDocument slide) { string FontColor = ""; if (Color.RgbColorModelHex != null) { FontColor = ReadColor(Color.RgbColorModelHex); } else if (Color.RgbColorModelPercentage != null) { FontColor = ReadColor(Color.RgbColorModelPercentage); } else if (Color.SystemColor != null) { FontColor = ReadColor(Color.SystemColor); } if (Color.SchemeColor != null) { FontColor = ReadColor(Color.SchemeColor, slide); } if (Color.HslColor != null) { FontColor = ReadColor(Color.HslColor); } if (Color.PresetColor != null) { FontColor = ReadColor(Color.PresetColor); } return FontColor; } public static HTEXLib.Models.HTEX.ShapeStyle DoShapeProperties(WordprocessingDocument wordDoc, DocumentFormat.OpenXml.Drawing.Charts.ShapeProperties shapeProperties, XElement element, Func ImageHandler, Position position) { if (shapeProperties == null) { return null; } //Outline var lnNode = shapeProperties.GetFirstChild(); Border border = null; if (lnNode != null) { border = DoOutline(lnNode, wordDoc); } Fill fill = new Fill() { type = -1 }; var gradFill = shapeProperties.GetFirstChild(); if (gradFill != null) { fill.type = 2; fill.gradientFill = DoGradientFill(gradFill, wordDoc); } var noFill = shapeProperties.GetFirstChild(); if (noFill != null) { fill.type = 0; } var pattFill = shapeProperties.GetFirstChild(); if (pattFill != null) { HtexPattFill htexPattFill = DoPattFill(pattFill, wordDoc); fill.type = 4; fill.pattFill = htexPattFill; } var solidFill = shapeProperties.GetFirstChild(); if (solidFill != null) { fill.type = 1; fill.solidFill = DoSolidFill(solidFill); } var groupFill = shapeProperties.GetFirstChild(); if (groupFill != null) { fill.type = 5; // fill.solidFill = ReadSolidFillColors(new SolidFill(solidFill.ToString()), slide); } var blipFill = shapeProperties.GetFirstChild(); if (blipFill != null) { fill.type = 3; fill.blipFill = DoBlipFill(wordDoc, blipFill, ImageHandler, element, position); // fill.solidFill = ReadSolidFillColors(new SolidFill(solidFill.ToString()), slide); } var EffectList = shapeProperties.GetFirstChild(); // var effect = doEffect(EffectList,partForm); var EffectDag = shapeProperties.GetFirstChild(); var Scene3DType = shapeProperties.GetFirstChild(); var Shape3DType = shapeProperties.GetFirstChild(); var ShapePropertiesExtensionList = shapeProperties.GetFirstChild(); return new HTEXLib.Models.HTEX.ShapeStyle { fill = fill, border = border/*, effect = effect */}; } public static HTEXLib.Models.HTEX.ShapeStyle DoShapeProperties(WordprocessingDocument wordDoc, DocumentFormat.OpenXml.Drawing.Charts.ChartShapeProperties shapeProperties, XElement element, Func ImageHandler, Position position) { if (shapeProperties == null) { return null; } //Outline var lnNode = shapeProperties.GetFirstChild(); Border border = null; if (lnNode != null) { border = DoOutline(lnNode, wordDoc); } Fill fill = new Fill() { type = -1 }; var gradFill = shapeProperties.GetFirstChild(); if (gradFill != null) { fill.type = 2; fill.gradientFill = DoGradientFill(gradFill, wordDoc); } var noFill = shapeProperties.GetFirstChild(); if (noFill != null) { fill.type = 0; } var pattFill = shapeProperties.GetFirstChild(); if (pattFill != null) { HtexPattFill htexPattFill = DoPattFill(pattFill, wordDoc); fill.type = 4; fill.pattFill = htexPattFill; } var solidFill = shapeProperties.GetFirstChild(); if (solidFill != null) { fill.type = 1; fill.solidFill = DoSolidFill(solidFill); } var groupFill = shapeProperties.GetFirstChild(); if (groupFill != null) { fill.type = 5; // fill.solidFill = ReadSolidFillColors(new SolidFill(solidFill.ToString()), slide); } var blipFill = shapeProperties.GetFirstChild(); if (blipFill != null) { fill.type = 3; fill.blipFill = DoBlipFill(wordDoc, blipFill, ImageHandler, element, position); // fill.solidFill = ReadSolidFillColors(new SolidFill(solidFill.ToString()), slide); } var EffectList = shapeProperties.GetFirstChild(); // var effect = doEffect(EffectList,partForm); var EffectDag = shapeProperties.GetFirstChild(); var Scene3DType = shapeProperties.GetFirstChild(); var Shape3DType = shapeProperties.GetFirstChild(); var ShapePropertiesExtensionList = shapeProperties.GetFirstChild(); return new HTEXLib.Models.HTEX.ShapeStyle { fill = fill, border = border/*, effect = effect */}; } public static HTEXLib.Models.HTEX.ShapeStyle DoShapeProperties(WordprocessingDocument wordDoc, DocumentFormat.OpenXml.Office2010.Word.DrawingShape.ShapeProperties shapeProperties, XElement element, Func ImageHandler, Position position) { if (shapeProperties == null) { return null; } //Outline var lnNode = shapeProperties.GetFirstChild(); Border border = null; if (lnNode != null) { border = DoOutline(lnNode,wordDoc); } Fill fill = new Fill() { type = -1 }; var gradFill = shapeProperties.GetFirstChild(); if (gradFill != null) { fill.type = 2; fill.gradientFill = DoGradientFill(gradFill,wordDoc); } var noFill = shapeProperties.GetFirstChild(); if (noFill != null) { fill.type = 0; } var pattFill = shapeProperties.GetFirstChild(); if (pattFill != null) { HtexPattFill htexPattFill = DoPattFill(pattFill,wordDoc); fill.type = 4; fill.pattFill = htexPattFill; } var solidFill = shapeProperties.GetFirstChild(); if (solidFill != null) { fill.type = 1; fill.solidFill = DoSolidFill(solidFill); } var groupFill = shapeProperties.GetFirstChild(); if (groupFill != null) { fill.type = 5; // fill.solidFill = ReadSolidFillColors(new SolidFill(solidFill.ToString()), slide); } var blipFill = shapeProperties.GetFirstChild(); if (blipFill != null) { fill.type = 3; fill.blipFill = DoBlipFill(wordDoc,blipFill, ImageHandler, element, position); // fill.solidFill = ReadSolidFillColors(new SolidFill(solidFill.ToString()), slide); } var EffectList = shapeProperties.GetFirstChild(); // var effect = doEffect(EffectList,partForm); var EffectDag = shapeProperties.GetFirstChild(); var Scene3DType = shapeProperties.GetFirstChild(); var Shape3DType = shapeProperties.GetFirstChild(); var ShapePropertiesExtensionList = shapeProperties.GetFirstChild(); return new HTEXLib.Models.HTEX.ShapeStyle { fill = fill, border = border/*, effect = effect */}; } public static string DoSolidFill(SolidFill Color) { string FontColor = "#000000"; if (Color.RgbColorModelHex != null) { FontColor = ReadColor(Color.RgbColorModelHex); } else if (Color.RgbColorModelPercentage != null) { FontColor = ReadColor(Color.RgbColorModelPercentage); } else if (Color.SystemColor != null) { FontColor = ReadColor(Color.SystemColor); } if (Color.HslColor != null) { FontColor = ReadColor(Color.HslColor); } if (Color.PresetColor != null) { FontColor = ReadColor(Color.PresetColor); } return FontColor; } public static HtexPattFill DoPattFill(DocumentFormat.OpenXml.Drawing.PatternFill patternFill, WordprocessingDocument wordDoc) { HtexPattFill htexPattFill = new HtexPattFill(); Color FgClr = Color.Empty; Color BgClr = Color.Empty; if (patternFill.ForegroundColor != null) { var fgClr = ColorTypeColors(patternFill.ForegroundColor,wordDoc); htexPattFill.fgClr = fgClr; FgClr = ColorTranslator.FromHtml(fgClr); } if (patternFill.BackgroundColor != null) { var bgClr = ColorTypeColors(patternFill.BackgroundColor,wordDoc); htexPattFill.bgClr = bgClr; BgClr = ColorTranslator.FromHtml(bgClr); } htexPattFill.prst = patternFill.Preset.ToString(); MemoryStream ms = new MemoryStream(); System.Drawing.Bitmap bm = PatternFillHelper.GetVmlPatternFill(patternFill.Preset, FgClr, BgClr); Graphics graphics = Graphics.FromImage(bm); graphics.Save(); graphics.Dispose(); bm.Save(ms, System.Drawing.Imaging.ImageFormat.Png); byte[] arr = new byte[ms.Length]; ms.Position = 0; ms.Read(arr, 0, (int)ms.Length); ms.Close(); bm.Dispose(); string sImageData = "data:image/png;base64," + Convert.ToBase64String(arr); htexPattFill.base64 = sImageData; return htexPattFill; } public static HtexBlipFill DoBlipFill(WordprocessingDocument wordDoc, DocumentFormat.OpenXml.Drawing.BlipFill blipFill, Func imageHandler, XElement element, Position position) { HtexBlipFill htexBlipFill = new HtexBlipFill(); SourceRectangle source = blipFill.SourceRectangle; ///源矩形 srcRect可能无用 htexBlipFill.dip = blipFill.Dpi; if (blipFill.RotateWithShape != null) { htexBlipFill.rotWithShape = blipFill.RotateWithShape; } { htexBlipFill.rotWithShape = true; } var fillRect = blipFill.GetPPTXNodeByPath("a:stretch/a:fillRect");///拉伸 if (fillRect != null) { htexBlipFill.fillRect = new Vector { t = fillRect.Attribute("t") != null ? int.Parse(fillRect.Attribute("t").Value) / 1000 : 0, l = fillRect.Attribute("l") != null ? int.Parse(fillRect.Attribute("l").Value) / 1000 : 0, r = fillRect.Attribute("r") != null ? int.Parse(fillRect.Attribute("r").Value) / 1000 : 0, b = fillRect.Attribute("b") != null ? int.Parse(fillRect.Attribute("b").Value) / 1000 : 0 }; } var alphaModFix = blipFill.Blip.GetPPTXNodeByPath("a:alphaModFix"); if (alphaModFix != null) { if (alphaModFix.Attribute("amt") != null) { htexBlipFill.opacity = int.Parse(alphaModFix.Attribute("amt").Value) * 1.0 / 1000; } } Tile tile = blipFill.GetFirstChild(); if (tile != null) { ///algn (Alignment) flip (Tile Flipping) sx (Horizontal Ratio) sy (Vertical Ratio) tx (Horizontal Offset) ty (Vertical Offset) htexBlipFill.tile = new HtexTile { algn = tile.Alignment, flip = tile.Flip, sx = tile.HorizontalRatio, sy = tile.VerticalRatio, tx = tile.HorizontalOffset, ty = tile.VerticalOffset }; } var Embed = blipFill.Blip.Embed; var Link = blipFill.Blip.Link; //slide.SlideLayoutPart IdPartPair part = null; if (blipFill == null) return null; // var imageRid = (string)blipFill.Elements(A.blip).Attributes(R.embed).FirstOrDefault(); //if (imageRid == null) return null; var pp3 = wordDoc.MainDocumentPart.Parts.FirstOrDefault(pp => pp.RelationshipId == Embed); if (pp3 == null) return null; var imagePart = (ImagePart)pp3.OpenXmlPart; if (imagePart == null) return null; // If the image markup points to a NULL image, then following will throw an ArgumentOutOfRangeException try { imagePart = (ImagePart)wordDoc.MainDocumentPart.GetPartById(Embed); } catch (ArgumentOutOfRangeException) { return null; } var contentType = imagePart.ContentType; if (!WmlToHtmlConverter. ImageContentTypes.Contains(contentType)) return null; using (var partStream = imagePart.GetStream()) { using (var bitmap = new Bitmap(partStream)) { var imageInfo = new ImageInfo() { Bitmap = bitmap, ImgStyleAttribute = new XAttribute("style", string.Format(NumberFormatInfo.InvariantInfo, "width: {0}px; height: {1}in", position.cx, position.cy)), ContentType = contentType, // DrawingElement = element, //AltText = altText, }; var imgElement2 = imageHandler(imageInfo); //imgElement2 = new XElement(XhtmlNoNamespace.a, // //new XAttribute(XhtmlNoNamespace.href, hyperlinkUri), // imgElement2); //return imgElement2; //var imageInfo2 = new ImageInfo() //{ // Bitmap = bitmap, // ContentType = contentType, // //DrawingElement = element, // //AltText = altText, //}; //var imgElement = imageHandler(imageInfo2); //imgElement= new XElement(XhtmlNoNamespace.a, // // new XAttribute(XhtmlNoNamespace.href, hyperlinkUri), // imgElement); htexBlipFill.url = imgElement2.ToString(); // return imgElement; } } return htexBlipFill; //if (partForm == "slide") //{ // part = slide.SlidePart.Parts.Where(x => x.RelationshipId == Embed).FirstOrDefault(); //} //if (partForm == "layout") //{ // part = slide.SlideLayoutPart.Parts.Where(x => x.RelationshipId == Embed).FirstOrDefault(); //} //if (partForm == "master") //{ // part = slide.slideMasterPart.Parts.Where(x => x.RelationshipId == Embed).FirstOrDefault(); //} //if (partForm == "chartPart") //{ // part = slide.chartPart.Parts.Where(x => x.RelationshipId == Embed).FirstOrDefault(); //} //if (partForm == "drawing") //{ // part = slide.diagramDataPart.Parts.Where(x => x.RelationshipId == Embed).FirstOrDefault(); //} //if (part != null) //{ // var url = part.OpenXmlPart.Uri.ToString().Replace("../", "/ppt/"); // var name = url.Replace("/", ""); // var thumbnail = slide.Media.Where(x => x.Attributes().Select(y => y.Value == url).FirstOrDefault()).FirstOrDefault(); // if (thumbnail != null) // { // var contentType = thumbnail.Attribute("{http://schemas.microsoft.com/office/2006/xmlPackage}contentType").Value; // //var data = thumbnail.Value; // //url = "data:" + contentType + ";base64," + data; // //url = url.Replace("\r\n", ""); // htexBlipFill.url = name; // // htexBlipFill.urlType = "base64"; // // htexBlipFill.contentType = contentType; // } //} //TODO 图片元素的更多信息需要后期继续实现,如滤镜,裁剪,图片颜色,图片校正等 return htexBlipFill; } /// /// 渐变颜色 /// GradientStopList (渐变停止列表)(默认) /// LinearGradientFill(线性渐变填充) /// PathGradientFill (路径渐变)根据具体形状描边式 /// TileRectangle (平铺矩形) 右下角是一个举行 /// /// /// public static HtexGradientFill DoGradientFill(DocumentFormat.OpenXml.Drawing.GradientFill GradientFill, WordprocessingDocument wordDoc) { HtexGradientFill gradientFill = new HtexGradientFill(); ///右下角弧形模式 string type = ""; ///平铺翻转模式 ///Horizontal 水平 ///HorizontalAndVertical 水平和垂直。 ///Vertical 垂直 ///None 无 string name = "None"; if (GradientFill.Flip != null) { name = GradientFill.Flip.Value.ToString(); } gradientFill.flip = name; List colors = new List(); bool RotateWithShape = true; if (GradientFill.RotateWithShape != null) { RotateWithShape = GradientFill.RotateWithShape; } gradientFill.RotateWithShape = RotateWithShape; foreach (GradientStop gradientStop in GradientFill.GradientStopList) { colors.Add(new ColorPosition { color = ColorTypeColors(gradientStop,wordDoc), position = gradientStop.Position * 1.0 / 1000 /// 位置,色带的位置 百分比0-100 }); } gradientFill.colors = colors; var lin = GradientFill.GetPPTXNodeByPath("//a:lin"); double? rot = null; string scaled = ""; //线性模式 if (lin != null) { rot = 90; type = "line"; var ang = lin.Attribute("ang"); var scaledNode = lin.Attribute("scaled"); if (ang != null) { rot = ShapeHelper.AngleToDegrees(ang.Value); } if (scaledNode != null) { scaled = scaledNode.Value; } gradientFill.line = new LineFill { rot = rot, scaled = scaled }; } var path = GradientFill.GetPPTXNodeByPath("//a:path"); if (path != null) { type = "path"; ///circle ///rect ///shape var pathType = path.Attribute("path").Value; var fillToRect = path.GetPPTXNodeByPath("//a:fillToRect"); if (fillToRect != null) { Vector vector = new Vector(); var bnode = fillToRect.Attribute("b"); var lnode = fillToRect.Attribute("l"); var rnode = fillToRect.Attribute("r"); var tnode = fillToRect.Attribute("t"); if (bnode != null) { vector.b = int.Parse(bnode.Value) / 1000; } if (lnode != null) { vector.l = int.Parse(lnode.Value) / 1000; } if (rnode != null) { vector.r = int.Parse(rnode.Value) / 1000; } if (tnode != null) { vector.t = int.Parse(tnode.Value) / 1000; } gradientFill.path = new PathFill { fillToRect = vector, pathType = pathType }; } } var tileRect = GradientFill.GetPPTXNodeByPath("//a:tileRect"); if (tileRect != null) { ///四个方向的偏移量,矩形的下边缘。例如50000/1000= 50% ,辐射百分比 Vector vector = new Vector(); var bnode = tileRect.Attribute("b"); var lnode = tileRect.Attribute("l"); var rnode = tileRect.Attribute("r"); var tnode = tileRect.Attribute("t"); if (bnode != null) { vector.b = int.Parse(bnode.Value) / 1000; } if (lnode != null) { vector.l = int.Parse(lnode.Value) / 1000; } if (rnode != null) { vector.r = int.Parse(rnode.Value) / 1000; } if (tnode != null) { vector.t = int.Parse(tnode.Value) / 1000; } gradientFill.tileRect = vector; } gradientFill.type = type; return gradientFill; } public static string ColorTypeColors(GradientStop Color, WordprocessingDocument wordDoc) { string FontColor = "#000000"; if (Color.RgbColorModelHex != null) { FontColor = ReadColor(Color.RgbColorModelHex); } else if (Color.RgbColorModelPercentage != null) { FontColor = ReadColor(Color.RgbColorModelPercentage); } else if (Color.SystemColor != null) { FontColor = ReadColor(Color.SystemColor); } if (Color.SchemeColor != null) { FontColor = ReadColor(Color.SchemeColor, wordDoc); } if (Color.HslColor != null) { FontColor = ReadColor(Color.HslColor); } if (Color.PresetColor != null) { FontColor = ReadColor(Color.PresetColor); } return FontColor; } public static string ColorTypeColors(ColorType Color, WordprocessingDocument wordDoc) { string FontColor = "#000000"; if (Color.RgbColorModelHex != null) { FontColor = ReadColor(Color.RgbColorModelHex); } else if (Color.RgbColorModelPercentage != null) { FontColor = ReadColor(Color.RgbColorModelPercentage); } else if (Color.SystemColor != null) { FontColor = ReadColor(Color.SystemColor); } if (Color.SchemeColor != null) { FontColor = ReadColor(Color.SchemeColor, wordDoc); } if (Color.HslColor != null) { FontColor = ReadColor(Color.HslColor); } if (Color.PresetColor != null) { FontColor = ReadColor(Color.PresetColor); } return FontColor; } public static string ReadColor(DocumentFormat.OpenXml.Drawing.RgbColorModelHex RgbColorModelHex) { string FontColor = "#000000"; FontColor = "#" + RgbColorModelHex.Val; FontColor = "#" + ShapeHelper.ColorToning(RgbColorModelHex.OuterXml, FontColor.Replace("#", "")).Replace("#", ""); return FontColor; } public static string ReadColor(DocumentFormat.OpenXml.Drawing.RgbColorModelPercentage RgbColorModelPercentage) { string FontColor = "#000000"; int red = RgbColorModelPercentage.RedPortion.Value; int green = RgbColorModelPercentage.GreenPortion.Value; int blue = RgbColorModelPercentage.BluePortion.Value; var colorFromRGB = System.Drawing.Color.FromArgb(red, green, blue); string fontcolor = System.Drawing.ColorTranslator.ToHtml(colorFromRGB); FontColor = fontcolor; FontColor = "#" + ShapeHelper.ColorToning(RgbColorModelPercentage.OuterXml, FontColor.Replace("#", "")).Replace("#", ""); return FontColor; } public static string ReadColor(DocumentFormat.OpenXml.Drawing.SystemColor SystemColor) { string FontColor = "#000000"; FontColor = "#" + SystemColor.LastColor.Value; FontColor = "#" + ShapeHelper.ColorToning(SystemColor.OuterXml, FontColor.Replace("#", "")).Replace("#", ""); return FontColor; } public static string ReadColor(DocumentFormat.OpenXml.Drawing.SchemeColor SchemeColor, WordprocessingDocument wordDoc) { string FontColor = ""; FontColor = DoSchemeColor(SchemeColor,wordDoc); FontColor = "#" + ShapeHelper.ColorToning(SchemeColor.OuterXml, FontColor.Replace("#", "")).Replace("#", ""); return FontColor; } public static string ReadColor(DocumentFormat.OpenXml.Drawing.HslColor HslColor) { string FontColor = "#000000"; var hue = HslColor.HueValue / 100000; var sat = HslColor.SatValue / 100; var lum = HslColor.LumValue / 100; var hsl2rgb = ShapeHelper.HslToRgb(hue, sat, lum); FontColor = "#" + ShapeHelper.ToHex(hsl2rgb.r) + ShapeHelper.ToHex(hsl2rgb.g) + ShapeHelper.ToHex(hsl2rgb.b); FontColor = "#" + ShapeHelper.ColorToning(HslColor.OuterXml, FontColor.Replace("#", "")).Replace("#", ""); return FontColor; } public static string ReadColor(PresetColor PresetColor) { string FontColor = "#000000"; //https://m.fontke.com/tool/rgb/0000ff/ //判断是否是拿到名字 还是值 FontColor = ShapeHelper.GetColorName2Hex(PresetColor.Val); FontColor = "#" + ShapeHelper.ColorToning(PresetColor.OuterXml, FontColor.Replace("#", "")).Replace("#", ""); //TODO return FontColor; } public static string DoSchemeColor(SchemeColor schemeColor, WordprocessingDocument wordDoc ) { //var light1Color1= slide.SlideLayoutPart.SlideMasterPart. // ThemePart; string FontColor = ""; if (schemeColor.Val == "bg1") { Light1Color light1Color = wordDoc.MainDocumentPart.ThemePart.Theme.ThemeElements.ColorScheme.Light1Color; if (light1Color.RgbColorModelHex != null) { FontColor = "#" + light1Color.RgbColorModelHex.Val.Value; } else if (light1Color.SystemColor != null) { FontColor = "#" + light1Color.SystemColor.LastColor.Value; } } else if (schemeColor.Val == "bg2") { Light2Color light2Color = wordDoc.MainDocumentPart.ThemePart.Theme.ThemeElements.ColorScheme.Light2Color; if (light2Color.RgbColorModelHex != null) { FontColor = "#" + light2Color.RgbColorModelHex.Val.Value; } else if (light2Color.SystemColor != null) { FontColor = "#" + light2Color.SystemColor.LastColor.Value; } } else if (schemeColor.Val == "tx1") { Dark1Color dark1Color = wordDoc.MainDocumentPart.ThemePart.Theme.ThemeElements.ColorScheme.Dark1Color; if (dark1Color.RgbColorModelHex != null) { FontColor = "#" + dark1Color.RgbColorModelHex.Val.Value; } else if (dark1Color.SystemColor != null) { FontColor = "#" + dark1Color.SystemColor.LastColor.Value; } } else if (schemeColor.Val == "tx2") { Dark2Color dark2Color = wordDoc.MainDocumentPart.ThemePart.Theme.ThemeElements.ColorScheme.Dark2Color; if (dark2Color.RgbColorModelHex != null) { FontColor = "#" + dark2Color.RgbColorModelHex.Val.Value; } else if (dark2Color.SystemColor != null) { FontColor = "#" + dark2Color.SystemColor.LastColor.Value; } } else if (schemeColor.Val == "hlink") { Hyperlink HyperLink = wordDoc.MainDocumentPart.ThemePart.Theme.ThemeElements.ColorScheme.Hyperlink; if (HyperLink.RgbColorModelHex != null) { FontColor = "#" + HyperLink.RgbColorModelHex.Val.Value; } } else if (schemeColor.Val == "folHlink") { FollowedHyperlinkColor folHyperLink = wordDoc.MainDocumentPart.ThemePart.Theme.ThemeElements.ColorScheme.FollowedHyperlinkColor; if (folHyperLink.RgbColorModelHex != null) { FontColor = "#" + folHyperLink.RgbColorModelHex.Val.Value; } } if (string.IsNullOrEmpty(FontColor)) { FontColor = ReadAccentSchemeColors(schemeColor, wordDoc); } if (FontColor.Replace("#", "") == "") { var chren = wordDoc.MainDocumentPart.ThemePart.Theme.ThemeElements.ColorScheme.ChildElements; foreach (var cl in chren) { if (schemeColor.Val == cl.LocalName) { if (schemeColor.Val == "dk1" || schemeColor.Val == "lt1") { if (cl.GetFirstChild() != null) { FontColor = "#" + cl.GetFirstChild().LastColor; } else { FontColor = "#" + cl.GetFirstChild().Val.Value; ; } } else { FontColor = "#" + cl.GetFirstChild().Val.Value; ; } } } } return FontColor; } public static string ReadAccentSchemeColors(SchemeColor schemeColor, WordprocessingDocument wordDoc) { string FontColor = ""; for (int i = 1; i <= 6; i++) { if (schemeColor.Val == "accent" + i) { switch (i) { case 1: { Accent1Color c = wordDoc.MainDocumentPart.ThemePart.Theme.ThemeElements.ColorScheme.Accent1Color; if (c.RgbColorModelHex != null) FontColor = "#" + c.RgbColorModelHex.Val.Value; } break; case 2: { Accent2Color c = wordDoc.MainDocumentPart.ThemePart.Theme.ThemeElements.ColorScheme.Accent2Color; if (c.RgbColorModelHex != null) FontColor = "#" + c.RgbColorModelHex.Val.Value; } break; case 3: { Accent3Color c = wordDoc.MainDocumentPart.ThemePart.Theme.ThemeElements.ColorScheme.Accent3Color; if (c.RgbColorModelHex != null) FontColor = "#" + c.RgbColorModelHex.Val.Value; } break; case 4: { Accent4Color c = wordDoc.MainDocumentPart.ThemePart.Theme.ThemeElements.ColorScheme.Accent4Color; if (c.RgbColorModelHex != null) FontColor = "#" + c.RgbColorModelHex.Val.Value; } break; case 5: { Accent5Color c = wordDoc.MainDocumentPart.ThemePart.Theme.ThemeElements.ColorScheme.Accent5Color; if (c.RgbColorModelHex != null) FontColor = "#" + c.RgbColorModelHex.Val.Value; } break; case 6: { Accent6Color c = wordDoc.MainDocumentPart.ThemePart.Theme.ThemeElements.ColorScheme.Accent6Color; if (c.RgbColorModelHex != null) FontColor = "#" + c.RgbColorModelHex.Val.Value; } break; default: { } break; } } } return FontColor; } /// /// a:ln 图形的外边 /// /// /// public static Border DoOutline(DocumentFormat.OpenXml.Drawing.LinePropertiesType outline, WordprocessingDocument wordDoc) { Border border = new Border() { color = new Fill { type = -1 }, type = "none" }; //20.1.10.35 EMUs. 1 pt = 12700 EMUs. double? Width = null; if (outline == null ) { return new Border { outline = new HtexOutline { width = 0.75 }, type = "solid", stroke = "0" }; } if (outline.Width != null) { Width = double.Parse(outline.Width) * 1.0 * Globals.px96 / Globals.px914400; } else { Width = 0.75; } // Center = 0,Insert = 1 // //20.1.10.39 string algn = null; if (outline.Alignment != null) { algn = outline.Alignment; } /* 20.1.10.15 复合类型 Single = 0, 单线 默认 Double = 1, 双细线 ThickThin = 2, 粗到细 ThinThick = 3, 细到粗 Triple = 4 三线 */ string cmpd = null; if (outline.CompoundLineType != null) { cmpd = outline.CompoundLineType; } /* 20.1.10.31 线端类型 Round = 0, 方 Square = 1,圆 Flat = 2 平 */ string cap = null; if (outline.CapType != null) { cap = outline.CapType; } border.outline = new HtexOutline { width = Width, algn = algn, cmpd = cmpd, cap = cap }; var gradFill = outline.GetFirstChild(); if (gradFill != null) { border.color.type = 2; border.color.gradientFill = DoGradientFill(gradFill,wordDoc); } var noFill = outline.GetFirstChild(); if (noFill != null) { border.color.type = 0; } var pattFill = outline.GetFirstChild(); if (pattFill != null) { HtexPattFill htexPattFill = DoPattFill(pattFill,wordDoc); border.color.type = 4; border.color.pattFill = htexPattFill; } var solidFill = outline.GetFirstChild(); if (solidFill != null) { border.color.type = 1; border.color.solidFill = DoSolidFill(solidFill); } if (Width != null && Width > 0) { border.type = "solid"; border.stroke = "0"; } var prstDash = outline.GetFirstChild(); //预设连接符 if (prstDash != null) { // PresetDash var (type, stroke) = DoPrstDash(prstDash); border.type = type; border.stroke = stroke; } if (outline.GetFirstChild() != null) { // 棱台切角 border.corner = "bevel"; } if (outline.GetFirstChild() != null) { //圆角 border.corner = "round"; } if (outline.GetFirstChild() != null) { //斜角 border.corner = "miter"; } var headEndNodeAttrs = outline.GetFirstChild(); //连接符开始符号 if (headEndNodeAttrs != null) { border.headEnd = headEndNodeAttrs.Type; } var tailEndNodeAttrs = outline.GetFirstChild();//末端连接符 if (tailEndNodeAttrs != null) { border.tailEnd = tailEndNodeAttrs.Type; } //TODO //outline.GetTextByPath("//a:extLst"); var custDash = outline.GetFirstChild();//自定义连接线 if (custDash != null) { var (type, stroke) = DoCustDash(custDash); border.type = type; border.stroke = stroke; } if (border.color != null && (border.color.type != null || border.color.type != 1) && border.outline.width == null) { border.outline.width = 1; if (border.type == "none" || border.type == null) { border.type = "solid"; border.stroke = "0"; } } return border; } /// ///自定义连线符 /// /// /// public static (string type, string stroke) DoCustDash(CustomDash custDash) { string type = "cust"; string stroke = "0"; IEnumerable elements = custDash.GetPPTXNodeListByPath("ds"); if (elements != null) { foreach (XElement element in elements) { var d = element.Attribute("d");//线段长度 if (d != null) { stroke = stroke + d.Value + ","; } var sp = element.Attribute("sp");//空白间距长度 if (sp != null) { stroke = stroke + sp.Value + ","; } } } if (string.IsNullOrEmpty(stroke)) { stroke = "0"; } if (stroke.EndsWith(",")) { stroke.Substring(0, stroke.Length - 1); } return (type, stroke); } /// /// 预设连线符 /// /// /// public static (string type, string stroke) DoPrstDash(PresetDash presetDash) { Border border = new Border() { type = "solid", stroke = "0" }; //PresetLineDashValues 枚举 string val = presetDash.Val; switch (val) { case "solid": border.type = "solid"; border.stroke = "0"; break; case "dash": border.type += "dashed"; border.stroke = "5"; break; case "dashDot": border.type = "dashed"; border.stroke = "5, 5, 1, 5"; break; case "dot": border.type = "dotted"; border.stroke = "1, 5"; break; case "lgDash": border.type = "dashed"; border.stroke = "10, 5"; break; case "lgDashDotDot": border.type = "dashed"; border.stroke = "10, 5, 1, 5, 1, 5"; break; case "sysDash": border.type = "dashed"; border.stroke = "5, 2"; break; case "sysDashDot": border.type = "dashed"; border.stroke = "5, 2, 1, 5"; break; case "sysDashDotDot": border.type = "dashed"; border.stroke = "5, 2, 1, 5, 1, 5"; break; case "sysDot": border.type = "dotted"; border.stroke = "2, 5"; break; case null: //console.log(borderType); default: border.type = "solid"; border.stroke = "0"; break; } return (border.type, border.stroke); } } }