Free Support Forum - aspose.com

[JAVA] altering the text of a shape

This should be a simple question… After calling Presentation.addBodySlide(), how should I go about setting the text of the shapes’ TextFrames? I can set the text by getting the Placeholders and casting them to TextHolders, but nothing I do to the TextFrames seems to have an impact on the saved slide.

I’ve tried calling setText() directly, as well as setting the text on a Paragraph or a Portion belonging to the TextFrame. Is there something I’m missing?

Here’s one version of my attempts to get the TextFrame to work:

Shapes shapes = slide.getShapes();
try
{
for(int i = 0; i < shapes.size(); i++)
{
TextFrame frame = shapes.get(i).getTextFrame();
frame.getParagraphs().add(0, new Paragraph());
Paragraph block = frame.getParagraphs().get(0);
block.getPortions().add(0, new Portion("Shape " + i));
}
}

I had to add new Paragraphs and Portions because the shapes made by addBodySlide() don’t seem to have any paragraphs set by default.

So, can anyone offer any advice? Thanks.

TextFrame is not a Shape so it can’t exist in Shapes collection.
Short scenario to add TextFrames:

1. Call addEmptySlide() to add slide without placeholders.

Slide slide = pres.addEmptySlide();

2. Add Rectnagle to a list of shapes.

Rectangle rect = slide.getShapes().addRectangle(100,100,3000,500);

3. Create TextFrame inside rectangle (with single space instead of text in our case).

rect.addTextFrame(" ");

4. Change created TextFrame.

TextFrame frame = rect.getTextFrame();
frame.getParagraphs().add(0, new Paragraph());
Paragraph block = frame.getParagraphs().get(0);
block.getPortions().add(0, new Portion("Shape " + i));

The main advantage of this way is TextFrame can be added not only to Rectangles
but to any shape. For example Ellipse, AutoShape, Polyline and in theory to any other.

Actually, I’ve gotten this to work in the past already, but the new shape doesn’t inherit the style of the existing presentation. I don’t know where to find the default fonts from the slide, so I’ve been trying to edit an existing shape. When I set the test using TextHolders, at least the font matches the presentation. For example, when I edit TextHolder associated with the body shape made by addBodySlide(), the font size and color match the rest of the slides’ design.

How can I get/set the font for a TextFrame based on the presentation’s style? I’ll dig through the API reference… Maybe I can spot it.

Just documenting my progress here…

It looks like the master slide contains Rectangles with formatted portions that represent various text styles for the slide. Copying these portions’ settings should let me duplicate the presentation’s design for my new text frames. Is there a more straightforward method of obtaining these settings besides parsing through the master slide?

If you found how to parse master slide and can find template for paragraphs and portions
it’s the most simple and correct way.