Cari dan Ganti Teks Tanpa Kehilangan Format dalam Presentasi

Kedua metode mengikuti langkah-langkah berikut:

  • Buka presentasi.
  • Cari teks.
  • Ganti teks.
  • Tulis presentasi.

VSTO


 private void findReplaceText(string strToFind, string strToReplaceWith)

{

//Buka presentasi
PowerPoint.Presentation pres = null;

pres = Globals.ThisAddIn.Application.Presentations.Open("mytextone.ppt",

						  Microsoft.Office.Core.MsoTriState.msoFalse,

						  Microsoft.Office.Core.MsoTriState.msoFalse,

						  Microsoft.Office.Core.MsoTriState.msoFalse);

//Loop melalui slide
foreach (PowerPoint.Slide sld in pres.Slides)

	//Loop melalui semua shape di slide
	foreach (PowerPoint.Shape shp in sld.Shapes)

	{

		//Akses teks di shape
		string str = shp.TextFrame.TextRange.Text;

		//Cari teks yang akan diganti
		if (str.Contains(strToFind))

		//Ganti teks yang ada dengan teks baru
		{

			int idx = str.IndexOf(strToFind);

			string strStartText = str.Substring(0, idx);

			string strEndText = str.Substring(idx + strToFind.Length, str.Length - 1 - (idx + strToFind.Length - 1));

			shp.TextFrame.TextRange.Text = strStartText + strToReplaceWith + strEndText;

		}

		pres.SaveAs("MyTextOne___.ppt",

		PowerPoint.PpSaveAsFileType.ppSaveAsPresentation,

		Microsoft.Office.Core.MsoTriState.msoFalse);

	}

}

Aspose.Slides


 private static void findReplaceText(string strToFind, string strToReplaceWith)

{

	//Buka presentasi
	Presentation pres = new Presentation("mytextone.ppt");

	//Dapatkan semua kotak teks dalam presentasi
	ITextBox[] tb = PresentationScanner.GetAllTextBoxes(pres, false);

	for (int i = 0; i < tb.Length; i++)

		foreach (Paragraph para in tb[i].Paragraphs)

			foreach (Portion port in para.Portions)

				//Cari teks yang akan diganti
				if (port.Text.Contains(strToFind))

				//Ganti teks yang ada dengan teks baru
				{

					string str = port.Text;

					int idx = str.IndexOf(strToFind);

					string strStartText = str.Substring(0, idx);

					string strEndText = str.Substring(idx + strToFind.Length, str.Length - 1 - (idx + strToFind.Length - 1));

					port.Text = strStartText + strToReplaceWith + strEndText;

				}

	pres.Write("myTextOneAspose.ppt");

}

Unduh Kode Contoh