Re-sizing Shapes on Slide
Re-sizing Shapes on Slide
One of the most frequent questions asked by the Aspose.Slides for Java customers is how to re-size shapes so that when Slide size is changed the data does not cut off. This short technical tip shows how to achieve that.
To avoid shapes disorientation, each shape on the slide needs to be updated according to new slide size.
Presentation presentation = new Presentation("D:\\TestResize.ppt"); | |
//Old slide size | |
float currentHeight = (float)presentation.getSlideSize().getSize().getHeight(); | |
float currentWidth =(float)presentation.getSlideSize().getSize().getWidth(); | |
//Changing slide size | |
presentation.getSlideSize().setType(SlideSizeType.A4Paper); | |
//New slide size | |
float newHeight = (float)presentation.getSlideSize().getSize().getHeight(); | |
float newWidth = (float)presentation.getSlideSize().getSize().getWidth(); | |
float ratioHeight = newHeight / currentHeight; | |
float ratioWidth = newWidth / currentWidth; | |
for (ISlide slide : presentation.getSlides()) | |
{ | |
for (IShape shape : slide.getShapes()) | |
{ | |
//Resize position | |
shape.setHeight(shape.getHeight() * ratioHeight); | |
shape.setWidth(shape.getWidth() * ratioWidth); | |
//Resize shape size if required | |
shape.setY(shape.getY() * ratioHeight); | |
shape.setX(shape.getX() * ratioWidth); | |
} | |
} | |
presentation.save("Test.pptx",SaveFormat.Pptx); |
You need to use following code on your end if you need to re-size the slides with tables. Setting table width or height is a special case in shapes where you need to alter the individual row height and column width to alter the table height and width.
Presentation presentation = new Presentation("D:\\Test.pptx"); | |
//Old slide size | |
float currentHeight = (float) presentation.getSlideSize().getSize().getHeight(); | |
float currentWidth = (float) presentation.getSlideSize().getSize().getWidth(); | |
//Changing slide size | |
presentation.getSlideSize().setType(SlideSizeType.A4Paper); | |
//presentation.SlideSize.Orientation = SlideOrienation.Portrait; | |
//New slide size | |
float newHeight = (float) presentation.getSlideSize().getSize().getHeight(); | |
float newWidth = (float) presentation.getSlideSize().getSize().getWidth(); | |
float ratioHeight = newHeight / currentHeight; | |
float ratioWidth = newWidth / currentWidth; | |
for (IMasterSlide master : presentation.getMasters()) | |
{ | |
for (IShape shape : master.getShapes()) | |
{ | |
//Resize position | |
shape.setHeight(shape.getHeight() * ratioHeight); | |
shape.setWidth(shape.getWidth() * ratioWidth); | |
//Resize shape size if required | |
shape.setY(shape.getY() * ratioHeight); | |
shape.setX(shape.getX() * ratioWidth); | |
} | |
for (ILayoutSlide layoutslide : master.getLayoutSlides()) | |
{ | |
for (IShape shape : layoutslide.getShapes()) | |
{ | |
//Resize position | |
shape.setHeight(shape.getHeight() * ratioHeight); | |
shape.setWidth(shape.getWidth() * ratioWidth); | |
//Resize shape size if required | |
shape.setY(shape.getY() * ratioHeight); | |
shape.setX(shape.getX() * ratioWidth); | |
} | |
} | |
} | |
for (ISlide slide : presentation.getSlides()) | |
{ | |
for (IShape shape : slide.getShapes()) | |
{ | |
//Resize position | |
shape.setHeight(shape.getHeight() * ratioHeight); | |
shape.setWidth(shape.getWidth() * ratioWidth); | |
//Resize shape size if required | |
shape.setY(shape.getY() * ratioHeight); | |
shape.setX(shape.getX() * ratioWidth); | |
if (shape instanceof ITable) | |
{ | |
ITable table = (ITable)shape; | |
IRow row=null; | |
for(int i=0;i<table.getRows().size();i++) | |
{ | |
row=table.getRows().get_Item(i); | |
row.setMinimalHeight(row.getMinimalHeight() * ratioHeight); | |
} | |
IColumn col; | |
for (int j=0;j<table.getColumns().size();j++) | |
{ | |
col=table.getColumns().get_Item(j); | |
col.setWidth(col.getWidth() * ratioWidth); | |
} | |
} | |
} | |
} | |
presentation.save("D:\\Test.pptx", SaveFormat.Pptx); |