WebViewer getTemplateKeys return 0

WebViewer Version: 8.1.0

I encountered issues when trying to use the getTemplateKeys() as it always return 0 in length.

This is the content of my test.docx file, with the Template Keys defined:

Name of Patient: {{name}}
NRIC: {{NRIC}}
Gender: {{gender}}

Below is the code I used (Modified based on the react sample code)

import React, { useRef, useEffect } from 'react';
import WebViewer from '@pdftron/webviewer';
import './App.css';

const App = () => {
  const viewer = useRef(null);

  // if using a class, equivalent of componentDidMount 
  useEffect(() => {
    WebViewer(
      {
        path: '/webviewer/lib',
        initialDoc: '/files/test.docx',
      },
      viewer.current,
    ).then((instance) => {
      const docViewer = instance.Core.documentViewer;
      const annotManager = instance.Core.annotationManager;
      // call methods from instance, docViewer and annotManager as needed

      // you can also access major namespaces from the instance as follows:
      // const Tools = instance.Core.Tools;
      // const Annotations = instance.Core.Annotations;
      
      docViewer.addEventListener('documentLoaded', () => {
        // call methods relating to the loaded document
        const doc = docViewer.getDocument();
        const keys = doc.getTemplateKeys();
        console.log(keys)

        // it is possible to extract document template keys in WebViewer
      });
    });
  }, []);

  return (
    <div className="App">
      <div className="header">React sample</div>
      <div className="webviewer" ref={viewer}></div>
    </div>
  );
};

export default App;

Hello, I’m Ron, an automated tech support bot :robot:

While you wait for one of our customer support representatives to get back to you, please check out some of these documentation pages:

Guides:APIs:Forums:

Thanks for the report. I took a closer look and getTemplateKeys is actually supposed to return a promise (though it isn’t right now).

For now if you want it to work you need to wait for the documentCompletePromise to resolve before calling getTemplateKeys.

await doc.documentCompletePromise();
const keys = doc.getTemplateKeys();

Since this is a bug we will be fixing this to return a promise soon and I’ll let you know when there’s a build you can try out with that update.

This fix will be in tomorrow’s (September 16) nightly stable 8.1 build. See here for how you can download it https://www.pdftron.com/documentation/web/faq/webviewer-nightly-build/

Hi Matt

Thank you for your help. The workaround works for now. Will try the build once it is ready.