在使用
Aspose.Slides的过程中,开发人员经常问到的问题就是怎么调整PPT文件里的图形大小时,文本数据不会被剪切掉,这篇文章就这个问题做详细的介绍,为了避免图形变形,每个图形需要根据新的图形大小进行更新。具体可以参考下面的代码:
//Load a presentation
Presentation presentation = new Presentation(@"D:\TestResize.ppt");
//Old slide size
float currentHeight = presentation.SlideSize.Size.Height;
float currentWidth = presentation.SlideSize.Size.Width;
//Changing slide size
presentation.SlideSize.Type = SlideSizeType.A4Paper;
//New slide size
float newHeight = presentation.SlideSize.Size.Height;
float newWidth = presentation.SlideSize.Size.Width;
float ratioHeight = newHeight / currentHeight;
float ratioWidth = newWidth / currentWidth;
foreach (ISlide slide in presentation.Slides)
{
foreach (IShape shape in slide.Shapes)
{
//Resize position
shape.Height = shape.Height * ratioHeight;
shape.Width = shape.Width * ratioWidth;
//Resize shape size if required
shape.Y = shape.Y * ratioHeight;
shape.X = shape.X * ratioWidth;
}
}
presentation.Save("Resize.pptx",SaveFormat.Pptx);
当PPT文件中有任何的表格时,则需要注意上面的代码就不能更好的运行了,因为每个单元格都需要进行重新调整,如果要调整代表格的PPT文件,则使用下面的代码:
Presentation presentation = new Presentation("D:\\Test.pptx");
//Old slide size
float currentHeight = presentation.SlideSize.Size.Height;
float currentWidth = presentation.SlideSize.Size.Width;
//Changing slide size
presentation.SlideSize.Type = SlideSizeType.A4Paper;
//presentation.SlideSize.Orientation = SlideOrienation.Portrait;
//New slide size
float newHeight = presentation.SlideSize.Size.Height;
float newWidth = presentation.SlideSize.Size.Width;
float ratioHeight = newHeight / currentHeight;
float ratioWidth = newWidth / currentWidth;
foreach (IMasterSlide master in presentation.Masters)
{
foreach (IShape shape in master.Shapes)
{
//Resize position
shape.Height = shape.Height * ratioHeight;
shape.Width = shape.Width * ratioWidth;
//Resize shape size if required
shape.Y = shape.Y * ratioHeight;
shape.X = shape.X * ratioWidth;
}
foreach (ILayoutSlide layoutslide in master.LayoutSlides)
{
foreach (IShape shape in layoutslide.Shapes)
{
//Resize position
shape.Height = shape.Height * ratioHeight;
shape.Width = shape.Width * ratioWidth;
//Resize shape size if required
shape.Y = shape.Y * ratioHeight;
shape.X = shape.X * ratioWidth;
}
}
}
foreach (ISlide slide in presentation.Slides)
{
foreach (IShape shape in slide.Shapes)
{
//Resize position
shape.Height = shape.Height * ratioHeight;
shape.Width = shape.Width * ratioWidth;
//Resize shape size if required
shape.Y = shape.Y * ratioHeight;
shape.X = shape.X * ratioWidth;
if (shape is ITable)
{
ITable table = (ITable)shape;
foreach (IRow row in table.Rows)
{
row.MinimalHeight = row.MinimalHeight * ratioHeight;
// row.Height = row.Height * ratioHeight;
}
foreach (IColumn col in table.Columns)
{
col.Width = col.Width * ratioWidth;
}
}
}
}
presentation.Save("D:\\Resize.pptx", SaveFormat.Pptx);