A sub-optimal yet functional way to do this is round trip using the clipboard. Slide(s) copied to the clipboard are actually packaged up as ppt data (which is just a zip). And you have control over the clipboard and inserting etc slides. So basically:
- Programmatically select and copy the content you want to edit to the clipboard using PP APIs.
- Read "PowerPoint 14.0 Slides Package" part of clipboard data into memory stream.
- Pass/edit memory stream with Open XML.
- Copy memory stream back to clipboard.
- paste back into PP as needed.
Here's an example of steps 2-3 working using this Open XML example as a base that write all text in the first slide in the clipboard to the console:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Drawing;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Presentation;
using P = DocumentFormat.OpenXml.Presentation;
using D = DocumentFormat.OpenXml.Drawing;
using DocumentFormat.OpenXml.Office2016.Drawing.ChartDrawing;
namespace PPClipboardTest
{
class Program
{
public static string format = "PowerPoint 14.0 Slides Package";
public static string[] GetAllTextInSlide(string presentationFile, int slideIndex)
{
using (PresentationDocument presentationDocument = PresentationDocument.Open(presentationFile, false))
{
return GetAllTextInSlide(presentationDocument, slideIndex);
}
}
public static string[] GetAllTextInSlide(PresentationDocument presentationDocument, int slideIndex)
{
if (presentationDocument == null)
{
throw new ArgumentNullException("presentationDocument");
}
if (slideIndex < 0)
{
throw new ArgumentOutOfRangeException("slideIndex");
}
PresentationPart presentationPart = presentationDocument.PresentationPart;
if (presentationPart != null && presentationPart.Presentation != null)
{
Presentation presentation = presentationPart.Presentation;
if (presentation.SlideIdList != null)
{
DocumentFormat.OpenXml.OpenXmlElementList slideIds =
presentation.SlideIdList.ChildElements;
if (slideIndex < slideIds.Count)
{
string slidePartRelationshipId = (slideIds[slideIndex] as SlideId).RelationshipId;
SlidePart slidePart =
(SlidePart)presentationPart.GetPartById(slidePartRelationshipId);
return GetAllTextInSlide(slidePart);
}
}
}
return null;
}
public static string[] GetAllTextInSlide(SlidePart slidePart)
{
if (slidePart == null)
{
throw new ArgumentNullException("slidePart");
}
LinkedList<string> texts = new LinkedList<string>();
if (slidePart.Slide != null)
{
foreach (DocumentFormat.OpenXml.Drawing.Paragraph paragraph in
slidePart.Slide.Descendants<DocumentFormat.OpenXml.Drawing.Paragraph>())
{
StringBuilder paragraphText = new StringBuilder();
foreach (DocumentFormat.OpenXml.Drawing.Text text in
paragraph.Descendants<DocumentFormat.OpenXml.Drawing.Text>())
{
paragraphText.Append(text.Text);
}
if (paragraphText.Length > 0)
{
texts.AddLast(paragraphText.ToString());
}
}
}
if (texts.Count > 0)
{
return texts.ToArray();
}
else
{
return null;
}
}
[STAThread]
static void Main(string[] args)
{
IDataObject iData = new DataObject();
iData = Clipboard.GetDataObject();
string[] formats = iData.GetFormats();
foreach(string f in formats)
{
if(f == format)
{
MemoryStream ms = iData.GetData(format) as MemoryStream;
using (PresentationDocument pres = PresentationDocument.Open(ms, true))
{
string[] allText = GetAllTextInSlide(pres, 0);
Console.Write("Text in first slide copied to clipboard:\n");
foreach (string txt in allText)
{
Console.Write(txt + "\n");
}
}
break;
}
}
}
}
}