Añadir rectángulos a presentaciones en PHP
Add a Rectangle to a Slide
Para añadir un rectángulo sencillo a una diapositiva seleccionada de la presentación, siga los pasos a continuación:
- Cree una instancia de la clase Presentation.
- Obtenga la referencia de una diapositiva mediante su Index.
- Añada un AutoShape de tipo Rectangle usando el método addAutoShape expuesto por el objeto ShapeCollection.
- Guarde la presentación modificada como archivo PPTX.
En el ejemplo que se muestra a continuación, hemos añadido un rectángulo sencillo a la primera diapositiva de la presentación.
# Instanciar la clase Presentation que representa el PPTX
# Obtener la primera diapositiva
# Añadir AutoShape de tipo elipse
# Escribir el archivo PPTX en disco
$pres = new Presentation();
try {
# Obtener la primera diapositiva
$sld = $pres->getSlides()->get_Item(0);
# Añadir AutoShape de tipo elipse
$shp = $sld->getShapes()->addAutoShape(ShapeType::Rectangle, 50, 150, 150, 50);
# Escribir el archivo PPTX en disco
$pres->save("RecShp1.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Add a Formatted Rectangle to a Slide
Para añadir un rectángulo con formato a una diapositiva, siga los pasos a continuación:
- Cree una instancia de la clase Presentation.
- Obtenga la referencia de una diapositiva mediante su Index.
- Añada un AutoShape de tipo Rectangle usando el método addAutoShape expuesto por el objeto ShapeCollection.
- Establezca el Fill Type del rectángulo a Solid.
- Defina el color del rectángulo mediante el método ColorFormat::setColor expuesto por el objeto FillFormat asociado al objeto Shape.
- Establezca el color de las líneas del rectángulo.
- Establezca el ancho de las líneas del rectángulo.
- Guarde la presentación modificada como archivo PPTX.
Los pasos anteriores se implementan en el ejemplo que se muestra a continuación.
# Instanciar la clase Presentation que representa el PPTX
$pres = new Presentation();
try {
# Obtener la primera diapositiva
$sld = $pres->getSlides()->get_Item(0);
# Añadir AutoShape de tipo elipse
$shp = $sld->getShapes()->addAutoShape(ShapeType::Rectangle, 50, 150, 150, 50);
# Aplicar algo de formato a la forma elipse
$shp->getFillFormat()->setFillType(FillType::Solid);
$shp->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->GRAY);
# Aplicar algo de formato a la línea de la elipse
$shp->getLineFormat()->getFillFormat()->setFillType(FillType::Solid);
$shp->getLineFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLACK);
$shp->getLineFormat()->setWidth(5);
# Guardar el archivo PPTX en disco
$pres->save("RecShp2.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
FAQ
How do I add a rectangle with rounded corners?
Use the rounded-corner shape type and adjust the corner radius in the shape’s properties; rounding can also be applied per corner via geometry adjustments.
How do I fill a rectangle with an image (texture)?
Select the picture fill type, provide the image source, and configure stretching/tiling modes.
Can a rectangle have shadow and glow?
Yes. Outer/inner shadow, glow, and soft edges are available with adjustable parameters.
Can I turn a rectangle into a button with a hyperlink?
Yes. Assign a hyperlink to the shape click (jump to a slide, file, web address, or e-mail).
How can I protect a rectangle from moving and changes?
Use shape locks: you can forbid moving, resizing, selection, or text editing to preserve the layout.
Can I convert a rectangle to a raster image or SVG?
Yes. You can render the shape to an image with a specified size/scale or export it as SVG for vector use.
How do I quickly get the actual (effective) properties of a rectangle considering theme and inheritance?
Use the shape’s effective properties: the API returns computed values that account for theme styles, layout, and local settings, simplifying formatting analysis.