We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to add multiple textboxes to a slide

I am able add a single textbox, when I try to add multiple textboxes, I ran into exception,

I am using following code…my main()


public static void main(String[] args) {
//Instantiate the Presentation class that represents the presentation file
Presentation pres = new Presentation();
ISlide sld = pres.getSlides().get_Item(0);


String[] coverText = {“One One”, “Two Two”, “Three Three”, “Four Four”};
int[] setOffsetY = {170, 245, 325, 400};
int[] fontSize = {32, 35, 35, 25};
//Add an AutoShape of Rectangle type
for (int i = 0; i < coverText.length; i++) {
IAutoShape ashp = sld.getShapes().addAutoShape(ShapeType.Rectangle, 50, setOffsetY[i], 500, 20);

//Add TextFrame to the Rectangle and Set Text
ashp.addTextFrame(coverText[i]);

ashp.getFillFormat().setFillType(FillType.NoFill);
ashp.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
}
for (int j = 0; j < coverText.length; j++) {
//Accessing the text frame
ITextFrame tf1 = ((IAutoShape) sld.getShapes().get_Item(j)).getTextFrame();

//Create the Paragraph object for text frame
IParagraph para1 = tf1.getParagraphs().get_Item(j);

//seting TextAlignment to Left
para1.getParagraphFormat().setAlignment(TextAlignment.Left);

//Create Portion object for paragraph
IPortion port1 = para1.getPortions().get_Item(j);

//seting Font Size
port1.getPortionFormat().setFontHeight(fontSize[j]);

//Setting Bold true
port1.getPortionFormat().setFontBold(NullableBool.True);

//Making Font Solid
port1.getPortionFormat().getFillFormat().setFillType(FillType.Solid);

//Setting font Color to White
port1.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.white);
}

//Write the presentation to disk
pres.save(“firstSilde.pptx”, SaveFormat.Pptx);
System.out.println(“I am done here”);
}



And exception :-


Parameter name: Parameter name: index
com.aspose.slides.Collections.Generic.List.get_Item(Unknown Source)
com.aspose.slides.ParagraphCollection.get_Item(Unknown Source)
firstslide.Firstslide.main(Firstslide.java:61)
at com.aspose.slides.Collections.Generic.List.get_Item(Unknown Source)
at com.aspose.slides.ParagraphCollection.get_Item(Unknown Source)
at firstslide.Firstslide.main(Firstslide.java:61)
Java Result: 1
BUILD SUCCESSFUL (total time: 11 seconds)

First Loop works fine. In 2nd Loop, for 2nd iteration it through exception @ following line…

//Create the Paragraph object for text frame
IParagraph para1 = tf1.getParagraphs().get_Item(j);

Hi Abdul Ghaffar,

Thanks for inquiring Aspose.Slides.

I have observed the sample code shared by you and have observed issue in your sample code when code execute in second for loop. You are actually setting the variable j as index for Paragraph and Portion number inside the text frame. Where as there is only one paragraph and portion in text frame. Therefore, the first iteration where j=0 the things work fine and exception is thrown for next one. You need to please use perform following modification in your sample code to serve the purpose.

//Create the Paragraph object for text frame
IParagraph para1 = tf1.getParagraphs().get_Item(0);

//Create Portion object for paragraph
IPortion port1 = para1.getPortions().get_Item(0);


Many Thanks