/// <summary> /// Change the fill color of a shape, docName must have a filled shape as the first shape on the first slide. /// </summary> /// <param name="docName">path to the file</param> public static void SetPPTShapeColor(string docName) { using (PresentationDocument ppt = PresentationDocument.Open(docName, true)) { // Get the relationship ID of the first slide. PresentationPart part = ppt.PresentationPart; OpenXmlElementList slideIds = part.Presentation.SlideIdList.ChildElements; string relId = (slideIds[0] as SlideId).RelationshipId; // Get the slide part from the relationship ID. SlidePart slide = (SlidePart)part.GetPartById(relId); if (slide != null) { // Get the shape tree that contains the shape to change. ShapeTree tree = slide.Slide.CommonSlideData.ShapeTree; // Get the first shape in the shape tree. PShape shape = tree.GetFirstChild <PShape>(); if (shape != null) { // Get the style of the shape. ShapeStyle style = shape.ShapeStyle; // Get the fill reference. FillReference fillRef = style.FillReference; // Set the fill color to SchemeColor Accent 6; fillRef.SchemeColor = new SchemeColor { Val = SchemeColorValues.Accent6 }; // Save the modified slide. slide.Slide.Save(); } } } }
private void insertLink(Slide slide, PageUrl pageUrl, int objId) { slide.SlidePart.AddHyperlinkRelationship(new System.Uri(pageUrl.url, System.UriKind.Absolute), true, "rId" + objId); P.Shape shape = new P.Shape(); P.NonVisualShapeProperties nonVisualShapeProperties1 = new P.NonVisualShapeProperties() { NonVisualDrawingProperties = new P.NonVisualDrawingProperties() { Id = (UInt32Value)2U, Name = "矩形 1", HyperlinkOnClick = new A.HyperlinkOnClick() { Id = "rId" + objId } }, NonVisualShapeDrawingProperties = new P.NonVisualShapeDrawingProperties(), ApplicationNonVisualDrawingProperties = new P.ApplicationNonVisualDrawingProperties() }; P.ShapeProperties shapeProperties = new P.ShapeProperties() { Transform2D = new A.Transform2D() { Offset = new A.Offset() { X = pageUrl.origin.Width * ImageInfoUtils.RATE, Y = pageUrl.origin.Height * ImageInfoUtils.RATE }, Extents = new A.Extents() { Cx = pageUrl.size.Width * ImageInfoUtils.RATE, Cy = pageUrl.size.Height * ImageInfoUtils.RATE } } }; A.PresetGeometry presetGeometry = new A.PresetGeometry() { Preset = A.ShapeTypeValues.Rectangle, AdjustValueList = new A.AdjustValueList() }; A.NoFill noFill = new A.NoFill(); A.Outline outline = new A.Outline(); A.NoFill noFill2 = new A.NoFill(); outline.Append(noFill2); shapeProperties.Append(presetGeometry); shapeProperties.Append(noFill); shapeProperties.Append(outline); P.ShapeStyle shapeStyle1 = new P.ShapeStyle(); A.LineReference lineReference = new A.LineReference() { Index = (UInt32Value)2U }; A.SchemeColor schemeColor = new A.SchemeColor() { Val = A.SchemeColorValues.Accent1 }; A.Shade shade1 = new A.Shade() { Val = 50000 }; schemeColor.Append(shade1); lineReference.Append(schemeColor); A.FillReference fillReference = new A.FillReference() { Index = (UInt32Value)1U }; A.SchemeColor schemeColor2 = new A.SchemeColor() { Val = A.SchemeColorValues.Accent1 }; fillReference.Append(schemeColor2); A.EffectReference effectReference = new A.EffectReference() { Index = (UInt32Value)0U }; A.SchemeColor schemeColor3 = new A.SchemeColor() { Val = A.SchemeColorValues.Accent1 }; effectReference.Append(schemeColor3); A.FontReference fontReference = new A.FontReference() { Index = A.FontCollectionIndexValues.Minor }; A.SchemeColor schemeColor4 = new A.SchemeColor() { Val = A.SchemeColorValues.Light1 }; fontReference.Append(schemeColor4); shapeStyle1.Append(lineReference); shapeStyle1.Append(fillReference); shapeStyle1.Append(effectReference); shapeStyle1.Append(fontReference); P.TextBody textBody = new P.TextBody(); A.BodyProperties bodyProperties = new A.BodyProperties() { RightToLeftColumns = false, Anchor = A.TextAnchoringTypeValues.Center }; A.ListStyle listStyle = new A.ListStyle(); A.Paragraph paragraph = new A.Paragraph(); A.ParagraphProperties paragraphProperties = new A.ParagraphProperties() { Alignment = A.TextAlignmentTypeValues.Center }; A.EndParagraphRunProperties endParagraphRunProperties = new A.EndParagraphRunProperties() { Language = "zh-CN", AlternativeLanguage = "en-US" }; paragraph.Append(paragraphProperties); paragraph.Append(endParagraphRunProperties); textBody.Append(bodyProperties); textBody.Append(listStyle); textBody.Append(paragraph); shape.Append(nonVisualShapeProperties1); shape.Append(shapeProperties); shape.Append(shapeStyle1); shape.Append(textBody); slide.CommonSlideData.ShapeTree.AppendChild(shape); }