I just want to let you guys know we are working on some server issues here, which is why some of the previous posts are not showing images or links to source code. This issue will hopefully be resolved soon.
In my previous post, I showed you the easy way to assemble multiple Word documents. Today, I am going to show you how to assemble multiple PowerPoint decks together.
To merge two decks, a source deck and a destination deck, together 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 two decks, each with three slides:
If you just want to jump straight into the code, feel free to download this solution here.
As described in the solution section above, the first three steps require us to open both the destination and source decks in order to add every slide in the source deck to the destination deck. Below is the code snippet necessary to accomplish those tasks:
We needed to go through the slide id list in order to ensure that we added all the slides from the source deck to the destination deck in order. In addition, note that we also taking advantage of AddPart, which allows us to not only add a slide part, but all parts referenced by that slide part.
At this point we have just imported the slide part to the destination deck. Our next task is to fix up the relationship between the main destination presentation part to the added slide master part. Again, we are going to take advantage of AddPart to add this relationship with the following code (AddPart will fix up relationships if the part already exists in the package):
The next steps are to add the list of slide master part ids and slide ids to the appropriate lists in the main destination presentation part. The ids referenced in each of these lists need to be unique. An additional constraint is that the id values across the slide master id and the slide layout id lists need to be unique and are required to have id values that are greater than or equal to 2147483684. To help deal with the uniqueness factor we need a method that is able to return the current max id value in the list. So anytime we add a new item to the list we simply use the max id value and add 1. Below is a generic method that is able to retrieve the max id value in a set of children elements:
This method is called before the initial for loop in order to get the appropriate unique ids for the slide master part id and slide id lists. Once we have the unique ids we can add the slide master part id and slide id to the appropriate lists in the main destination presentation part. Below is the necessary code needed to add to these two lists:
Almost done! The last bit of work we need to do is ensure that all slide layout ids are unique. As mentioned above these id values cannot conflict with the id values of the slide master part id list. In addition we need to make sure that these ids are greater than 2147483684. Below is a method that will go through all slide master parts and will fix up all referenced slide layout ids by simply incrementing id values based on the maximum seen uint value seen thus far:
Putting everything together and running this code, we end up with a presentation that has six slides, all in the proper order.
Here is a screenshot of the final presentation: