In previous posts, I showed you how to import charts from spreadsheets to a Wordprocessing document and how to import tables from Word documents to spreadsheets. Today, I am going to show you how to import SmartArt from a PowerPoint deck into a Word document.
To import a SmartArt graphic from a PowerPoint deck to a Word document we need to take the following actions:
My post will talk about using version 2 of the SDK.
For the sake of this post, let's say I am starting with the following PowerPoint deck and SmartArt graphic:
Also, let's say I am starting with the following document, which contains a placeholder content control:
If you just want to jump straight into the code, feel free to download this solution here.
As described in the solution section above, steps two and three require us to open the PowerPoint deck, find the slide with the SmartArt graphic, and extract all related parts. Below is the code snippet necessary to accomplish those tasks:
Note that in the code above I knew which part/slide contained the SmartArt graphic.
Next step is to retrieve some necessary information from the SmartArt graphic itself. For example, we can retrieve the name, id, extent width, and extent height. The following code snippet retrieves the SmartArt elements that contain all this information (we will use these objects later in the code):
Now we get to do the fun stuff; import the SmartArt graphic into the Word document. Below is the code snippet necessary to accomplish steps five and six as mentioned in the solution section above:
Notice how we are simply importing the parts from the PowerPoint deck into the Word document. The next step is to create a paragraph object that contains the SmartArt. The easiest way to accomplish this task is to leverage the Document Reflector tool that comes with the SDK. This tool is awesome! It automatically generates SDK code based on a document or content within a document. I would recommend checking out Erika's post on how to use the Document Reflector.
Here is a screenshot of the Document Reflector in action when reading in a Word document containing a SmartArt graphic. Notice the nice C# code it generated for me.
Copying this code into my solution I get the following:
The code snippet above is pretty much a copy/paste. The only difference is that I added the correct extent width and height values as well as the proper name and ids for the SmartArt graphic.
Almost done! The next thing we need to do is find the content control within the Word document and swap out the content control for this newly created paragraph. The following code snippet accomplishes this task:
Putting everything together and running this code, we end up with a Word document, which contains the SmartArt imported from my deck.
Here is a screenshot of the final document: