Convert Selected PowerPoint Shapes (or DrawingML) to XAML

1.6k views Asked by At

I need to convert selected PowerPoint shapes to XAML so I can effectively place an equivalent vector-based shape inside my WPF app (the XAML end result has to be scalable - converting to an image defeats the purpose of what I'm trying to do).

I'm open to a variety of ways to accomplish this, including writing a PowerPoint addin (if this could get me access to the bezier point coordinates of the selected shapes in PowerPoint). I'm not familiar enough with PowerPoint addins to know if this is possible or not.

My first approach was to copy the PowerPoint shapes to the clipboard, and then convert the clipboard contents to objects I can understand and then generate the XAML from there. If I do this I can get to the DrawingML representation (through the Art::GVML ClipFormat), but it's unclear how to easily convert that DrawingML to XAML (looks like weeks of error-prone work to create from scratch).

There are other formats available on the clipboard (EMF, System.Drawing.Imaging.Metafile, PowerPoint 12.0 Internal Shapes), but they all appear to be dead ends.

Any suggestions?

1

There are 1 answers

0
csharpwinphonexaml On BEST ANSWER

You can try this:

Powerpoint Shape to WMF

Microsoft.Office.Interop.PowerPoint.Application app = new Microsoft.Office.Interop.PowerPoint.Application();
var pps = app.Presentations;
string d ="filepath.pptx";
Presentation ppt = pps.Open(d, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
for (int j = 1; j < ppt.Slides.Count; j++)
{
    Slide sld = ppt.Slides[j];
    List<Microsoft.Office.Interop.PowerPoint.Shape> shapes = new List<Microsoft.Office.Interop.PowerPoint.Shape>();
    for (int k = 1; k < sld.Shapes.Count; k++)
    {
        Microsoft.Office.Interop.PowerPoint.Shape shape = sld.Shapes[k]; 
        shape.Export("outputFilePath.wmf", PpShapeFormat.ppShapeFormatWMF);
    }
}

then WMF to XAML: Microsoft Forum:

1) Check out http://www.wpf-graphics.com/ReaderWmf.aspx and it's related project http://www.wpf-graphics.com/Paste2Xaml.aspx. These are EMF/WMF readers for WPF. You'd write a simple utility to read in the images and write them back out again. I've played with them a little and had pretty good luck with them. If they'll work for you, this would be my first choice. You'll need to test them of course. Conversions like this are never perfect due to difference in design between the various formats so make sure you're not using something that's not supported in the converter.

2) Another project I ran into was http://lab.aspitalia.com/15/EmfToXaml-Beta.aspx. I haven't played with it, but you might give it a look.

3) If the direct route doesn't work, you might consider converting the WMF files to a different vector format like SVG and then converting that SVG file to XAML. There are lots of tools for EMF/WMF to SVG conversion. For the SVG to XAML conversion you can look at http://www.codeplex.com/XamlTune and http://www.wpf-graphics.com/ReaderSvg.aspx. I doubt this will work as well as a direct conversion, but you never know.

About PowerPoint Add-ins and Running the above example you need Microsoft Office Interop libraries.