Text to Speech Tutorial

JPedal has the ability to speak text that has been highlighted, until recently this was handled within JPedal by Freetts. As of JPedal version 5.36 we have moved this functionality to allow you to use any speech library you wish and have it work with the existing JPedal functionality. This is done by creating your own Speech implementation or by using the example provided on this page.


Creating a speech class.

In order to create a speech engine you need to implement the interface org.jpedal.io.Speech. This interface has the following four methods


public void setVoice(String voiceName);

This method allows JPedal to set which voice should be used in the text to speech. This method should allow for incorrect names being passed in and use a default if the name is not in your voice list.


public boolean speechAvailible();

This method determines of the class is currently capable of speaking any text. This can be used to ensure any required jars are present.


public String[] listVoices();

This method returns a list of voices that can be used to speak selected text. Any attempts to change voice should check this methods returned array to ensure it is present. This method is also used in the preferences window to display the possible voices to allow the user a choice.


public void speakText(final String text);

This method performs the actual speaking of the text. The text passed in is the extract text highlighted on the page.


Adding the speech implementation

To use your speech implementation in the JPedal example viewer you will need to add it in in the following way.

After you have an instance of Viewer you will need to gain access to the instance of PdfDecoder that the viewer is using. This can be done as follows.




Now in PdfDecoder you add your speech implementation as an external handler of the SpeechEngine type.

PdfDecoder.addExternalHandler(new yourSpeechImp(), Options.SpeechEngine);


After this, so long as your speech implementation speechAvailible() method is returning true, you will be able to use the functionality.


Example using Freetts

As our original functionality was built on Freetts we are providing here a basic example using the Freetts library.

import org.jpedal.io.Speech;

public class SpeechPackage implements Speech{

    public String selectedVoice = "kevin16";

    public boolean speechAvailible(){
        final String classpath = System.getProperty("java.class.path");
        return classpath.contains("freetts");

    public String[] listVoices() {
        final com.sun.speech.freetts.VoiceManager voiceManager = com.sun.speech.freetts.VoiceManager.getInstance();
        final com.sun.speech.freetts.Voice[] voices = voiceManager.getVoices();
        final String[] voiceNames = new String[voices.length];
        for (int i = 0; i < voices.length; i++) {
            voiceNames[i] = voices[i].getName()+ '(' +voices[i].getDomain()+" domain)";
        return voiceNames;

    public void speakText(final String text)
            * Create voice
            final com.sun.speech.freetts.VoiceManager voiceManager = com.sun.speech.freetts.VoiceManager.getInstance();
            final com.sun.speech.freetts.Voice helloVoice = voiceManager.getVoice(selectedVoice);
            if (helloVoice == null) {
                System.err.println("Cannot find voice kevin16.\nExiting.");
            } else {
    public void setVoice(String voiceName) {
        selectedVoice = voiceName;

Accessibility Options for Text to Speech

This tutorial explains how you can use various options to make the viewer more accessible to those with more specific needs, you can find it here.