Thêm Văn Bản Động

Cả hai phương pháp đều tuân theo các bước sau:

  • Tạo một bản trình chiếu.
  • Thêm một slide trống.
  • Thêm một hộp văn bản.
  • Đặt một đoạn văn bản.
  • Ghi lại bản trình chiếu.

VSTO


 private void AddTextBox()

{

	//Tạo một bản trình chiếu
	PowerPoint.Presentation pres = Globals.ThisAddIn.Application
		.Presentations.Add(Microsoft.Office.Core.MsoTriState.msoFalse);
	//Lấy bố cục slide trống
	PowerPoint.CustomLayout layout = pres.SlideMaster.
		CustomLayouts[7];
	//Thêm một slide trống
	PowerPoint.Slide sld = pres.Slides.AddSlide(1, layout);
	//Thêm văn bản
	PowerPoint.Shape shp =sld.Shapes.AddTextbox
	(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal,150, 100, 400, 100);
	//Đặt văn bản
	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;
	//Ghi đầu ra ra đĩa
	pres.SaveAs("outVSTOAddingText.ppt",
		PowerPoint.PpSaveAsFileType.ppSaveAsPresentation,
		Microsoft.Office.Core.MsoTriState.msoFalse);
}

Aspose.Slides


 static void AddTextBox()

{

	//Tạo một bản trình chiếu
	Presentation pres = new Presentation();
	//Slide trống được thêm mặc định, khi bạn tạo
	//bản trình chiếu từ hàm khởi tạo mặc định
	//Vì vậy, chúng ta không cần thêm bất kỳ slide trống nào
	Slide sld = pres.GetSlideByPosition(1);
	//Lấy chỉ số phông chữ cho Arial
	//Nó luôn luôn là 0 nếu bạn tạo bản trình chiếu từ
	//hàm khởi tạo mặc định
	int arialFontIndex = 0;
	//Thêm một hộp văn bản
	//Để thêm nó, chúng ta sẽ đầu tiên thêm một hình chữ nhật
	Shape shp = sld.Shapes.AddRectangle(1200, 800, 3200, 370);
	//Ẩn đường viền của nó
	shp.LineFormat.ShowLines = false;
	//Sau đó thêm một khung văn bản bên trong
	TextFrame tf = shp.AddTextFrame("");
	//Đặt văn bản
	tf.Text = "Text added dynamically";
	Portion port = tf.Paragraphs[0].Portions[0];
	port.FontIndex = arialFontIndex;
	port.FontBold = true;
	port.FontHeight = 32;
	//Ghi đầu ra ra đĩa
	pres.Write("outAspose.ppt");
}

Tải mã mẫu