Copy PowerPoint Shapes Between Slides with JavaScript

The same group of graphics often has to reappear on several slides: a process diagram, a legend, a decorative band that keeps a consistent style from page to page. Copying them by hand works for a page or two, but once the deck grows it is easy to leave one out, and moving a shape onto a page with a different layout usually means realigning its position and size again. Copying shapes with Spire.Presentation for JavaScript while the deck is generated keeps the same group of graphics consistent across pages and saves the repeated adjustment.

This article uses Spire.Presentation for JavaScript to copy shapes between PowerPoint slides in the browser. The whole process runs locally on the user's device through WebAssembly, requires no Office installation and no back-end service, and keeps the presentation from leaving the browser.

This article covers two features:

For installation and project configuration, see Integrating Spire.Presentation for JavaScript in a React Project. The examples below assume that Spire.Presentation for JavaScript is installed and the WebAssembly module has been initialized.


Copy a specific shape to another slide

Reusing a graphic that is already laid out on another page is common when building a deck: the diagram introduced on an earlier slide is still needed later, and the sample shape on a template page has to appear on a content page as well. Copying a single shape takes only two values: where it sits in the source slide, and the target slide it should join. Its fill, outline and text travel with it, while the source slide stays as it was.

Writing Result
Presentation.Slides.get_Item(index) Returns a slide of the presentation by index; indexing starts at 0
slide.Shapes Returns all shapes of the slide
slide.Shapes.get_Item(index) Returns one shape of the slide by index
slide.Shapes.Count Returns the number of shapes on the slide
targetSlide.Shapes.AddShape(sourceShape) Copies the source shape to the target slide, keeping its position and size

The steps are:

  1. Get the Spire.Presentation WASM module and make sure it is ready, load the font and the presentation into the VFS, then create a Presentation object and load the file.
  2. Get the source slide and the target slide through Presentation.Slides.get_Item().
  3. Take the shape to copy with the source slide's Shapes.get_Item() and hand it to the target slide's Shapes.AddShape().
  4. Call Presentation.SaveToFile() to save the result file, release the presentation with Dispose(), then read the file from the VFS and trigger the download.

The code for copying a specific shape to another slide is as follows:

function App() {
  const copyShape = async () => {
    // Get the Spire.Presentation WASM module
    const pptModule = window.wasmModule?.spirepresentation;

    // Check if the module is ready
    if (!pptModule) {
      alert('Spire.Presentation is not ready yet');
      return;
    }

    // Load the font and the presentation into VFS
    await window.spire.FetchFileToVFS('ARIAL.TTF', '/Library/Fonts/', `${process.env.PUBLIC_URL}/font/`);
    const inputFileName = 'en_ShapeDeck.pptx';
    await window.spire.FetchFileToVFS(inputFileName, '', `${process.env.PUBLIC_URL}/data/`);

    // Load the presentation
    const ppt = new pptModule.Presentation();
    ppt.LoadFromFile(inputFileName);

    // Specify the source slide and the target slide
    const sourceSlide = ppt.Slides.get_Item(0);
    const targetSlide = ppt.Slides.get_Item(1);

    // Copy the second shape of the source slide to the target slide
    targetSlide.Shapes.AddShape(sourceSlide.Shapes.get_Item(1));

    // Save the result file
    const outputFileName = 'CopyShape_result.pptx';
    ppt.SaveToFile({ file: outputFileName, fileFormat: pptModule.FileFormat.Pptx2013 });
    ppt.Dispose();

    // Read the generated file from VFS and download it
    const fileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
    const blob = new Blob([fileArray], { type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation' });
    const url = URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = outputFileName;
    a.click();
    URL.revokeObjectURL(url);
  };

  return (
    <div style={{ textAlign: 'center', height: '300px' }}>
      <h1>Copy Shapes Between Slides</h1>
      <button onClick={copyShape}>Copy a shape</button>
    </div>
  );
}

export default App;

The presentation after copying a specific shape to another slide:

Presentation after copying a specific shape to another slide


Copy all shapes of one slide to another slide

A slide usually holds a title, some text blocks and several shapes, and copying them one by one is both tedious and easy to get wrong. With the shape collection, every shape of the source slide can be taken by index and copied to the target slide in turn, so the target page receives the same set of content. When the target is a blank page, it can serve directly as a complete content page once the copy finishes.

The steps are:

  1. Load the font and the presentation into the VFS, then get the source slide and the target slide from the loaded presentation.
  2. Loop up to Shapes.Count and take each shape of the source slide with Shapes.get_Item().
  3. Hand each shape to the target slide's Shapes.AddShape() inside the loop to finish the whole-slide copy.
  4. Call Presentation.SaveToFile() to save the result file, release the presentation with Dispose(), then read the file from the VFS and trigger the download.

The code for copying all shapes of one slide to another slide is as follows:

function App() {
  const copyAllShapes = async () => {
    // Get the Spire.Presentation WASM module
    const pptModule = window.wasmModule?.spirepresentation;

    // Check if the module is ready
    if (!pptModule) {
      alert('Spire.Presentation is not ready yet');
      return;
    }

    // Load the font and the presentation into VFS
    await window.spire.FetchFileToVFS('ARIAL.TTF', '/Library/Fonts/', `${process.env.PUBLIC_URL}/font/`);
    const inputFileName = 'en_ShapeDeck.pptx';
    await window.spire.FetchFileToVFS(inputFileName, '', `${process.env.PUBLIC_URL}/data/`);

    // Load the presentation
    const ppt = new pptModule.Presentation();
    ppt.LoadFromFile(inputFileName);

    // Specify the source slide and the blank target slide
    const sourceSlide = ppt.Slides.get_Item(0);
    const targetSlide = ppt.Slides.get_Item(2);

    // Take each shape of the source slide and copy it to the target slide
    for (let i = 0; i < sourceSlide.Shapes.Count; i++) {
      targetSlide.Shapes.AddShape(sourceSlide.Shapes.get_Item(i));
    }

    // Save the result file
    const outputFileName = 'CopyAllShapes_result.pptx';
    ppt.SaveToFile({ file: outputFileName, fileFormat: pptModule.FileFormat.Pptx2013 });
    ppt.Dispose();

    // Read the generated file from VFS and download it
    const fileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
    const blob = new Blob([fileArray], { type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation' });
    const url = URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = outputFileName;
    a.click();
    URL.revokeObjectURL(url);
  };

  return (
    <div style={{ textAlign: 'center', height: '300px' }}>
      <h1>Copy All Shapes</h1>
      <button onClick={copyAllShapes}>Copy all shapes</button>
    </div>
  );
}

export default App;

The presentation after copying all shapes of one slide to another slide:

Presentation after copying all shapes of one slide to another slide


FAQ

Does a copied shape keep its original fill and text formatting

Cause: Besides the outline and position, the text inside the shape, its fill color and font size also need to survive the copy so the source page's layout can be reused as is.

Solution: AddShape() copies the shape itself, so fill, outline, text and font settings all come along. Get the shape back after copying and read Fill and TextFrame to confirm:

const copiedShape = targetSlide.Shapes.get_Item(targetSlide.Shapes.Count - 1);
console.log(copiedShape.Fill.SolidColor.Color.ToString());
console.log(copiedShape.TextFrame.Text, copiedShape.TextFrame.TextRange.FontHeight);

How to adjust the position and size of a copied shape

Cause: A copied shape keeps the coordinates it had on the source slide, so it lands in the same spot on the target page. When the target page is laid out differently, or the graphic should sit elsewhere, it has to be adjusted after the copy.

Solution: Get the shape back through the last index of the collection, then set Left, Top, Width and Height:

const copiedShape = targetSlide.Shapes.get_Item(targetSlide.Shapes.Count - 1);
copiedShape.Left = 470;
copiedShape.Top = 170;
copiedShape.Width = 160;
copiedShape.Height = 100;

How to copy a shape to another presentation

Cause: Graphics tend to accumulate in template files and earlier projects, so redrawing them for a new deck is expensive.

Solution: The shape object passed in is not bound to a particular presentation. Load the source presentation and the target presentation, hand the source shape to a slide of the target, and save the target:

const sourcePpt = new pptModule.Presentation();
sourcePpt.LoadFromFile('source.pptx');
const targetPpt = new pptModule.Presentation();
targetPpt.Slides.get_Item(0).Shapes.AddShape(sourcePpt.Slides.get_Item(0).Shapes.get_Item(0));
targetPpt.SaveToFile({ file: 'output.pptx', fileFormat: pptModule.FileFormat.Pptx2013 });

Get a Free License

If you wish to delete the evaluation message from the resulting documents, or to get rid of function limitations, please contact sales to obtain a valid 30-day temporary license.