Modifier la couleur de remplissage d'une forme dans une présentation
Contents
[
Hide
]
Présentation OpenXML
string FilePath = @"..\..\..\..\Sample Files\";
string FileName = FilePath + "Fill color of a shape.pptx";
SetPPTShapeColor(FileName);
// Change the fill color of a shape.
// The test file must have a filled shape as the first shape on the first slide.
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.
Shape shape = tree.GetFirstChild<Shape>();
if (shape != null)
{
// Get the style of the shape.
ShapeStyle style = shape.ShapeStyle;
// Get the fill reference.
Drawing.FillReference fillRef = style.FillReference;
// Set the fill color to SchemeColor Accent 6;
fillRef.SchemeColor = new Drawing.SchemeColor();
fillRef.SchemeColor.Val = Drawing.SchemeColorValues.Accent6;
// Save the modified slide.
slide.Slide.Save();
}
}
}
}
Aspose.Slides
Nous devons suivre les étapes suivantes pour remplir les formes dans la présentation :
- Créer une instance de la classe Presentation.
- Obtenir la référence d’une diapositive en utilisant son indice.
- Ajouter un IShape à la diapositive.
- Définir le type de remplissage de la forme sur Solid.
- Définir la couleur de la forme.
- Enregistrer la présentation modifiée sous forme de fichier PPTX.
string FilePath = @"..\..\..\..\Sample Files\";
string FileName = FilePath + "Fill color of a shape.pptx";
//Instantiate PrseetationEx class that represents the PPTX
using (Presentation pres = new Presentation())
{
//Get the first slide
ISlide sld = pres.Slides[0];
//Add autoshape of rectangle type
IShape shp = sld.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 150, 75, 150);
//Set the fill type to Solid
shp.FillFormat.FillType = FillType.Solid;
//Set the color of the rectangle
shp.FillFormat.SolidFillColor.Color = Color.Yellow;
//Write the PPTX file to disk
pres.Save(FileName, SaveFormat.Pptx);
}