Reset the size and position for the shape on presentation slides in C#

2017-01-04 07:40:20 Written by  support iceblue
Rate this item
(0 votes)

Every time when we change the slide's size, we need to reset the size and position for the shape to ensure the shape on the slide shows orderly. This tutorial will focus on demonstrating how to reset the size and position for the shape in C#.

Firstly, please view the original shape:

Reset the size and position for the shape on presentation slides in C#

Code snippet of how to set the size and position of the shape:

Step 1: Create a new presentation document and load the document from file.

Presentation presentation = new Presentation();
presentation.LoadFromFile("Sample.pptx");

Step 2: Define the original slide size.

float currentHeight = presentation.SlideSize.Size.Height;
float currentWidth = presentation.SlideSize.Size.Width;

Step 3: Change the slide size as A3.

presentation.SlideSize.Type = SlideSizeType.A3;

Step 4: Define the new slide size.

float newHeight = presentation.SlideSize.Size.Height;
float newWidth = presentation.SlideSize.Size.Width;

Step 5: Define the ratio from the old and new slide size.

float ratioHeight = newHeight / currentHeight;
float ratioWidth = newWidth / currentWidth;

Step 6: Reset the size and position of the shape on the slide.

foreach (ISlide slide in presentation.Slides)
{
    foreach (IShape shape in slide.Shapes)
    {
        //Reset the shape size
        shape.Height = shape.Height * ratioHeight;
        shape.Width = shape.Width * ratioWidth;

        //Reset the shape position
        shape.Left = shape.Left * ratioHeight;
        shape.Top = shape.Top * ratioWidth;

    }

Step 7: Save the document to file.

presentation.SaveToFile("resize.pptx", FileFormat.Pptx2010);

Effective screenshot after reset the size and position of the shape on the slide:

Reset the size and position for the shape on presentation slides in C#

Full codes:

using Spire.Presentation;

namespace ResetSizeandPosition
{

    class Program
    {

        static void Main(string[] args)
        {
            {
                Presentation presentation = new Presentation();
                presentation.LoadFromFile("Sample.pptx");

                float currentHeight = presentation.SlideSize.Size.Height;
                float currentWidth = presentation.SlideSize.Size.Width;

                presentation.SlideSize.Type = SlideSizeType.A3;

                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)
                    {
                        shape.Height = shape.Height * ratioHeight;
                        shape.Width = shape.Width * ratioWidth;

                        shape.Left = shape.Left * ratioHeight;
                        shape.Top = shape.Top * ratioWidth;
                    }

                    presentation.SaveToFile("resize.pptx", FileFormat.Pptx2010);
                }
            }

        }
    }
}

Additional Info

  • tutorial_title: Reset the size and position for the shape on presentation slides
Last modified on Friday, 24 September 2021 09:19