動態新增文字

兩個方法遵循以下步驟:

  • 建立簡報。
  • 新增空白投影片。
  • 新增文字方塊。
  • 設定文字內容。
  • 寫入簡報。

VSTO


 private void AddTextBox()

{

	//建立簡報

	PowerPoint.Presentation pres = Globals.ThisAddIn.Application

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

	//取得空白投影片版面配置

	PowerPoint.CustomLayout layout = pres.SlideMaster.

		CustomLayouts[7];

	//新增空白投影片

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

	//新增文字

	PowerPoint.Shape shp =sld.Shapes.AddTextbox

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

	//設定文字

	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;

	//將輸出寫入磁碟

	pres.SaveAs("outVSTOAddingText.ppt",

		PowerPoint.PpSaveAsFileType.ppSaveAsPresentation,

		Microsoft.Office.Core.MsoTriState.msoFalse);

}

Aspose.Slides


 static void AddTextBox()

{

	//建立簡報

	Presentation pres = new Presentation();

	//預設情況下會新增空白投影片,當您建立
	//簡報時使用預設建構函式
	//因此,我們不需要再新增任何空白投影片

	Slide sld = pres.GetSlideByPosition(1);

	//取得 Arial 的字型索引
	//如果您使用預設建構函式建立簡報,則始終為 0
	//預設建構函式
	int arialFontIndex = 0;

	//新增文字方塊
	//為了新增它,我們會先新增一個矩形
	Shape shp = sld.Shapes.AddRectangle(1200, 800, 3200, 370);

	//隱藏其線條
	shp.LineFormat.ShowLines = false;

	//然後在其中新增文字框
	TextFrame tf = shp.AddTextFrame("");

	//設定文字
	tf.Text = "Text added dynamically";

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

	port.FontIndex = arialFontIndex;

	port.FontBold = true;

	port.FontHeight = 32;

	//將輸出寫入磁碟
	pres.Write("outAspose.ppt");

}

下載範例程式碼