Adding Text Dynamically

Both methods follow these steps:

  • Create a presentation.
  • Add a blank slide.
  • Add a text box.
  • Set some text.
  • Write the presentation.

VSTO


 private void AddTextBox()

{

	//Create a presentation

	PowerPoint.Presentation pres = Globals.ThisAddIn.Application

		.Presentations.Add(Microsoft.Office.Core.MsoTriState.msoFalse);

	//Get the blank slide layout

	PowerPoint.CustomLayout layout = pres.SlideMaster.

		CustomLayouts[7];

	//Add a blank slide

	PowerPoint.Slide sld = pres.Slides.AddSlide(1, layout);

	//Add a text

	PowerPoint.Shape shp =sld.Shapes.AddTextbox

	(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal,150, 100, 400, 100);

	//Set a text

	PowerPoint.TextRange txtRange = shp.TextFrame.TextRange;

	txtRange.Text = "Text added dynamically";

	txtRange.Font.Name = "Arial";

	txtRange.Font.Bold = Microsoft.Office.Core.MsoTriState.msoTrue;

	txtRange.Font.Size = 32;

	//Write the output to disk

	pres.SaveAs("outVSTOAddingText.ppt",

		PowerPoint.PpSaveAsFileType.ppSaveAsPresentation,

		Microsoft.Office.Core.MsoTriState.msoFalse);

}

Aspose.Slides


 static void AddTextBox()

{

	//Create a presentation

	Presentation pres = new Presentation();

	//Blank slide is added by default, when you create

	//presentation from default constructor

	//So, we don't need to add any blank slide

	Slide sld = pres.GetSlideByPosition(1);

	//Get the font index for Arial

	//It is always 0 if you create presentation from

	//default constructor

	int arialFontIndex = 0;

	//Add a textbox

	//To add it, we will first add a rectangle

	Shape shp = sld.Shapes.AddRectangle(1200, 800, 3200, 370);

	//Hide its line

	shp.LineFormat.ShowLines = false;

	//Then add a textframe inside it

	TextFrame tf = shp.AddTextFrame("");

	//Set a text

	tf.Text = "Text added dynamically";

	Portion port = tf.Paragraphs[0].Portions[0];

	port.FontIndex = arialFontIndex;

	port.FontBold = true;

	port.FontHeight = 32;

	//Write the output to disk

	pres.Write("outAspose.ppt");

}

Download Sample Code