Jacob Collins Jacob is the JPedal Product Lead and specialises in PDF creation and manipulation. He also develops Salesforce backend systems and contributes to marketing and support. Outside work, he’s a 1900‑rated chess player, guitarist, and French learner.

How to add AI to the JPedal Viewer (Tutorial)

1 min read

JPedal AI chat

The JPedal Viewer is the best Java PDF Viewer because it so easily customizable.

JPedal provides access to the underlying Swing components which allows you to extend it with custom behaviour and additional UI elements.

In this tutorial, I am going to show you how to add a chat window to the Viewer which allows users to ask a large language model questions about the document they have open.

Download a JPedal trial jar to try this yourself!

First we need to make a chat window that can be embedded in the Viewer. I won’t write the complete source code here because it is a couple hundred lines long, but you can find the full class here.

The important part is that we need some kind of callback to receive the messages the user sends.

Now we can add this to JPedal by getting a handle to the JTabbedPane which represents the side bar in the Viewer.

AI chat window

The chat window works, but it does not do anything yet so we need to add the AI capabilities.

Again, I won’t give the full code here (you can see it on our GitHub), but essentially we can use the callback from the chat window and pass that to the AI, along with the context from the PDF.

In this example, I show how to provide the text extracted from the document, and also an image of the page which some AIs may be able to use for OCR.

Here is the final result!

JPedal AI chat

Resources

You can find all the source code on our GitHub page.

Find out what else you can do with JPedal.

We can help you better understand the PDF format as developers who have been working with the format for more than 2 decades!



The JPedal PDF library allows you to solve these problems in Java


Jacob Collins Jacob is the JPedal Product Lead and specialises in PDF creation and manipulation. He also develops Salesforce backend systems and contributes to marketing and support. Outside work, he’s a 1900‑rated chess player, guitarist, and French learner.