Adding a chart in a chart placeholder

Hello.

I need to copy layouts from a template PPTX file into a new PPTX file.

If I have a master slide in a PPTX, how do I choose a layout (with placeholders in it) from this master slide (because as far as I see in PowerPoint, there are many layouts in a master slide).
Here I see a helpful post:
...but not help full enough. Should I create a normal (not master) slide for each layout and then clone the slide to add it in the new PPTX?

And then, how do I add, lets say, a chart in a chart placeholder?
Here it only shows how to modify the text of a text placeholder :
http://www.aspose.com/docs/display/slidesnet/Replacing+Text+on+a+PlaceHolder
Does TextHolder handle all types of placeholders (text, chart, picture, etc)?
If yes, how do I add a chart in it?

Thanks in advance,
Kostas.

Hi Kostas,

I like to share that layout slides are part of master slides and Aspose.Slides allows you to clone the master slides. Once master is cloned, you can assign the layout slide of that particular master to any of your slide. Please visit this link for your convenience.

Many Thanks,

Thanks a lot!

Concerning my 2nd question, do you support placeholders for content other that text?

Hi Kostas,


Please try using the following sample code on your end. You can tailor it to serve your purpose as well.

public static void AddChartPlacehoder()
{
PresentationEx presentation = new PresentationEx(“D:\Aspose Data\TemplatePPT.pptx”);
//PresentationEx presentation = new PresentationEx();
presentation.UpdateDateTimeFields = true;
foreach (LayoutSlideEx layout in presentation.Masters[0].LayoutSlides)
{
presentation.Slides.AddEmptySlide(layout);
SlideEx slide = presentation.Slides[presentation.Slides.Count - 1];
foreach (ShapeEx shape in slide.LayoutSlide.Shapes)
{
if (shape.Placeholder != null)
{
switch (shape.Placeholder.Type)
{
default:
slide.Shapes.AddAutoShape(
ShapeTypeEx.RoundCornerRectangle, float.NaN, float.NaN, float.NaN, float.NaN);
break;
case PlaceholderTypeEx.SlideNumber:
{
slide.Shapes.AddAutoShape(
ShapeTypeEx.RoundCornerRectangle, float.NaN, float.NaN, float.NaN, float.NaN);
AutoShapeEx ashape = (AutoShapeEx)slide.Shapes[slide.Shapes.Count - 1];
ashape.AddTextFrame(“text”);
ashape.TextFrame.Paragraphs[0].Portions[0].AddField(FieldTypeEx.SlideNumber);
}
break;
case PlaceholderTypeEx.DateAndTime:
{
slide.Shapes.AddAutoShape(
ShapeTypeEx.RoundCornerRectangle, float.NaN, float.NaN, float.NaN, float.NaN);
AutoShapeEx ashape = (AutoShapeEx)slide.Shapes[slide.Shapes.Count - 1];
ashape.AddTextFrame(“text”);
ashape.TextFrame.Paragraphs[0].Portions[0].AddField(FieldTypeEx.DateTime);
}
break;
case PlaceholderTypeEx.Object:
case PlaceholderTypeEx.Table:
slide.Shapes.AddTable(
float.NaN, float.NaN, new double[] { 30, 30, 30 }, new double[] { 20, 20 });
break;
case PlaceholderTypeEx.Picture:
slide.Shapes.AddPictureFrame(
ShapeTypeEx.Ellipse, float.NaN, float.NaN, float.NaN, float.NaN, null);
break;
case PlaceholderTypeEx.Chart:
slide.Shapes.AddChart(
Aspose.Slides.Pptx.Charts.ChartTypeEx.StackedLine,
float.NaN,
float.NaN,
float.NaN,
float.NaN);
break;
//case PlaceholderTypeEx.Diagram:
// // TODO: add diagram insertion here
// break;
case PlaceholderTypeEx.Media:
slide.Shapes.AddVideoFrame(float.NaN, float.NaN, float.NaN, float.NaN, @“video.avi”);
break;
}

ShapeEx newShape = slide.Shapes[slide.Shapes.Count - 1];
newShape.AddPlaceholder(shape.Placeholder);
}
}
}
presentation.Save(“D:\Aspose Data\placeholders.pptx”, Aspose.Slides .Export .SaveFormat .Pptx);
}

Many Thanks,

That code looks interesting. Thanks! I will try it.


Is it possible to use placeholders in aspose slides for dynamic content and still prevent overlapping of content? Is there any sort of “grid layout” that adjusts the size of its elements based on their size?

I am supposed to use placeholders for slides and I can have 3 elements in one slide (1 title, 1 chart and 1 table) that can vary in size. I need a way to prevent them from overlapping when using placeholders.
Is there any?

(I guess that positioning the elements by code would solve the problem but the requirements are to use placeholders…)

Also, once I find a shape with a placeholder, I could use this shape, right? E.g. add data to a chart placeholder. In that way perhaps I could keep the format settings (colors, fonts, etc) intact and not have to copy them to a new shape.

In your code you create new shapes after you find the placeholder. Is there a reason for it? If I place a chart placeholder in a PPTX document, won’t it be a ChartEx shape with a placeholder of type PlaceholderTypeEx.Chart ?

I just saw that the shape that has the placeholder is an AutoShapeEx, no matter what.

So, how do I copy the formating of a placeholder to my new shape?
As far as I have seen, in PowerPoint you can set colors and fonts for a placeholder in a layout slide. How can I access and copy those settings?
When I use your code above, not all shapes get the format (background color, etc) specified in the layout. I wonder how they get positioned right… :frowning:

I use the following (slightly modified) code, but most of the shapes (if not all) don’t get the formating of the layout (background color, etc). It seems that I can’t remove all of the old placeholders either (so, I get a textbox and there is the placeholder shape behind it…) :

			PresentationEx presentation = new PresentationEx(@“C:\Users\User\Desktop\Template.pptx”);
//PresentationEx presentation = new PresentationEx();
presentation.UpdateDateTimeFields = true;
foreach (LayoutSlideEx layout in presentation.Masters[0].LayoutSlides)
{
int at = presentation.Slides.AddEmptySlide(layout);
SlideEx slide = presentation.Slides[at];
for (int i = 0; i < slide.Shapes.Count; i++) slide.Shapes.RemoveAt(0);
			<span style="color:blue;">foreach</span> (<span style="color:#2b91af;">ShapeEx</span> shape <span style="color:blue;">in</span> slide.LayoutSlide.Shapes)
			{
				<span style="color:blue;">if</span> (shape.Placeholder != <span style="color:blue;">null</span>)
				{
					<span style="color:blue;">switch</span> (shape.Placeholder.Type)
					{
						<span style="color:blue;">default</span>:
							slide.Shapes.AddAutoShape(
								<span style="color:#2b91af;">ShapeTypeEx</span>.RoundCornerRectangle, <span style="color:blue;">float</span>.NaN, <span style="color:blue;">float</span>.NaN, <span style="color:blue;">float</span>.NaN, <span style="color:blue;">float</span>.NaN);
							<span style="color:blue;">break</span>;

						<span style="color:blue;">case</span> <span style="color:#2b91af;">PlaceholderTypeEx</span>.SlideNumber:
							{
								slide.Shapes.AddAutoShape(
									<span style="color:#2b91af;">ShapeTypeEx</span>.RoundCornerRectangle, <span style="color:blue;">float</span>.NaN, <span style="color:blue;">float</span>.NaN, <span style="color:blue;">float</span>.NaN, <span style="color:blue;">float</span>.NaN);
								<span style="color:#2b91af;">AutoShapeEx</span> ashape = (<span style="color:#2b91af;">AutoShapeEx</span>)slide.Shapes[slide.Shapes.Count - 1];
								ashape.AddTextFrame(<span style="color:#a31515;">"text"</span>);
								ashape.TextFrame.Paragraphs[0].Portions[0].AddField(<span style="color:#2b91af;">FieldTypeEx</span>.SlideNumber);
							}
							<span style="color:blue;">break</span>;
						<span style="color:blue;">case</span> <span style="color:#2b91af;">PlaceholderTypeEx</span>.DateAndTime:
							{
								slide.Shapes.AddAutoShape(
									<span style="color:#2b91af;">ShapeTypeEx</span>.RoundCornerRectangle, <span style="color:blue;">float</span>.NaN, <span style="color:blue;">float</span>.NaN, <span style="color:blue;">float</span>.NaN, <span style="color:blue;">float</span>.NaN);
								<span style="color:#2b91af;">AutoShapeEx</span> ashape = (<span style="color:#2b91af;">AutoShapeEx</span>)slide.Shapes[slide.Shapes.Count - 1];
								ashape.AddTextFrame(<span style="color:#a31515;">"text"</span>);
								ashape.TextFrame.Paragraphs[0].Portions[0].AddField(<span style="color:#2b91af;">FieldTypeEx</span>.DateTime);
							}
							<span style="color:blue;">break</span>;
						<span style="color:blue;">case</span> <span style="color:#2b91af;">PlaceholderTypeEx</span>.Object:
						<span style="color:blue;">case</span> <span style="color:#2b91af;">PlaceholderTypeEx</span>.Table:
							slide.Shapes.AddTable(
								<span style="color:blue;">float</span>.NaN, <span style="color:blue;">float</span>.NaN, <span style="color:blue;">new</span> <span style="color:blue;">double</span>[] { 30, 30, 30 }, <span style="color:blue;">new</span> <span style="color:blue;">double</span>[] { 20, 20 });
							<span style="color:blue;">break</span>;
						<span style="color:blue;">case</span> <span style="color:#2b91af;">PlaceholderTypeEx</span>.Picture:
							slide.Shapes.AddPictureFrame(
								<span style="color:#2b91af;">ShapeTypeEx</span>.Ellipse, <span style="color:blue;">float</span>.NaN, <span style="color:blue;">float</span>.NaN, <span style="color:blue;">float</span>.NaN, <span style="color:blue;">float</span>.NaN, <span style="color:blue;">null</span>);
							<span style="color:blue;">break</span>;
						<span style="color:blue;">case</span> <span style="color:#2b91af;">PlaceholderTypeEx</span>.Chart:
							slide.Shapes.AddChart(
								Aspose.Slides.Pptx.Charts.<span style="color:#2b91af;">ChartTypeEx</span>.StackedLine,
								<span style="color:blue;">float</span>.NaN,
								<span style="color:blue;">float</span>.NaN,
								<span style="color:blue;">float</span>.NaN,
								<span style="color:blue;">float</span>.NaN);
							<span style="color:blue;">break</span>;
						<span style="color:green;">//case PlaceholderTypeEx.Diagram:</span>
						<span style="color:green;">//    // </span><span style="font-weight:bold;color:darkblue;">TODO: add diagram insertion here</span>
						<span style="color:green;">//    break;</span>
						<span style="color:blue;">case</span> <span style="color:#2b91af;">PlaceholderTypeEx</span>.Media:
							slide.Shapes.AddVideoFrame(<span style="color:blue;">float</span>.NaN, <span style="color:blue;">float</span>.NaN, <span style="color:blue;">float</span>.NaN, <span style="color:blue;">float</span>.NaN, <span style="color:#a31515;">@"video.avi"</span>);
							<span style="color:blue;">break</span>;
					}

					<span style="color:#2b91af;">ShapeEx</span> newShape = slide.Shapes[slide.Shapes.Count - 1];
					newShape.AddPlaceholder(shape.Placeholder);
				}
			}
		}
		<span style="color:blue;">string</span> str = <span style="color:#a31515;">@"C:\Users\</span><span style="color: rgb(163, 21, 21);">User</span><span style="color: rgb(163, 21, 21);">\Desktop\Output2.pptx"</span>;<br>			presentation.Save(str, Aspose.Slides.Export.<span style="color:#2b91af;">SaveFormat</span>.Pptx);

But the new shapes go to the right place without any placement code and this makes me curious.
Why doesn’t this work right???

What should I do if I want to copy the formating (font, background color, border) of a placeholder into my new shape?
And is there any documentation on the placeholders (because I can’t find any)?


Hi Kostas,

Yes you are right that placeholder with text are same as normal autoshapes. In order to copy the formatting of text from one placeholder to another you need to copy the desired paragraphs and their respective portions from one placeholder paragraph to another. Please visit this documentation link for your convenience to observe how to access paragraph and portions. Please visit this link for your convenience to see how to copy the paragraph and portions. Please also visit this thread link and observe the code given in the attachment where by a customer has shared the scenario where he is trying to copy paragraphs.

Also, the autoshapes can be moved to desired place as per your convenience using X and Y properties of ShapeEx and unfortunately there is no mechanism available to identify if that if the added text inside any particular shape has reached a level where it is overlapping other shapes. You may need to devise your own logic in this regard.

Please share if I may help you further in this regard.

Many Thanks,

Thanks!
I’ve figured things out now.

Just two questions:

1) When you do this:
slide.Shapes.AddPictureFrame(ShapeTypeEx.Ellipse, float.NaN, float.NaN, float.NaN, float.NaN, null);
…how does aspose/office know which placeholder to use for getting the coordinates for the new shape?
2) Why don’t the styles of the layout (font, colors, etc) get applied automatically (just like the shape coordinates do) for each shape and I have to set them by code?
In PowerPoint, whenever you change the master slide, all the styling gets updated.
I have the impression that setting the styling by code prevents PowerPoint from setting it automatically when the user changes the master slide.
That is a problem for me.

I think I have the answers now:

1) It sems that MS Office takes the 1st placeholder of the desired type that isn’t used already.
2) It seems that some the styling does get applied. I’ll check it a liitle bit more and, if I have any problem, I will post here again.

Hi Kostas,


Thanks for your growing interest in Aspose.Slides. You may share your feedback and inquiries with us once you complete your investigation.

Many Thanks,

Where can I find an example which is usable with the current version of the library - the float.NaNs are not accepted for e.g. AddAutoShape anymore!

Hi Kostas,


I have tried to understand your requirement and have not been able to completely understand that. I suggest you to please visit this documentation link for your kind reference in this regard to see how to add AutoShape using new API. I hope this will be helpful.

Many Thanks,

Still have the same question as QBKleidersack, how do I add for instance a table to a table placeholder? The example from 2013 doesn’t work now with System.ArgumentException: ‘Value of x paramenter must be defined (not a float.NaN).’

@motionqiwi,
Welcome to our community! Please describe your issue in a new forum topic and share the code example throwing ArgumentException.