Set SmartArt Styles and Node Hyperlinks with JavaScript

SmartArt turns a process, a hierarchy or a set of parallel items into one clear diagram, and it is a common way to explain an idea on a slide. Once the diagram is drawn, its colors and depth often have to match the style of the whole deck; when readers should be able to jump from the diagram to a detail page or an external page, its nodes also need to become clickable entry points. Adjusting styles page by page and adding links node by node is slow, and with a longer deck it is easy to miss one and leave the pages looking inconsistent.

This article uses Spire.Presentation for JavaScript to set PowerPoint SmartArt styles and add node hyperlinks 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.


Set SmartArt styles

The look of a SmartArt diagram is defined by two settings: the color style decides how the nodes are colored, and the shape style decides their fill, outline and depth. Default settings usually rely on a single accent color and a flat effect, which looks thin next to a set of matching slides; a richer palette and a style with some depth make the levels stand out and echo the rest of the deck. Both settings are supplied as enumerations, so there is no need to fill in colors or effects node by node — one assignment covers the whole diagram.

Syntax Result
shape instanceof pptModule.ISmartArt Checks whether the shape is a SmartArt diagram
smartArt.ColorStyle Reads or sets the color style of the SmartArt
SmartArtColorType.ColorfulAccentColors Sets the color style to the colorful accent scheme
smartArt.Style Reads or sets the shape style of the SmartArt
SmartArtStyleType.BrickScene Sets the shape style to the brick scene effect

The steps are:

  1. Get the Spire.Presentation WASM module and confirm that it is ready, load the font and the presentation into the VFS, then create a Presentation object and load the file.
  2. Use Presentation.Slides.get_Item() to get the first slide and loop through its shapes up to Shapes.Count.
  3. Use shape instanceof pptModule.ISmartArt to tell whether the current shape is a SmartArt diagram, then set its ColorStyle to SmartArtColorType.ColorfulAccentColors.
  4. Set the same diagram's Style to SmartArtStyleType.BrickScene, which updates the color and the effect together.
  5. Call Presentation.SaveToFile() to save the result file and Dispose() to release the presentation, then read the file from the VFS and trigger the download.

The code for setting SmartArt styles is as follows:

function App() {
  const setSmartArtStyle = 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 the VFS
    await window.spire.FetchFileToVFS('ARIAL.TTF', '/Library/Fonts/', `${process.env.PUBLIC_URL}/font/`);
    const inputFileName = 'en_SmartArtDeck.pptx';
    await window.spire.FetchFileToVFS(inputFileName, '', `${process.env.PUBLIC_URL}/data/`);

    // Load the presentation and get the first slide
    const ppt = new pptModule.Presentation();
    ppt.LoadFromFile(inputFileName);
    const slide = ppt.Slides.get_Item(0);

    // Loop through the shapes on the slide and change the SmartArt styles
    for (let i = 0; i < slide.Shapes.Count; i++) {
      const shape = slide.Shapes.get_Item(i);
      if (shape instanceof pptModule.ISmartArt) {
        shape.ColorStyle = pptModule.SmartArtColorType.ColorfulAccentColors;
        shape.Style = pptModule.SmartArtStyleType.BrickScene;
      }
    }

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

    // Read the saved file from the VFS and trigger the download
    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>Set SmartArt Style and Node Hyperlinks</h1>
      <button onClick={setSmartArtStyle}>Set SmartArt style</button>
    </div>
  );
}

export default App;

Presentation after setting the SmartArt style:

Presentation after setting the SmartArt style


Add hyperlinks to SmartArt nodes

A SmartArt node hyperlink can point to another slide inside the same presentation or to an external web address. The nodes of a process diagram usually correspond to detail pages that follow, which suits internal jumps; when product or proposal material lives on a web page, a URL link guides readers there. Turning a node into a hyperlink lets the presenter open a detail directly from the overview page instead of paging through the deck, and lets readers reach the link without searching for it in a long document. The link is stored on the node rather than on the slide, so moving or resizing the diagram does not affect the jump.

Syntax Result
smartArt.Nodes Gets the node collection of the SmartArt
smartArt.Nodes.get_Item(index) Gets one node by index, starting from 0
node.Click Sets or reads the hyperlink triggered when the node is clicked
new pptModule.ClickHyperlink(link) Creates a hyperlink

The steps are:

  1. Load the font and the presentation into the VFS, load the presentation, then get the first slide and the SmartArt diagram on it.
  2. Loop over smartArt.Nodes and take each node with Nodes.get_Item().
  3. For an internal jump, pass the target slide returned by Presentation.Slides.get_Item() to new pptModule.ClickHyperlink(); for an external jump, pass a URL string instead. Then assign the result to the node's Click property.
  4. Call Presentation.SaveToFile() to save the result file and Dispose() to release the presentation, then read the file from the VFS and trigger the download.

The code for adding hyperlinks to SmartArt nodes is as follows (the example links each node to an internal slide):

function App() {
  const addNodeHyperlink = 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 the VFS
    await window.spire.FetchFileToVFS('ARIAL.TTF', '/Library/Fonts/', `${process.env.PUBLIC_URL}/font/`);
    const inputFileName = 'en_SmartArtDeck.pptx';
    await window.spire.FetchFileToVFS(inputFileName, '', `${process.env.PUBLIC_URL}/data/`);

    // Load the presentation and get the first slide
    const ppt = new pptModule.Presentation();
    ppt.LoadFromFile(inputFileName);
    const slide = ppt.Slides.get_Item(0);

    // Find the SmartArt shape on the slide
    let smartArt = null;
    for (let i = 0; i < slide.Shapes.Count; i++) {
      const shape = slide.Shapes.get_Item(i);
      if (shape instanceof pptModule.ISmartArt) {
        smartArt = shape;
      }
    }

    // Link each node to the slide that follows it
    for (let i = 0; i < smartArt.Nodes.Count; i++) {
      const node = smartArt.Nodes.get_Item(i);
      node.Click = new pptModule.ClickHyperlink(ppt.Slides.get_Item(i + 1));
    }

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

    // Read the saved file from the VFS and trigger the download
    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>Set SmartArt Style and Node Hyperlinks</h1>
      <button onClick={addNodeHyperlink}>Add node hyperlinks</button>
    </div>
  );
}

export default App;

Presentation after adding hyperlinks to SmartArt nodes:

Presentation after adding hyperlinks to SmartArt nodes


FAQ

How to tell whether a shape on a slide is SmartArt

Cause: Regular shapes and SmartArt diagrams both appear in the slide's shape collection. Setting Style or Nodes without telling them apart leaves the other shapes unable to respond.

Solution: Use instanceof against ISmartArt and continue only for SmartArt diagrams:

const shape = slide.Shapes.get_Item(i);
if (shape instanceof pptModule.ISmartArt) {
  console.log(shape.Nodes.Count);
}

How to tell whether a node already has a hyperlink

Cause: node.Click still returns an object for a node without a link, so testing the object itself does not tell whether a link has been set; the action type has to be read instead.

Solution: Read node.Click.ActionType. A slide link returns GotoSlide and a URL link returns Hyperlink; reading it throws when the node has no hyperlink, so wrap the call in try/catch:

try {
  console.log(node.Click.ActionType); // GotoSlide or Hyperlink
} catch {
  console.log('No hyperlink on this node');
}

Can child nodes of a multi-level SmartArt have hyperlinks

Cause: Nodes of a multi-level diagram such as an organization chart sit at different levels, so it is worth confirming that child nodes accept links just like the top-level nodes.

Solution: Yes. Top-level nodes come from smartArt.Nodes, while the direct children of a node are reached through node.ChildNodes; links are set the same way, with either a slide or a URL:

const root = smartArt.Nodes.get_Item(0);
const child = root.ChildNodes.get_Item(0);
child.Click = new pptModule.ClickHyperlink(ppt.Slides.get_Item(1));

smartArt.Nodes is a flat collection that lists nodes of every level in document order, while ChildNodes only lists the direct children of the current node.


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.