Изменение размеров фигур на слайде
Изменение размеров фигур на слайде
Один из самых частых вопросов, задаваемых клиентами Aspose.Slides для Java, заключается в том, как изменить размеры фигур так, чтобы при изменении размера слайда данные не обрезались. Эта короткая техническая подсказка показывает, как это сделать.
Чтобы избежать дезориентации фигур, каждая фигура на слайде должна быть обновлена в соответствии с новым размером слайда.
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); |
Вам нужно использовать следующий код, если вам необходимо изменить размеры слайдов с таблицами. Установка ширины или высоты таблицы является особым случаем в фигурах, где необходимо изменить высоту отдельных строк и ширину столбцов, чтобы изменить высоту и ширину таблицы.
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); |