Znajdź i zamień tekst bez utraty formatu w prezentacji

Obie metody wykonują następujące kroki:

  • Otwórz prezentację.
  • Wyszukaj tekst.
  • Zastąp tekst.
  • Zapisz prezentację.

VSTO


 private void findReplaceText(string strToFind, string strToReplaceWith)

{

//Otwórz prezentację

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);

//Iteruj przez slajdy

foreach (PowerPoint.Slide sld in pres.Slides)

	//Iteruj przez wszystkie kształty na slajdzie

	foreach (PowerPoint.Shape shp in sld.Shapes)

	{

		//Uzyskaj dostęp do tekstu w kształcie

		string str = shp.TextFrame.TextRange.Text;

		//Znajdź tekst do zamiany

		if (str.Contains(strToFind))

		//Zastąp istniejący tekst nowym tekstem

		{

			int idx = str.IndexOf(strToFind);

Aspose.Slides


 private static void findReplaceText(string strToFind, string strToReplaceWith)

{

	//Otwórz prezentację
	Presentation pres = new Presentation("mytextone.ppt");
	//Pobierz wszystkie pola tekstowe w prezentacji
	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)
				//Znajdź tekst do zamiany
				if (port.Text.Contains(strToFind))
				//Zastąp istniejący tekst nowym tekstem
				{
					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");
}

Pobierz przykładowy kod