If you want to view PDF files in your Java application you will quickly find that Java has no native support for the PDF file format. It is for this reason that you would need a Java PDF viewer.
Today I’m going to walk you through the steps required using our product JPedal, which has over 20 years of development behind it!
Installing the Java PDF viewer
There are a few ways you can install JPedal, you can either download the trial JAR from our website, or you can add JPedal to your project dependencies.
In this tutorial, I am going to be using Maven however you can also use Gradle.
<repositories> <repository> <id>IDRsolutions</id> <name>IDR Solutions</name> <url>https://maven.idrsolutions.com</url> </repository> </repositories> <dependencies> <dependency> <groupId>com.idrsolutions</groupId> <artifactId>jpedal-trial</artifactId> <version>2022.10</version> </dependency> </dependencies>
You can find the latest version number of JPedal on our support site.
Running the standalone viewer
If you want to quickly view PDF files, you may use the JPedal Viewer. To launch the viewer you can double-click to open the JAR or you can run the following:
java -jar jpedal.jar --view [optional/filename.pdf]
Embedding in an existing Java application
Creating an instance of the JPedal Viewer only requires two lines of code:
Viewer viewer = new Viewer(); viewer.setupViewer();
You can then use the following to open and display a PDF file:
viewer.executeCommand(Commands.OPENFILE, new Object[] {"pdfFile.pdf"});
Advanced usage
You may want to customise the look and feel of the viewer, for example you might not want customers to be able to print your PDFs, or you might want to hide the menu buttons.
All of this and more is possible using a properties profile:
- Create an XML properties file by exporting and modifying the default one. In JPedal go to Edit -> Preferences and click Save As.
- Customise the properties file to your requirements. For example:
<textSelect value="false"/>
<Printbutton value="false"/>
- Create a viewer and load the properties file using
Viewer viewer = new Viewer("path/to/prefs.xml");
Learn more
Visit www.idrsolutions.com/jpedal to learn more about JPedal.
Visit support.idrsolutions.com/jpedal for comprehensive documentation and tutorials.