Use for Text to Speech

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. To use the example found here you will need the Freetts jars found at the link above.

Creating a speech class.

In order to create a speech engine, you need to implement the interface 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 if 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 method returned value 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 extracted 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. When adding the functionality you should add the Speech class after the Viewer is created as shown below.

Speech mySpeechImp;
//Initialize your Speech implementation here

Viewer viewer = new Viewer();
PdfDecoderInt pdfDecoder = viewer.getPdfDecoder();
pdfDecoder.addExternalHandler(mySpeechImp, Options.SpeechEngine);

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

Example Speech implementation using Freetts

As the original functionality was built on Freetts we are providing here a basic implementation of the Speech interface that is designed to use the Freetts library. Just copy the following into its own class, add it to the viewer as described above and ensure the Freetts jars are on the classpath.


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.

Still need help? Send us your questions.