Εύρεση και αντικατάσταση κειμένου χωρίς απώλεια μορφοποίησης σε παρουσίαση

Και οι δύο μέθοδοι ακολουθούν τα παρακάτω βήματα:

  • Ανοίξτε μια παρουσίαση.
  • Αναζητήστε το κείμενο.
  • Αντικαταστήστε το κείμενο.
  • Αποθηκεύστε την παρουσίαση.

VSTO


 private void findReplaceText(string strToFind, string strToReplaceWith)

{

//Άνοιγμα της παρουσίασης
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);

//Διάσχιση των διαφανειών
foreach (PowerPoint.Slide sld in pres.Slides)
	//Διάσχιση όλων των σχημάτων στη διαφάνεια
	foreach (PowerPoint.Shape shp in sld.Shapes)
	{
		//Πρόσβαση στο κείμενο του σχήματος
		string str = shp.TextFrame.TextRange.Text;
		//Εύρεση κειμένου προς αντικατάσταση
		if (str.Contains(strToFind))
		//Αντικατάσταση υπάρχοντος κειμένου με το νέο κείμενο
		{
			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)

{

	//Άνοιγμα της παρουσίασης
	Presentation pres = new Presentation("mytextone.ppt");

	//Ανάκτηση όλων των πλαισίων κειμένου στην παρουσίαση
	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)

				//Εύρεση κειμένου προς αντικατάσταση
				if (port.Text.Contains(strToFind))

				//Αντικατάσταση υπάρχοντος κειμένου με το νέο κείμενο
				{

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

}

Λήψη δείγματος κώδικα