Set Row Height and Column Width of PowerPoint Tables in React

Tables in a slide deck often exist to make information comparable: a header row sits tighter while data rows get more room to breathe, a text column such as a store name needs to be wider, and numeric columns such as quantities or amounts can be narrower. When row heights and column widths are all identical, the text feels cramped in one place and the table looks empty in another; dragging each row and column by hand cannot hit exact values and has to be redone for the next table.

This article uses Spire.Presentation for JavaScript to set the row heights and column widths of PowerPoint tables 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 the row height of a PowerPoint table

The rows of a table do not all play the same role: a header row should stand out, data rows should stay even, and a total row may need more breathing room. With one height for the whole table, it is hard to tell important rows from ordinary ones; dragging by hand is a matter of eyeballing and rarely gives a repeatable size. Setting heights row by row fixes the height distribution of the table, so the same proportions can be reused for different content.

Writing Result
table.TableRows.Count Gets the number of rows in the table
table.TableRows.get_Item(row).Height Gets or sets the height of the specified row

The steps are:

  1. Get the Spire.Presentation WASM module and check that it is ready, load the font and the presentation into the VFS, create a Presentation object and load it with LoadFromFile(), then take the first slide with Slides.get_Item(0).
  2. Walk the shape collection up to Shapes.Count and find the table with shape instanceof pptModule.ITable.
  3. Loop over the rows up to TableRows.Count and set table.TableRows.get_Item(row).Height to the intended value.
  4. Call Presentation.SaveToFile() to save the result file, release the presentation with Dispose(), then read the file from the VFS and start the download.

The code to set the row height of a PowerPoint table is as follows:

function App() {
  // set the row heights of a PowerPoint table
  const setRowHeights = async () => {
    // get the Spire.Presentation WASM module
    const pptModule = window.wasmModule?.spirepresentation;

    // check that 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_TableSize.pptx';
    await window.spire.FetchFileToVFS(inputFileName, '', `${process.env.PUBLIC_URL}/data/`);

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

    // walk the shape collection and find the table by type
    let table = null;
    for (let i = 0; i < slide.Shapes.Count; i++) {
      const shape = slide.Shapes.get_Item(i);
      if (shape instanceof pptModule.ITable) {
        table = shape;
        break;
      }
    }

    // set the height of each row in order
    const rowHeights = [100, 85, 70, 55, 40];
    for (let row = 0; row < table.TableRows.Count; row++) {
      table.TableRows.get_Item(row).Height = rowHeights[row];
    }

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

    // read the generated file from the VFS and start the download
    downloadFile(outputFileName);
  };

  // read the generated file from the VFS and start the download
  const downloadFile = (outputFileName) => {
    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>Table Row Height and Column Width in PowerPoint</h1>
      <button onClick={setRowHeights}>Set row heights</button>
    </div>
  );
}

export default App;

Presentation after setting the table row heights:

Presentation after setting the table row heights


Set the column width of a PowerPoint table

Column width decides how much text a column can hold. A text column that is too narrow wraps, a numeric column that is too wide pushes the others to the edge, and equal widths for every column rarely suit content of very different lengths. With a width per column, text columns and numeric columns each get what they need, and the horizontal proportions of the table stay stable.

Writing Result
table.ColumnsList.Count Gets the number of columns in the table
table.ColumnsList.get_Item(col).Width Gets or sets the width of the specified column

The steps are:

  1. Get the Spire.Presentation WASM module and check that it is ready, load the font and the presentation into the VFS, create a Presentation object and load it with LoadFromFile(), then take the first slide with Slides.get_Item(0).
  2. Walk the shape collection up to Shapes.Count and find the table with shape instanceof pptModule.ITable.
  3. Loop over the columns up to ColumnsList.Count and set table.ColumnsList.get_Item(col).Width to the intended value.
  4. Call Presentation.SaveToFile() to save the result file, release the presentation with Dispose(), then read the file from the VFS and start the download.

The code to set the column width of a PowerPoint table is as follows:

function App() {
  // set the column widths of a PowerPoint table
  const setColumnWidths = async () => {
    // get the Spire.Presentation WASM module
    const pptModule = window.wasmModule?.spirepresentation;

    // check that 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_TableSize.pptx';
    await window.spire.FetchFileToVFS(inputFileName, '', `${process.env.PUBLIC_URL}/data/`);

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

    // walk the shape collection and find the table by type
    let table = null;
    for (let i = 0; i < slide.Shapes.Count; i++) {
      const shape = slide.Shapes.get_Item(i);
      if (shape instanceof pptModule.ITable) {
        table = shape;
        break;
      }
    }

    // set the width of each column in order
    const columnWidths = [80, 130, 170, 220];
    for (let col = 0; col < table.ColumnsList.Count; col++) {
      table.ColumnsList.get_Item(col).Width = columnWidths[col];
    }

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

    // read the generated file from the VFS and start the download
    downloadFile(outputFileName);
  };

  // read the generated file from the VFS and start the download
  const downloadFile = (outputFileName) => {
    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>Table Row Height and Column Width in PowerPoint</h1>
      <button onClick={setColumnWidths}>Set column widths</button>
    </div>
  );
}

export default App;

Presentation after setting the table column widths:

Presentation after setting the table column widths


FAQ

How do I auto-fit table column widths to the text

Reason: Column contents differ in length. With fixed widths, text columns tend to wrap while numeric columns leave large blank areas, and trying a value per column is slow and hard to reproduce.

Solution: Call AdjustColumnByTextWidth() on each column; it recomputes the width from that column's longest text:

for (let col = 0; col < table.ColumnsList.Count; col++) {
  table.ColumnsList.get_Item(col).AdjustColumnByTextWidth();
}

Why can't I find the table when walking the shape collection

Reason: Only an editable table object is an ITable. If the table on the slide was inserted as a screenshot, picture, or embedded object, it is an ordinary picture or OLE object, and shape instanceof pptModule.ITable will never be true.

Solution: Open the deck in PowerPoint and check that the table's cells can be edited. If it is a picture, insert it again as a table, or convert it in the source document before processing.


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.