Adding Text-to-Speech to Your Document Viewer


Text-to-speech (also known as speech synthesis) is the artificial production of human speech. It is most commonly used to allow people with visual impairments or reading disabilities to listen to written words. Thanks to the Speech Synthesis API, which is natively supported in modern browsers, text-to-speech functionality can easily be integrated into a web application. In this post, I will outline how to add it to the PrizmDoc Viewer with a few simple steps.


1. Add a Listen Menu

Add the code below to viewer-assets/templates/viewerTemplate.html to add a "Listen" menu to the viewer containing a play, pause, and stop button. You can add this code after the div containing "data-pcc-nav-tab=”esign”" to show the "Listen" tab to the right of the existing "E-Sign" tab.

To show an icon on the new "Listen" tab, create a new file (named listen.svg) in the viewer-assets/icons/svg folder, and add the SVG below to the file. This icon is referenced as pcc-icon-listen in the HTML template code above.

The HTML template code above also references some text. All viewer text is stored in the language file, viewer-assets/languages/en-US.json. You will need to add the following new terms to the language file.

You will need to build the viewer after making the above changes, and copy the output dist/viewer-assets files over the viewer-assets files of your web page. Steps for building the viewer are in the README file of each installed PrizmDoc Viewer sample. In summary, you will need to run "npm install" and then run "gulp build". You will need to copy all of the files in the viewer-assets folder except Gulpfile.js and package.json to a "src" folder in the viewer-assets folder first.


2. Reference the UI and Enable in Supported Browsers

The code in this step and the following steps can be added to the function where you are calling the pccViewer method to create the viewer. For example, in the webforms sample you would add it to the embedViewer function in Default.aspx.

Add variables to reference the stop, pause, and play buttons. Enable the play button only if the browser supports Speech Synthesis. The Speech Synthesis API is natively supported by the latest versions of Chrome, Firefox, Edge, and Safari, but is not supported in Internet Explorer 11 and many mobile browsers.


3. Implement Speaking

Add the play button click event handler below. Clicking the play button will begin playing all of the pages starting at the current page, or resume if playing had already started and was paused.

Add the play and stop functions below. The play function requests the text of a page and uses it to create a SpeechSynthesisUtterance. The speechSynthesis.speak method is called to initiate the speaking. When completed, the onend event handler is used to play the next page, unless the last page has been played. In that case, the stop function is called to reset the state to allow playing again.


4. Implement Pausing

The speechSynthesis.pause method pauses the speech. Add the following pause button click event handler after the previously added play button click event handler.


5. Implement Stopping

To implement stopping of the speech, add the following stop button click event handler after the previously added Pause button click event handler.

Once you’ve followed these steps to integrate text-to-speech functionality into your PrizmDoc Viewer, your users can start listening to documents with the click of a button. For more information on what you can do with PrizmDoc Viewer, visit the documentation.

Jared Jacoby Jared Jacoby, Software Engineer, PrizmDoc

Jared Jacoby has worked at Accusoft since 2005. Starting as a support engineer and promoted to a software engineer a couple years later, Jared has worked on numerous Accusoft products including the ImagXpress family, ImageGear, and PrizmDoc. He graduated from the University of Florida in 2004 with a degree in Computer Science. In his spare time, Jared enjoys playing board games, music, and writing.


Related posts

visual testing
5 Visual Testing Best Practices
Read More >
Integrating PrizmDoc Editor and Using a Workflow in Your Application
Integrating PrizmDoc Editor and Using a Workflow in Your Application
Read More >
What’s Pushing the Python Preference?
Snake Charmer: What’s Pushing the Python Preference?
Read More >

Join the discussion.