Free Support Forum - aspose.com

Surround Text with Shape

What is the best way to surround text with a shape? From the examples and this forum, it seems that the shape comes first; draw a rectangle, for example, then add a text frame and paragraphs to the rectangle. My goal is the opposite: given a paragraph, draw a rectangle around it. I’ve tried adding a rectangle and positioning it via setY(), but am hoping there is an easier and more precise way.

Ken

Dear Ken,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

I regret to inform you that it is unfortunately not possible to generate the shape based on the text inside the text frame to surround the text. In fact one doesn’t need to do this as well. The text frame only contains text and its width and height are inherited from the shape. However, there are two properties of TextFrame class that can be used to accommodate any text inside the shape. TextFrame.setFitShapeToText(true) and TextFrame.setWrapText(true) properties can be used to accommodate the text inside the shape automatically and the size of shape will be altered to a certain limit to accommodate text inside the text frame, if the text exceeds shape limit. Below please find the code snippet using the above mentioned properties and I feel may be this only possibility with Aspose.Slides for Java may serve your purpose.

public static void AddSampleTextFrame()

{

//Create presentation

Presentation pres = new Presentation();

//Get first Slide, first slide is added automatically

Slide firstSlide = pres.getSlideByPosition (1);

//Add Rectangle inside the first slide

//This rectangle will contain the textframe inside it

com.aspose.slides.Rectangle rect = firstSlide.getShapes().addRectangle(300, 1000, 2220, 100);

//Make its line invisble

rect.getLineFormat().setShowLines(false);

//Now add a text frame inside the rectangle with some sample text

rect.addTextFrame("This is sample text.This is sample text.This is sample text.This is sample text.This is sample text.This is sample text.This is sample text.This is sample text.This is sample text.This is sample text.This is sample text.This is sample text.This is sample text.This is sample text.");

TextFrame tf = rect.getTextFrame();

//Set its wrap text property true, so it wraps text automatically

tf.setWrapText(true);

//Set this propert true, so textframe can chage its shape automatically

tf.setFitShapeToText(true);

//write the presentation

pres.write("D:\\Aspose Data\\JavaoutSampleTextFrame.ppt");

}

Thanks and Regards,