Click here to Skip to main content
Click here to Skip to main content

Merge Presentations

By , 19 Jul 2012
 

Introduction

This article tells how to merge multiple presentation files to one file at a specified position using Office OpenXML SDK.

Overview

Let's copy one presentation to other manually. I have two presentation files: ppt1.pptx and ppt2.pptx. I would copy all slides from ppt2.pptx into ppt1.pptx.

ppt1.pptx

ppt2.pptx

Resultant presentation

This looks pretty easy if there are only couple of presentations to be copied. Think about the case where you need to copy/merge many presentations. So, I thought of copying these programmatically using Office OpenXML SDK 2.0.

Using the code

The PresentationInfo class contains information of file and its insert position in the destination file.

public class PresentationInfo
{
    public string File { get; set; }
    public short InsertPosition { get; set; }
}

The MergeHandler class contains a method MergeAllSlides which takes parameters source presentation, destination presentation and a list of presentations to be merged, and copies all the slides in these presentations into the destination presentation.

void MergeAllSlides(string parentPresentation, string destinationFileName, List<PresentationInfo> mergingFileList);  

MergeAllSlides does two things. First, it merges all the slides keeping count of source and destination locations of each slide. Later, it re-orders the slides.

// key=>current slide position, value=>new position of the slide
Dictionary<int, int> reOrderPair = new Dictionary<int, int>(); 
foreach (PresentationInfo importedFile in mergingFileList)
{
  try
  {
     int val = (importedFile.InsertPosition - 1) + reOrderPair.Count;
     this.MergeSlides(importedFile, destinationFileName, 
                      ref reOrderPair, ref val, out reOrderConstant);
  }
  catch
  {
     continue; // try merging other presentation files
  }
} 
// re-order the slides to the actual position                                        
foreach (KeyValuePair<int, int> reOrderValue in reOrderPair)
{
    try
    {
        this.ReorderSlides(destinationFileName, reOrderValue.Key, reOrderValue.Value);                    
    }
    catch
    {
        continue;
    } 
}

The caller would need to create the list of PresentationInfo and call MergeAllSlides.

string ppt1 = "ppt1.pptx";
string ppt2 = "ppt2.pptx";
new MergeHandler().MergeAllSlides(ppt1, ppt1, new List<PresentationInfo>()
{
    new PresentationInfo()
    {
        File = ppt2,
        InsertPosition = 2
    }
});

Using the above code, the slides would be inserted in second position.

Point Of Interest

  1. As you have observed, during the copy of a slide, the referred master and layout slide is also copied each time. This results in increased file size and additional time overhead. So, the code can further be re-factored to copy master and layout slides only once.
  2. I have taken an approach where all slides from ppt2.pptx are copied into ppt1.pptx. This can further be refined to copy only the selected files from ppt2.pptx.
  3. To copy multiple presentation files into one file, all we need to do is create multiple PresentationInfo in the list.
  4. new MergeHandler().MergeAllSlides(ppt1, ppt1, new List<PresentationInfo>()
    {
        new PresentationInfo()
        {
            File = ppt2,
            InsertPosition = 2
        },
        new PresentationInfo()
        {
            File = @"c:\wip\ppt3",
            InsertPosition = 3
        },
        new PresentationInfo()
        {
            File = @"c:\wip\ppt4",
            InsertPosition = 6
        },
        new PresentationInfo()
        {
            File = @"c:\wip\ppt5",
            InsertPosition = 1
        }
       // you can copy as many presentations as required
    });

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

pramodhegde88
Software Developer
India India
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionExample using a text file that has list of pptx files?membermarkyodo26-Feb-13 4:51 
I am 3 weeks new to C# and I was wondering if you could provide an example of passing the file names to the slide merge using file names contained in a text file.
 
I was trying to do this with the StreamReader but I am stuck on what is needed for the loop.
 
            // Use Path class to manipulate file and directory paths.
            destFile = Path.Combine(targetPath, destFile); 
            // Grabs the .txt file in this directory.
            string[] files = Directory.GetFiles(destFile, "*.txt");
                foreach (string s in files)
                    {
                        // Reads each line of the .txt file as a file name.
                        StreamReader fileName = new StreamReader(s) ;
                            while ((line = fileName.ReadLine()) != null)
                            {
                                // new PresentationInfo()
                                //    {
                                //        File = line,
                                //        InsertPosition = 2
                                //    } 
                            }
                     }

AnswerRe: Example using a text file that has list of pptx files?membermarkyodo4-Mar-13 6:56 
Spent a few days on this but I think I figured it out. If anyone has a better way please let me know.
My final version contains variables containing the paths to the files so the program can pull files from any directory I assign in my settings.
 
Each of my PowerPoint presentations contains only 1 slide.
 
// Assign the string for file and counter
string l0 = "Title_Slide.pptx";
string l1;
short counterX = 1;
 
// Grab the .txt file containing the individual file names
List<string> list = new List<string>();
using (StreamReader reader = new StreamReader("List_of_PPTX_Files.txt"))
{
    // loop through each file name and merge it with target presentation
    while ((l1 = reader.ReadLine()) != null)
    {
        string l2;
        l2 = l1;
        list.Add(l2); // Add to list.
        new MergeHandler().MergeAllSlides(l0, l0, new List<PresentationInfo>()
            {
                new PresentationInfo()
                {
                    File = l2,
                    InsertPosition = counterX++
                }
            });
    }
 
}\

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130617.1 | Last Updated 19 Jul 2012
Article Copyright 2012 by pramodhegde88
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid