This article will show you how to programmatically merge PowerPoint 2010 presentations into one using Open XML, Open XML SDK 2 and Visual Studio 2010.
We have 3 PowerPoint 2010 presentations containing a calendar for each year from year 2011 to 2013 respectively. We’re going to merge them in to one presentation using a template.
Create a Windows Forms Application Project:
1. Fire up Visual Studio 2010. Click on File->New->Project
2. Select Visual C#-> Windows and select the Windows Forms Application template
3. Set the name of the presentation to PowerPointPresentationMerger and select a desired location for the solution.
Design the Form:
1. Drag a button on the form as seen in the image below with the following properties:
Name: buttonMerge
Text: Merge
2. Drag 2 openFileDialog and 1 saveFileDialog on the form
Set the properties of the controls:
openFileDialog1:
Name: openFileDialogPresentations
MultiSelect: true
Filter: PowerPoint Presentation|*.pptx
openFileDialog2:
Name: openFileDialogTemplate
Filter: PowerPoint Presentation|*.pptx
saveFileDialog1:
Name: saveFileDialog
3. Drag 3 more buttons on the form
Set the properties of the controls:
button1:
Name: buttonPresentations
Text: Presentations to Merge
button2:
Name: buttonTemplate
Text: Template to Use
button3:
Name: buttonMergedOutput
Text: Merged Presentation
Create Events for the Controls
1. Double click buttonPresentations and enter the code below:
2. Double click buttonTemplate and enter the code below:
3. Double click buttonMergedOutput and enter the code below:
Add Reference to Open XML Assemblies
1. Right click PowerPointPresentationMerger project and select Add Reference
2. In the .NET tab, hold the crtl key and select DocumentFormat.OpenXml and WindowsBase then click OK.
Add the code for Open XML
1. In the form add the additional using statements
2. Create a variable to hold the unique id for both the slide master and the slide layout. Name the variable as uniqueId;
3. Create the GetMaxSlideMasterId method. This method will return the Slide Master Id. Note that Slide Master Ids should be greater than or equal to 2147483648. This method loops through the elements of the Slide Master Id List and checks whether the child id is greater than the defined max Id.
4. Create the GetMaxSlideId method. This method will return the Slide Id. Note that Slide Ids have a minimum value of greater than or equal to 256 and a maximum value of less than and a maximum value of less than 2147483648. Ids more than 2147483648, represents Slide Master Ids.
This method loops through the elements of the Slide Id List and checks whether the child id is greater than the defined max Id.
5. Create the FixSlideLayoutIds method. This method will ensure that each slide layouts inside the Slide Master Part in each Presentation Part will have a unique id. These ids must be unique to identify each layout from one another.
6. Create FormatErrors method. This method generates a string of error messages that will be used to display in a Message box. This loops through all items in the enumerable ValidationErrorInfo. These errors are generated when the Open XML Validator calls the validate method.
7. Create the MergePresentationSlides method. This method merges 1 PowerPoint Presentation to another. It does it by opening the destination PowerPoint as editable and checks if it has an existing Slide Id List. If it doesn’t the code will create one.
While the destination PowerPoint presentation is open, we open the source PowerPoint presentation
With both PowerPoint presentations open, we loop through each through each Slide Id list of the presentation in the Presentation part of the Source PowerPoint. We then copy these parts and create new relations in to the destination PowerPoint presentation. Note that we have to get the Max Slide Master Id and the Max Slide Id before the foreach statement as this will be used to generate the incremental Ids for the new slide master id and slide id. The GetMaxSlideMasterId and GetMaxSlideId are called to ensure that we always get the last id and incrementing it by one to ensure that all the Ids for the Master Slide Id and Slide Id are unique.
Next we make sure that all Slide Ids are unique in the destination PowerPoint presentation by calling the FixSlideLayoutIds method. Once everything is hunky-dory we now save the destination PowerPoint Presentation.
8. Create click event for buttonMerge. Double click buttonMerge. Add the variables below to capture the user’s new merged PowerPoint presentation, the user’s selected PowerPoint presentation to merge and the selected template to use.
9. Create a copy of the template presentation to generate the new presentation.
10. Loop through each selected source PowerPoint presentations and call the MergePresentationSlides method
11. Open the merged PowerPoint presentation and validate the contents and display appropriate error messages
Running the Code
1. Hit F5
2. Click on Presentations to Merge button
3. Inside the PresentationToMerge folder which is found the solution directory, select all presentations and click Open
4. Click on Template to Use button
5. Inside the TemplateToUse which is found the solution directory, select all Template.pptx and click Open
6. Click on the Merge Presentation button
7. Select a location for the merge presentation and give it a name that ends with .pptx and click on the Save button
8. Click on the Merge button
9. When there are no errors you will receive the following pop-up. Open the merged presentation.
10. Open the merged PowerPoint presentation to see that all 3 PowerPoint presentations are merged in to one.