How do I extract templates embedded in a PDF using WebViewer?

Using the FullAPI, it is possible to extract PDF templates embedded within a PDF. Once this information is extracted, it can be appended to the document, or downloaded separately. The following sample extracts the templates, appends them to the document, and downloads it using FileSaver. It assumes the current document has templates embedded within it.

    const func = async() => {
      const pdfDoc = await  docViewer.getDocument().getPDFDoc();
      const nameTree = await PDFNet.NameTree.find(pdfDoc, 'Templates');
      if (nameTree && await nameTree.isValid()) {
        const iterator = await nameTree.getIteratorBegin();
        for (var counter = 0; await iterator.hasNext(); await iterator.next(), ++counter) {
          const entry_name = await iterator.key().then(key => key.getAsPDFText());
          console.log("Name: " + entry_name);
          const templateObj = await iterator.value();
          templateObj.putName("Type", "Page");
          const page = await PDFNet.Page.create(templateObj);
          pdfDoc.pagePushBack(page); 
        }

        const buf = await pdfDoc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_incremental);
        const blob = new Blob([buf], { type: 'application/pdf' });
        saveAs(blob, 'my_file_with_template.pdf')
      }
    }
    PDFNet.runWithCleanup(func);