Working Solution for Chart Resizing in PPTX

Background

In previous article , we have explained how to create an Excel Chart using Aspose.Cells for .NET and further embed this chart in a PowerPoint Presentation using Aspose.Slides for .NET. In order to accommodate the object changed issue , we assigned the chart image to the Chart OLE Object Frame. In the output presentation, when we double click the OLE Object Frame showing the Chart Image, the Excel Chart is activated. The end users can make any desired changes in the actual Excel Workbook and then return to the concerned Slide by clicking outside the activated Excel Workbook. The size of the OLE Object Frame will change when the user gets back to the slide. The resizing factor will be different for different sizes of OLE Object Frame and embedded Excel Workbook.

Cause of Resizing

Since the Excel Workbook has its own window size, it tries to retain its original size on first time activation. On the other hand, the OLE Object Frame will have its own size. According to Microsoft, on activation of the Excel Workbook, Excel and PowerPoint negotiate the size and ensure it is in the correct proportions as part of the embedding operation. Based on the differences in the Excel Windows size and OLE Object Frame size / position, the resizing takes place.

Working Solution

There are two possible scenarios for creation of the PowerPoint Presentations using Aspose.Slides for .NET.

Scenario 1: Create the presentation based on an existing template

Scenario 2: Create the presentation from scratch.

The solution that we will provide here will be valid for both scenarios. The base of all the solution approaches will be same. That is: Embedded OLE Object Window size should be the same as that of the OLE Object Frame in the PowerPoint Slide . Now, we will discuss the two approaches of the solution.

First Approach

In this approach, we will learn how to set the window size of the embedded Excel Workbook equivalent to the size of the OLE Object Frame in the PowerPoint Slide.

Scenario 1

Suppose, we have defined a template and desire to create the presentations based on this template. Let us say there is some shape at index 2 in the template where we want to place an OLE Frame carrying an embedded Excel Workbook. In this scenario, the size of the OLE Object Frame will be considered as predefined (which is the size of the shape at index 2 in the template). All we have to do: set the window size of the Workbook equal to the size of the Shape. The following code snippet will serve this purpose:

//define chart size with window 
chart.SizeWithWindow = true;

//set window width of the workbook in inches (divided by 72 as PowerPoint uses 
//72 pixels / inch)
wb.Worksheets.WindowWidthInch = slide.Shapes[2].Width / 72f;

//set window height of the workbook in inches
wb.Worksheets.WindowHeightInch = slide.Shapes[2].Height / 72f;

//Instantiate MemoryStream
MemoryStream ms = wb.SaveToStream();

//Create an OLE Object Frame with embedded Excel
Aspose.Slides.OleObjectFrame objFrame = slide.Shapes.AddOleObjectFrame(
				slide.Shapes[2].X,
				slide.Shapes[2].Y,
				slide.Shapes[2].Width,
				slide.Shapes[2].Height, "Excel.Sheet.8", ms.ToArray());

Scenario 2

Let us say, we want to create a presentation from scratch and desire an OLE Object Frame of any size with an embedded Excel Workbook. In the following code snippet, we have created an OLE Object Frame with 4 inch height and 9.5 inch width in the slide at x-axis=0.5 inch and y-axis=1 inch. Further, we have set the equivalent Excel Workbook window size, that is: height 4 inch and width 9.5 inch.

 //Our desired height
int desiredHeight = 288;//4 inch (4 * 72)

//Our desired width
int desiredWidth = 684;//9.5 inch (9.5 * 72)

//define chart size with window
chart.SizeWithWindow = true;

//set window width of the workbook in inches
wb.Worksheets.WindowWidthInch = desiredWidth / 72f;

//set window height of the workbook in inches
wb.Worksheets.WindowHeightInch = desiredHeight / 72f;

//Instantiate MemoryStream
MemoryStream ms = wb.SaveToStream();

//Create an OLE Object Frame with embedded Excel
Aspose.Slides.OleObjectFrame objFrame = slide.Shapes.AddOleObjectFrame(
							36,
							72,
							desiredWidth,
							desiredHeight, "Excel.Sheet.8", ms.ToArray());

Second Approach

In this approach, we will learn how to set the chart size present in the embedded Excel Workbook equivalent to the size of the OLE Object Frame in the PowerPoint Slide. This approach is useful when the size of the chart up-front is known and will never change.

Scenario 1

Suppose, we have defined a template and desire to create the presentations based on this template. Let us say there is some shape at index 2 in the template where we want to place an OLE Frame carrying an embedded Excel Workbook. In this scenario, the size of the OLE Frame will be considered as predefined (which is the size of the shape at index 2 in the template). All we have to do: set the size of the chart in the Workbook equal to the size of the shape. The following code snippet will serve this purpose:

//define chart size without window 
chart.SizeWithWindow = false;

//set chart width in pixels (Multiply by 96 as Excel uses 96 pixels per inch)    
chart.ChartObject.Width = (int)((slide.Shapes[2].Width / 72f) * 96f);

//set chart height in pixels
chart.ChartObject.Height = (int)((slide.Shapes[2].Height / 72f) * 96f);

//Define chart print size
chart.PrintSize = PrintSizeType.Custom;

//Instantiate MemoryStream
MemoryStream ms = wb.SaveToStream();

//Create an OLE Object Frame with embedded Excel
Aspose.Slides.OleObjectFrame objFrame = slide.Shapes.AddOleObjectFrame(
				slide.Shapes[2].X,
				slide.Shapes[2].Y,
				slide.Shapes[2].Width,
				slide.Shapes[2].Height, "Excel.Sheet.8", ms.ToArray());

Scenario 2

Let us say, we want to create a presentation from scratch and desire an OLE Object Frame of any size with an embedded Excel Workbook. In the following code snippet, we have created an OLE Object Frame with 4 inch height and 9.5 inch width in the slide at x-axis=0.5 inch and y-axis=1 inch. Further, we have set the equivalent Chart size, that is: height 4 inch and width 9.5 inch.

 //Our desired height
int desiredHeight = 288;//4 inch (4 * 576)

//Our desired width
int desiredWidth = 684;//9.5 inch (9.5 * 576)

//define chart size without window 
chart.SizeWithWindow = false;

//set chart width in pixels    
chart.ChartObject.Width = (int)((desiredWidth / 72f) * 96f);

//set chart height in pixels    
chart.ChartObject.Height = (int)((desiredHeight / 72f) * 96f);

//Instantiate MemoryStream
MemoryStream ms = wb.SaveToStream();

//Create an OLE Object Frame with embedded Excel
Aspose.Slides.OleObjectFrame objFrame = slide.Shapes.AddOleObjectFrame(
							36,
							72,
							desiredWidth,
							desiredHeight, "Excel.Sheet.8", ms.ToArray());

Conclusion

Creating and Embedding an Excel Chart as OLE Object in Presentation

Updating OLE Objects automatically