Rectangle

Add Rectangle to Slide

To add a simple rectangle to a selected slide of the presentation, please follow the steps below:

In the example given below, we have added a simple rectangle to the first slide of the presentation.

// Instantiate Prseetation class that represents the PPTX
var pres = new aspose.slides.Presentation();
try {
    // Get the first slide
    var sld = pres.getSlides().get_Item(0);
    // Add AutoShape of ellipse type
    var shp = sld.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 50, 150, 150, 50);
    // Write the PPTX file to disk
    pres.save("RecShp1.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    if (pres != null) {
        pres.dispose();
    }
}

Add Formatted Rectangle to Slide

To add a formatted rectangle to a slide, please follow the steps below:

  • Create an instance of Presentation class.
  • Obtain the reference of a slide by using its Index.
  • Add an AutoShape of Rectangle type using addAutoShape method exposed by ShapeCollection object.
  • Set the Fill Type of the Rectangle to Solid.
  • Set the Color of the Rectangle using SolidFillColor.setColor method as exposed by FillFormat object associated with the Shape object.
  • Set the Color of the lines of the Rectangle.
  • Set the Width of the lines of the Rectangle.
  • Write the modified presentation as PPTX file.

The above steps are implemented in the example given below.

// Instantiate Prseetation class that represents the PPTX
var pres = new aspose.slides.Presentation();
try {
    // Get the first slide
    var sld = pres.getSlides().get_Item(0);
    // Add AutoShape of ellipse type
    var shp = sld.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 50, 150, 150, 50);
    // Apply some formatting to ellipse shape
    shp.getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
    shp.getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "GRAY"));
    // Apply some formatting to the line of Ellipse
    shp.getLineFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
    shp.getLineFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
    shp.getLineFormat().setWidth(5);
    // Write the PPTX file to disk
    pres.save("RecShp2.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    if (pres != null) {
        pres.dispose();
    }
}